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) }
Ok, your mistake is that you try to instantiate the expectation directly. The docs clearly say
Use the following XCTestCase methods to create XCTestExpectation instances:
- expectation(description:)
This means, that you should create expectations like this :
func testMethod() { let exp = self.expectation(description: "myExpectation") // your test code }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With