Test Segue-Based Modal Navigation

We’ve come to the last of our navigation types, the segue-based modal. Add the following test:

Navigation/NavigationTests/ViewControllerTests.swift
 func​ ​test_tappingSegueModalButton_shouldShowSegueNextViewController​() {
 let​ presentationVerifier = ​PresentationVerifier​()
 
 tap​(sut.segueModalButton)
 
 let​ segueNextVC: ​SegueNextViewController​? = presentationVerifier.​verify​(
  animated: ​true​, presentingViewController: sut)
 XCTAssertEqual​(segueNextVC?.labelText, ​"Modal from segue"​)
 }

Run the tests, which should pass.

Unfortunately, by peering into the console output, we can see that both view controllers still exist. There may be a trick I don’t know yet, but as of this writing, I haven’t found a way to clean them out. The presenting view controller and the presented view controller both live on past the life cycle of the test.

This violates our clean room goals. The best we can do is mitigate the effects. If either view controller has a persistent side effect, then provide a backdoor for tests to clean it up. Add a cleanup method wrapped in #if DEBUG ... #endif so that it’s not included in your shipping app. Call this method at the end of your test or at the beginning of tearDown if the view controller is in the test fixture.

It’s not a perfect world. But we were able to test the modal presentation, so we’ll take what we can get.