Tyler
Tyler

Reputation: 19848

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.

Upvotes: 5

Views: 1368

Answers (1)

gregkerzhner
gregkerzhner

Reputation: 766

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]

Upvotes: 7

Related Questions