简体   繁体   中英

Unit test assertion on a coordinator fails because the view controller takes some time to be pushed on the navigation stack

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM