【问题标题】:XCTestExpectation doesn't wait for given amount of timeXCTestExpectation 不会等待给定的时间
【发布时间】: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


    【解决方案1】:

    您的 XCTAssertTrue 似乎立即失败,因为 foo 在立即调用时可能尚未完成。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-25
      • 1970-01-01
      • 1970-01-01
      • 2020-10-30
      • 1970-01-01
      • 2017-10-17
      • 1970-01-01
      相关资源
      最近更新 更多