【发布时间】:2016-03-19 04:14:44
【问题描述】:
在下面的代码中,我想测试是否调用了“DisplayHelper.displayAlert”。我正在依赖注入 DisplayHelper 和 AuthService 模拟对象,并使用 PromiseKit
在我的视图控制器中:
@IBAction func submitButtonPressed(sender: AnyObject) {
AuthService.updateUser(["zipcode": self.zipcodeTextField.text!])
.then { user -> Void in
// sucess code
}.error { error -> Void in
self.DisplayHelper.displayAlert("Zipcode Not Found", message: "We can't find that zipcode... please try again.", callingViewController: self)
}
}
以下是不工作的测试:
func testSubmitButtonServerCantFindZipcode() {
vc.zipcodeTextField.text = "00000"
vc.submitButtonPressed(self)
// called AuthService (passes)
XCTAssertEqual(authServiceMock.updateUserCalled, true)
// called displayAlert (fails because tests run before DisplayAlert is called)
XCTAssertEqual(displayHelperMock.displayAlertCalled, true)
}
如何让测试在断言之前等待所有代码执行?
【问题讨论】:
标签: ios swift unit-testing xcode7 xctest