Skip to content

Instantly share code, notes, and snippets.

@byaruhaf
Forked from mdb1/XCTest+AsyncAssert.swift
Created February 14, 2023 13:32
Show Gist options
  • Select an option

  • Save byaruhaf/a53b60b15eec39c27ae46a361fcead54 to your computer and use it in GitHub Desktop.

Select an option

Save byaruhaf/a53b60b15eec39c27ae46a361fcead54 to your computer and use it in GitHub Desktop.
asyncAssert
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