Tests for custom UITableViewCell, cellForRowAtIndexPath crashes with nil outlets

后端 未结 4 971
夕颜
夕颜 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:41

    Step 1: Go to Identity Inspector of your view controller in storyboard and give it a storyboard id. Ex: "vcIdentifier"

    Step 2: You have get the instance of your storyboard in Unit test

    Step 2 : Instantiate your view controller via storyboard instance from step 2

    Step 3 : Get access to your table view from the viewcontroller and pass it to 'cellForRowAt' method in your test. Please see below a sample code:

        let storyboard = UIStoryboard(name: "Main", bundle:nil)
        let newsViewController: UITableViewController = storyboard.instantiateViewController(identifier: "vcIdentifier")
        return newsViewController.tableView
    

提交回复
热议问题