【发布时间】:2014-10-15 16:08:53
【问题描述】:
我编写了一个测试来检查某个值是否会在 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 秒,只是立即崩溃。发生了什么事?
【问题讨论】:
标签: ios unit-testing swift