ios - XCTestExpectation 不等待给定的时间

标签 ios unit-testing swift

我编写了一个测试来检查某些值是否会在 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/

相关文章:

ios - 新 iOS 7 模拟器中的登录弹出错误无法验证用户凭据

ios - 点击文本字段后如何立即隐藏键盘?

javascript - 如何对 node js express 应用程序进行单元测试

unit-testing - automake:自动运行单元测试

ios - 模拟器中的 UITableView 分隔符问题

ios - 在 UICollectionView 单元格下添加 UILabel

visual-studio - 导入Visual Studio测试项目时如何创建vsmdi/testrunco​​nfig文件?

Swift 类型和良好的旧时尚分层单一继承

ios - Swift: 'Tap to Begin' 按钮 - 删除自身

Swift 本地通知 : Type Of Expression is Ambiguous Without More Context