1

I want to test a ViewController hierarchy without using XCUITests, because they take a huge ton longer than plain old XCTests.

I've had some success but I can't seem to create a ViewController hierarchy more than one level deep.

Here's a test case demonstrating the issue:

class VCHierarchyTests: XCTestCase {

    func test_CanMakeViewControllerHierarchy() {

          let baseVC = UIViewController()
          let vcToPresent = UIViewController()
          let vcOnTopOfThat = UIViewController()
          let vcOnTopOfThatOnTopOfThat = UIViewController()
          newKeyWindowWith(root: baseVC)
          baseVC.present(vcToPresent, animated: false)
          vcToPresent.present(vcOnTopOfThat, animated: false)
          vcOnTopOfThat.present(vcOnTopOfThatOnTopOfThat, animated: false)

          //this passes:
          XCTAssertNotNil(baseVC.presentedViewController)
          //this fails:
          XCTAssertNotNil(vcToPresent.presentedViewController)
          //this fails:
          XCTAssertNotNil(vcOnTopOfThat.presentedViewController)
    }

    private func newKeyWindowWith(root viewController: UIViewController){
           let window = UIWindow(frame: UIScreen.main.bounds)
           window.makeKeyAndVisible()
           window.rootViewController = viewController
    }
}

Is there a way to construct ViewController hierarchies without XCUITest?

Le Mot Juiced
  • 3,761
  • 1
  • 26
  • 46

1 Answers1

1

Try PresentationVerifier from ViewControllerPresentationSpy to unit test how view controllers are presented.

It's designed to intercept a single present() call, not several of them. If your use case falls outside what it does, please file an Issue.

Jon Reid
  • 20,545
  • 2
  • 64
  • 95