I've recreated the example from here: http://www.mokacoding.com/blog/testing-callbacks-in-swift-with-xctest/.
I want to test for a timeout using waitForExpectations()
. This should mimic a long running process that has timed out. To do this, I've set a sleep()
command in the called function that is longer than the timeout in waitForExpectations()
.
However, the sleep()
doesn't have any effect. The test always passes. I've tried putting sleep()
before completion(true)
as well but that doesn't change the outcome (i.e., passed test).
Any ideas what I'm doing run to trigger a test failure on timeout?
class SomeService {
func doSomethingAsync(completion: (_ success: Bool) -> ()) {
completion(true)
sleep(5)
}
}
In test class
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)")
}
}