点击按钮后,UIImagePickerController将显示为模式视图控制器,代码如下:
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.delegate = self;
[self presentViewController:imagePicker animated:YES completion:nil];
它在模拟器和设备上运行良好,但我想为它添加一个单元测试(使用GHUnit),并试图测试presentdViewController是否为零。
然而,当我运行测试时,我会将下面的警告打印到控制台。有人知道如何绕过这个,这样我就可以正确地测试它吗?
Warning: Attempt to present <UIImagePickerController: 0xab5e990> on <UINavigationController: 0xab5a790> whose view is not in the window hierarchy!
顺便说一句,我已经为这个特定的测试文件设置了shouldRunOnMainThread返回YES。
在单元测试期间,您的ViewController不会显示在窗口上,因此它不能显示其他Coordinator。例如,您需要在设置方法中创建一个UIWindow,并在测试中在窗口上显示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()
...
}
...
}