Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

navigationController is nil in Swift XCTest case

How do you mock/stub navigationController in a test case when you're testing a view controller? I am getting a runtime exception because its nil in my UIViewController when running tests. You can't set it directly because it's read-only. I'm using Swift 2.2, and XCTest.

like image 909
tyler Avatar asked Mar 31 '16 16:03

tyler


1 Answers

As described in the comment above, this is possible by instantiating a UINavigationController and adding the view controller you are trying to test to its view array. Code:

let navigationController = UINavigationController()
let yourViewController = YourUIViewController()
navigationController.viewControllers = [yourViewController]
like image 62
gregkerzhner Avatar answered Nov 17 '22 11:11

gregkerzhner