mercredi 14 décembre 2016

API violation when using waitForExpectations

I'm running a UI test where I need to test an asynchronous function using the waitForExpectations API. I'm getting this error: "NSInternalInconsistencyException", "API violation - call made to wait without any expectations having been set."

I really don't understand, as I have correctly created the expectation.

Also, there seems to be a documentation bug: according to the documentation the api is expectation(description:) but the compiler won't accept that, instead I need to use XCTestExpectation() to create one.

 func testExample() {
    XCTAssertTrue(state == .STATE_NOT_READY)
    let exp1 = XCTestExpectation()

    let queue = DispatchQueue(label: "net.tech4freedom.AppTest")
    let delay: DispatchTimeInterval = .seconds((2))
    queue.asyncAfter(deadline: .now() + delay) {
        XCTAssertTrue(true)
        exp1.fulfill()
    }

    self.waitForExpectations(timeout: 4){ [weak self] error in
        print("X: async expectation")
        XCTAssertTrue(true)
    }
    self.waitForExpectations(timeout: 10.0, handler: nil)
}

Aucun commentaire:

Enregistrer un commentaire