我编写了一个测试来检查某些值是否会在 5 秒内降为零。每隔一秒 NSTimer 都会触发一个将此值减一的方法。
看起来像这样:
let foo = SomeObject(initWithTime: 5)
let expectation = expectationWithDescription("Ohai")
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: foo, selector: Selector("decrement"), userInfo: nil, repeats: true)
XCTAssertTrue(foo.isFinished, "Foo is finished, so it should be true")
expectation.fulfill()
waitForExpectationsWithTimeout(5.0, handler: nil)
当 foo 达到零时,isFinished 设置为 true。定时器工作正常,但期望不会等待 5 秒,只是立即崩溃。发生了什么事?
最佳答案
您的 XCTAssertTrue 似乎立即失败,因为 foo 在立即调用时可能尚未完成。
关于ios - XCTestExpectation 不等待给定的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26387152/