【发布时间】:2021-06-18 23:21:52
【问题描述】:
我想做一个单元测试,看看一个视图控制器是否正在呈现另一个视图控制器。
func testMainTabController_WhenActionButtonIsTapped_NewSuppliersInvoiceControllerIsCreatedV2() {
let sut = MainTabBarController()
sut.loadViewIfNeeded()
let myExpectation = expectation(description: "The sut should present a view controller")
sut.actionButton.sendActions(for: .touchUpInside)
if let x = sut.presentedViewController {
myExpectation.fulfill()
} else {
XCTFail("The view controller was not presented")
}
wait(for: [myExpectation], timeout: 3)
}
这里我得到的结果是失败测试。因为我作为presentedViewController得到了nil。这是按钮的代码
@objc func handleActionButtonTap() {
let suppliersInvoiceController = SuppliersInvoiceController()
let navCon = UINavigationController(rootViewController: suppliersInvoiceController)
navCon.modalPresentationStyle = .fullScreen
present(navCon, animated: true, completion: nil)
}
这是我在测试中写的代码。当我运行单元测试并调用本方法时,按钮中的代码被成功调用。当然,如果我运行该应用程序,它可以正常工作。 当我点击按钮时,我得到了呈现的视图控制器。 如果我在 handleActionButtonTap() 和 print(vc) 中键入 let vc = presentViewController,我会得到导航控制。但是为什么我不能在单元测试里面做呢?
有人知道发生了什么吗?
谢谢
【问题讨论】:
标签: ios swift xcode unit-testing