我正在使用XCTestExpectation测试异步调用。
当在给定的1秒超时之前执行completionHandler时,以下代码有效(测试成功)。
func test__async_call() {
// prepare
let sut = ClassToTest()
let expectation: XCTestExpectation = self.expectationWithDescription(nil)
// test
sut.methodToTestWithCompletionHandler() { () -> () in
expectation.fulfill()
}
// verify
self.waitForExpectationsWithTimeout(1, handler: nil)
}
但是,如果没有调用completionHandler,因此没有满足期望,而是在调用waitForExpectationsWithTimeout时没有得到测试失败,我得到一个EXC_BAD_ACCESS,这不是很方便,因为这样就不可能看到整个测试套件的结果
如何避免这种情况并获得正常的测试失败?
答案 0 :(得分:4)
在创建期望时,似乎导致EXC_BAD_ACCESS的原因是传递了一个nil描述。
将任何字符串传递给此调用使其工作,并且在未满足期望时我们得到预期的测试失败。
let expectation: XCTestExpectation = self.expectationWithDescription("...")