rah*_*ulg 2 unit-testing xctest swift xctestexpectation xctwaiter
我想在 Swift 中测试异步函数,因此如下所示,我创建了一个XCTestExpectation并将其传递给XCTWaiter. 现在,无论期望是否得到满足,我总是能成功运行测试。
你能指出代码中有什么问题吗?我完全关注了为 Swift 3 制作的博客,但是,我正在运行 Swift 4。这是问题吗?
func testAsyncFunction() {
let expectation = XCTestExpectation(description: "Some description")
vc.asyncFunction(5) { (abc: Int) -> Int in
if (abc != 25) {
// expectation.fulfill()
}
return 0
}
_ = XCTWaiter.wait(for: [expectation], timeout: 2.0)
}
Run Code Online (Sandbox Code Playgroud)
XCTWaiter.wait返回一个XCTWaiter.Result你应该观察的。
func testAsyncFunction() {
let expectation = XCTestExpectation(description: "Some description")
vc.asyncFunction(5) { (abc: Int) -> Int in
if (abc != 25) {
// expectation.fulfill()
}
return 0
}
let result = XCTWaiter.wait(for: [expectation], timeout: 2.0) // wait and store the result
XCTAssertEqual(result, .timedOut) // check the result is what you expected
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2166 次 |
| 最近记录: |