Last active
January 11, 2023 00:26
-
-
Save dduan/5507c1e6db78b6ee38d56896764e288c to your computer and use it in GitHub Desktop.
A simple "eventually" method to aide asynchronous testing with XCTest
This file contains 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 | |
extension XCTestCase { | |
/// Simple helper for asynchronous testing. | |
/// Usage in XCTestCase method: | |
/// func testSomething() { | |
/// doAsyncThings() | |
/// eventually { | |
/// /* XCTAssert goes here... */ | |
/// } | |
/// } | |
/// Cloure won't execute until timeout is met. You need to pass in an | |
/// timeout long enough for your asynchronous process to finish, if it's | |
/// expected to take more than the default 0.01 second. | |
/// | |
/// - Parameters: | |
/// - timeout: amout of time in seconds to wait before executing the | |
/// closure. | |
/// - closure: a closure to execute when `timeout` seconds has passed | |
func eventually(timeout: TimeInterval = 0.01, closure: @escaping () -> Void) { | |
let expectation = self.expectation(description: "") | |
expectation.fulfillAfter(timeout) | |
self.waitForExpectations(timeout: 60) { _ in | |
closure() | |
} | |
} | |
} | |
extension XCTestExpectation { | |
/// Call `fulfill()` after some time. | |
/// | |
/// - Parameter time: amout of time after which `fulfill()` will be called. | |
func fulfillAfter(_ time: TimeInterval) { | |
DispatchQueue.main.asyncAfter(deadline: .now() + time) { | |
self.fulfill() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment