Home > Blockchain >  Unit test assertion on a coordinator fails because the view controller takes some time to be pushed
Unit test assertion on a coordinator fails because the view controller takes some time to be pushed

Time:12-29

I’m trying to unit test a coordinator I created. The way I’m doing it is using the following assertion:

func test_example() {
    // call a coordinator func that should perform a navigation (using pushViewController()) from AViewController to BViewController.

    XCTAssert(coordinator.navigationController.topViewController is BViewController)
}

The problem with my approach is that, sometimes, BViewController takes time to be pushed, and because of that the assertion runs before the view controller is added to the navigation stack, causing the unit test to fail.

In order to solve the problem, I configured an expectation and I called expectation.fulfill() inside a DispatchQueue.main.asyncAfter() closure.

I don’t like the approach I took. It’s kinda hacky. I’d love to know if there is a better, cleaner way to solve my problem without using DispatchQueue.main.asyncAfter().

Thanks in advance!

CodePudding user response:

Make (and wait for) a predicate expectation asserting that coordinator.navigationController.topViewController is BViewController.

  • Related