Testing If A View Controller Got Pushed
I want to write a unit test that tests if a view controller is pushed onto the navigation stack. Here is the solution I came up with:
//
func testViewControllerGotPushedStoryboardVersion() {
class MockNavigationController: UINavigationController {
var pushedViewController: UIViewController?
private override func pushViewController(viewController: UIViewController, animated: Bool) {
pushedViewController = viewController
super.pushViewController(viewController, animated: animated)
}
}
let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("MasterViewController") as MasterViewController
let mockNavigationController = MockNavigationController(rootViewController: viewController)
let expectation = expectationWithDescription("should push")
viewController.performSegueWithIdentifier("showDetail", sender: nil)
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(NSEC_PER_SEC*0)), dispatch_get_main_queue()) { () -> Void in
XCTAssertTrue(mockNavigationController.pushedViewController is DetailViewController, "")
expectation.fulfill()
}
waitForExpectationsWithTimeout(1, handler: nil)
}
What do you think? Is there a better way to test this?