mercredi 25 novembre 2015

Unit Testing Segues in iOS 9 with Xcode 7.1.1

I have an XCTest case which looks like this:

func testSuccessfulDropboxLoginSeguesToSectionsViewController() {
    let storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
    if let navigationController = storyboard.instantiateInitialViewController() as? UINavigationController {
        if let dropboxAuthenticationViewController = navigationController.viewControllers.last as? DropboxAuthenticationViewController {
            let _ = dropboxAuthenticationViewController.view

            let numberOfViewControllersPreSegue = navigationController.viewControllers.count
            NSNotificationCenter.defaultCenter().postNotificationName("DropboxFinishedAuthenticating", object: nil)
            let numberOfViewControllersPostSegue = navigationController.viewControllers.count

            XCTAssertEqual(numberOfViewControllersPreSegue + 1, numberOfViewControllersPostSegue)
        }
    }
}

I have storyboard with a root view controller which is a navigation controller. Its root view controller is called DropboxAuthenticationViewController. The notification called "DropboxFinishedAuthenticating" should cause the DropboxAuthenticationViewController to segue to the SectionsViewController. This works correctly in the app.

However, in the test, the number of view controllers on the navigation stack is not getting incremented when the segue is executed (and the segue definitely is being executed) causing the displayed test to fail with the error message:

error: -[AcceptuaryUnitTests.DropboxAuthenticationViewControllerTests testSuccessfulDropboxLoginSeguesToSectionsViewController] : XCTAssertEqual failed: ("Optional(2)") is not equal to ("Optional(1)")

Can anyone shed any light on why segueing does not increment navigationController.viewControllers.count ?

Is there a better way to do this? I prefer not to mock or stub.

Aucun commentaire:

Enregistrer un commentaire