[英]How to check if a view controller is presented modally or pushed on a navigation stack?
[英]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.