Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

API violation when using waitForExpectations

Tags:

ios

testing

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) } 
like image 441
onthemoon Avatar asked Dec 14 '16 14:12

onthemoon


1 Answers

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 } 
like image 68
Losiowaty Avatar answered Sep 17 '22 23:09

Losiowaty