-
-
Save byaruhaf/a53b60b15eec39c27ae46a361fcead54 to your computer and use it in GitHub Desktop.
asyncAssert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import XCTest | |
| public extension XCTestCase { | |
| /// Runs the assertions provided in the main thread in an async way. | |
| /// Useful for testing objects that make async requests (using mocked protocols for the responses) | |
| /// - Parameters: | |
| /// - expectation: The string representation of what we expect. | |
| /// - asyncWaitDuration: Duration in seconds to wait before trying to assert the assertions. Default: 0.05. | |
| /// - timeout: Maximum time in seconds to wait for the expectation. Default: 1 (plus the async await duration). | |
| /// - assertions: escaping method containing all the assertions. | |
| func asyncAssert( | |
| _ expectation: String, | |
| after asyncWaitDuration: Double = 0.05, | |
| timeout: Double = 1, | |
| assertions: @escaping () -> Void | |
| ) { | |
| let expectation = XCTestExpectation(description: expectation) | |
| DispatchQueue.main.asyncAfter(deadline: .now() + asyncWaitDuration) { | |
| assertions() | |
| expectation.fulfill() | |
| } | |
| wait(for: [expectation], timeout: timeout + asyncWaitDuration) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment