API violation when using waitForExpectations

I run a UI test where I need to test an asynchronous function using the waitForExpectations API.

I get this error:

"NSInternalInconsistencyException", "API Violation - A Call Waiting to Wait without Waiting."

I really do not understand, since I created the expectation correctly.

Also, there seems to be a bug in the documentation: according to the API documentation expectation(description:) , but the compiler will not agree with this, instead I need to use XCTestExpectation() to create it.

  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) } 
+6
source share
1 answer

Well, your mistake is that you are trying to instantiate the wait directly. The documents clearly say

Use the following XCTestCase methods to create XCTest Expectation instances:
- waiting (description :)

This means that you must create expectations as follows:

 func testMethod() { let exp = self.expectation(description: "myExpectation") // your test code } 
+8
source

Source: https://habr.com/ru/post/1013152/


All Articles