【发布时间】:2020-10-06 04:50:34
【问题描述】:
在TestCase 中,我调用了一个异步函数aTestFunction()。在回调中,取决于结果,它决定是满足预期expectation.fulfill()还是失败
failedExpectation.fulfill()
因为它是一个异步函数,所以我需要wait(for: [expectation], timeout: 5.0) 来获取结果。我的问题是'当测试失败failedExpectation.fulfill() 时,我不需要等待 5 秒来等待'期望',当failedExpectation 完成时我该如何停止'等待'?
let expectation = XCTestExpectation(description: "succeed")
let failedExpectation = XCTestExpectation(description: "failed")
failedExpectation.isInverted = true
aTestFunction() { result in
if result == .success {
expectation.fulfill()
} else {
failedExpectation.fulfill()
}
}
wait(for: [expectation], timeout: 5.0)
【问题讨论】: