![](/img/trans.png)
[英]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.