Create an asynchronous expectation in Swift Testing
The Swift Testing framework vends a confirmation method which enables testing asynchronous code. However unlike XCTest’s XCTestExpectation, this confirmation
must be confirmed before the confirmation’s body
completes. Swift Testing has no out-of-the-box way to ensure that an expectation is fulfilled at some indeterminate point in the future.
The Expectation
vended from this library fills that gap:
@Test func testMethodEventuallyTriggersClosure() async {
let expectation = Expectation()
systemUnderTest.closure = { expectation.fulfill() }
systemUnderTest.method()
await expectation.fulfillment(within: .seconds(5))
}
The Expectations
type vended from this library makes it easy to wait for multiple expectations:
@Test func testMethodEventuallyTriggersClosures() async {
let expectation1 = Expectation()
let expectation2 = Expectation()
let expectation3 = Expectation()
systemUnderTest.closure1 = { expectation1.fulfill() }
systemUnderTest.closure2 = { expectation2.fulfill() }
systemUnderTest.closure3 = { expectation3.fulfill() }
systemUnderTest.method()
await Expectations(expectation1, expectation2, expectation3).fulfillment(within: .seconds(5))
}
To install swift-testing-expectation in your project with Swift Package Manager, the following lines can be added to your Package.swift
file:
dependencies: [
.package(url: "https://github.com/dfed/swift-testing-expectation", from: "0.1.0"),
]
To install swift-testing-expectation in your project with CocoaPods, add the following to your Podfile
:
pod 'TestingExpectation', '~> 0.1.0'
I’m glad you’re interested in swift-testing-expectation, and I’d love to see where you take it. Please read the contributing guidelines prior to submitting a Pull Request.