Tests for custom UITableViewCell, cellForRowAtIndexPath crashes with nil outlets

后端 未结 4 1002
夕颜
夕颜 2021-01-21 22:58

I have a ViewController that contains a tableView. Since I need to keep the code well covered with tests, I need to write a test for [tableView:cellForRowAtIndexPath]

         


        
4条回答
  •  迷失自我
    2021-01-21 23:37

    There are two separate concepts, the tableView and the dataSource. TableView is just a view, and has cells which are views as well. DataSource is the data provider of the tableView, and the only component responsible for data. In your test example, you call a tableView.cellForRowAtIndexPath function in order to get a cell, in a specific indexPath. Based on official docs, this function will return nil if the cell is not visible. So this function should be used, only when you know that the table is visible. In your example, you are trying to test that your table will load data properly. When referring to data, data source is the first thing that should come to your mind. So you need to test that the dataSource will bind the correct information to your cells. As a result you call controller.tableView.dataSource(controller.tableView, cellForRowAt: IndexPath(row: 0, section: 0)))

提交回复
热议问题