簡體   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