如果 XCTestExpectation 出乎意料怎么办

What if XCTestExpectation is unexpected

我正在 Swift 中编写 XCTest 单元测试。 这个想法是在特定情况下不能调用回调。

所以我所做的是

func testThatCallbackIsNotFired() {

    let expectation = expectationWithDescription("A callback is fired")
    // configure an async operation

    asyncOperation.run() { (_) -> () in
        expectation.fulfill()    // if this happens, the test must fail
    }

    waitForExpectationsWithTimeout(1) { (error: NSError?) -> Void in

        // here I expect error to be not nil,
        // which would signalize that expectation is not fulfilled,
        // which is what I expect, because callback mustn't be called
        XCTAssert(error != nil, "A callback mustn't be fired")
    }
}

调用回调时,一切正常:它失败并显示消息“不得触发回调”,这正是我需要的。

但是如果期望没有实现,它就会失败并说

异步等待失败:超时超过 1 秒,未满足预期:"Callback is fired".

因为没有实现的期望是我所需要的,所以我不想有一个失败的测试。

您有什么建议可以避免这种情况吗?或者,也许,我可以用不同的方式实现我的目标?谢谢

我遇到了同样的问题,我很生气您不能使用处理程序来覆盖 waitForExpectationsWithTimeout 的超时失败。这是我解决它的方法(Swift 2 语法):

func testThatCallbackIsNotFired() {
    expectationForPredicate(NSPredicate{(_, _) in
        struct Holder {static let startTime = CACurrentMediaTime()}

        if checkSomehowThatCallbackFired() {
            XCTFail("Callback fired when it shouldn't have.")
            return true
        }

        return Holder.startTime.distanceTo(CACurrentMediaTime()) > 1.0 // or however long you want to wait
        }, evaluatedWithObject: self, handler: nil)
    waitForExpectationsWithTimeout(2.0 /*longer than wait time above*/, handler: nil)
}

像这样使用 isInverted post https://www.swiftbysundell.com/posts/unit-testing-asynchronous-swift-code

class DebouncerTests: XCTestCase {
    func testPreviousClosureCancelled() {
        let debouncer = Debouncer(delay: 0.25)

        // Expectation for the closure we'e expecting to be cancelled
        let cancelExpectation = expectation(description: "Cancel")
        cancelExpectation.isInverted = true

        // Expectation for the closure we're expecting to be completed
        let completedExpectation = expectation(description: "Completed")

        debouncer.schedule {
            cancelExpectation.fulfill()
        }

        // When we schedule a new closure, the previous one should be cancelled
        debouncer.schedule {
            completedExpectation.fulfill()
        }

        // We add an extra 0.05 seconds to reduce the risk for flakiness
        waitForExpectations(timeout: 0.3, handler: nil)
    }
}