【发布时间】:2018-02-16 10:40:09
【问题描述】:
我正在为异步操作编写一些测试用例,其中我有两个需要执行单元测试的操作。
假设我有一些登录 web 服务需要调用,并且响应应该调用另一个配置文件 web 服务。
是否可以在 iOS 中使用单元测试来测试上述场景?
提前谢谢你。
【问题讨论】:
标签: swift xcode xctestcase
我正在为异步操作编写一些测试用例,其中我有两个需要执行单元测试的操作。
假设我有一些登录 web 服务需要调用,并且响应应该调用另一个配置文件 web 服务。
是否可以在 iOS 中使用单元测试来测试上述场景?
提前谢谢你。
【问题讨论】:
标签: swift xcode xctestcase
是的,这实际上很简单,您可以使用expectations 等待任务完成。
例子:
// Create an expectation for a background download task.
let expectation = XCTestExpectation(description: "Login and fetch profile")
MyApiClient.shared.login(username: username, password: password) { auth in
// check login was successful before continuing
MyApiClient.shared.fetchUserProfile(userId: auth.userId) { profile in
XCTAssertNotNil(profile)
// Fulfill the expectation to indicate that the background task has finished successfully.
expectation.fulfill()
}
}
// Wait until the expectation is fulfilled, with a timeout of 10 seconds.
wait(for: [expectation], timeout: 10.0)
【讨论】:
查看 Apple 提供的有关测试异步操作的资源:@987654321@
【讨论】: