繁体   English   中英

协调器上的单元测试断言失败,因为视图 controller 需要一些时间才能推送到导航堆栈

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

我正在尝试对我创建的协调器进行单元测试。 我这样做的方式是使用以下断言:

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

    XCTAssert(coordinator.navigationController.topViewController is BViewController)
}

我的方法的问题是,有时, BViewController需要时间才能被推送,因此断言在将视图 controller 添加到导航堆栈之前运行,导致单元测试失败。

为了解决这个问题,我配置了一个expectation ,并在DispatchQueue.main.asyncAfter()闭包中调用了expectation.fulfill()

我不喜欢我采取的方法。 这有点hacky。 我很想知道是否有更好更简洁的方法来解决我的问题而不使用DispatchQueue.main.asyncAfter()

提前致谢!

创建(并等待)断言coordinator.navigationController.topViewController is BViewController断言期望

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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