【发布时间】:2017-06-21 16:06:26
【问题描述】:
我从这里重新创建了示例:http://www.mokacoding.com/blog/testing-callbacks-in-swift-with-xctest/。
我想使用waitForExpectations() 测试超时。这应该模拟一个已超时的长时间运行的进程。为此,我在被调用函数中设置了一个sleep() 命令,该命令比waitForExpectations() 中的超时时间长。
但是,sleep() 没有任何作用。测试总是通过。我也尝试将sleep() 放在completion(true) 之前,但这不会改变结果(即通过测试)。
任何想法我正在做什么以在超时时触发测试失败?
class SomeService {
func doSomethingAsync(completion: (_ success: Bool) -> ()) {
completion(true)
sleep(5)
}
}
在测试课中
let service = SomeService()
service.doSomethingAsync { (success) in
XCTAssertTrue(success, "assert is true")
expect.fulfill()
}
waitForExpectations(timeout: 3) { (error) in
if let error = error {
XCTFail("timeout errored: \(error)")
}
}
【问题讨论】:
标签: swift xcode xctest timeoutexception