在 Swift Testing 中创建异步期望
Swift Testing 框架提供了一个 confirmation 方法,该方法可以测试异步代码。然而,与 XCTest 的 XCTestExpectation 不同,此 confirmation
必须在 confirmation 的 body
完成之前进行确认。Swift Testing 没有开箱即用的方法来确保期望在未来的某个不确定的时间点被满足。
从此库导出的 Expectation
填补了这一空白
@Test func testMethodEventuallyTriggersClosure() async {
let expectation = Expectation()
systemUnderTest.closure = { expectation.fulfill() }
systemUnderTest.method()
await expectation.fulfillment(within: .seconds(5))
}
从此库导出的 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))
}
要使用 Swift Package Manager 在您的项目中安装 swift-testing-expectation,可以将以下行添加到您的 Package.swift
文件中
dependencies: [
.package(url: "https://github.com/dfed/swift-testing-expectation", from: "0.1.0"),
]
要使用 CocoaPods 在您的项目中安装 swift-testing-expectation,请将以下内容添加到您的 Podfile
中
pod 'TestingExpectation', '~> 0.1.0'
我很高兴您对 swift-testing-expectation 感兴趣,并且我很想看看您将它带到哪里。请在提交 Pull Request 之前阅读贡献指南。