1

I'm trying to write a unit test that a view controller is presented once a row is selected. The cell declaration doesn't work, because for some reason you can't call didSelectRow on the tableview. Also, I get an error that presentedVC is nil:

func testDidSelectNewsReportCalledWhenNewsReportSelected() {
    var cell = tableView.didSelectRow(at: IndexPath(row: 0, section: 3), animated: false) //This line doesn't work

   let presentedVC = controller.presentedViewController?.view
    let newsReportVC = UIStoryboard(name:"News", bundle: Bundle.init(for: NewsViewController.self)).instantiateViewController(withIdentifier: "NewsReport") as! NewsReportViewController
    XCTAssertEqual(newsReportVC, presentedVC)
}

Thanks.

Number45
  • 139
  • 3
  • 9
  • 1
    Set the tableView.delegate = self , and check this thread how to implement didSelectRow, you are doing it wrong. http://stackoverflow.com/questions/26740538/swift-uitableview-didselectrowatindexpath-diddeselectrowatindexpath-add-re –  Feb 13 '17 at 22:15

1 Answers1

1

In order to test this, you will have to look at the presentation stack (if you are presenting the view controller) or the navigation stack (if you are using a navigation controller.

Also, programmatically selecting a table view index path will not cause it to call it's delegate's didSelectRow method.

Don't bother testing to see if selecting a particular row causes the delegate's didSelect method to get called. Trust that Apple implemented their code correctly. All you need to test is that the table view has the correct delegate and that when didSelect is called, it does the right thing.

That said, testing view controller operation, especially presents and dismisses or pushes and pops is notoriously difficult and very slow. Don't do it. Move as much of your code as you can into the model layer and just test your models.

Daniel T.
  • 32,821
  • 6
  • 50
  • 72