簡體   English   中英

使用 GHUnit 測試presentedViewController

[英]Testing for presentedViewController with GHUnit

點擊按鈕后, UIImagePickerController 將顯示為具有以下代碼的模態視圖控制器:

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.delegate = self;
[self presentViewController:imagePicker animated:YES completion:nil];

它在模擬器和設備上運行良好,但我想為它添加一個單元測試(使用 GHUnit)並嘗試測試presentedViewController 是否為空。

但是,當我運行測試時,我將下面的警告打印到控制台。 有誰知道如何解決這個問題,以便我可以正確測試?

Warning: Attempt to present <UIImagePickerController: 0xab5e990> on <UINavigationController: 0xab5a790> whose view is not in the window hierarchy!

順便說一句 - 我已經設置了 shouldRunOnMainThread 來為這個特定的測試文件返回 YES。

在單元測試期間,您的 ViewController 不會顯示在 Window 上,因此它無法顯示其他 Coordinator。 您需要創建一個 UIWindow,例如在您的 setup 方法中,並在您的測試中的窗口上顯示 UINavigationController。

final class YourTest: XCTestCase {

    private var window: UIWindow!

    override func setUp() {
        super.setUp()

        window = UIWindow()
    }

    override func tearDown() {
        window = nil
        super.tearDown()
    }

    func testSomething() {
        let controller = YourCustomController()
        window.rootViewController = controller
        window.makeKeyAndVisible()

        controller.callSomeMethod()
        ...
    }

    ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM