首页 > 解决方案 > 在 IOS 中使用异步代码编写单元测试

问题描述

我需要测试我的 Api 调用响应但在块返回单元测试完成之前我如何测试我的 API

标签: iosswiftxcode

解决方案


你可以使用XCTestExpectation它。

XCTestExpectation *apiCallExpectation = [self expectationWithDescription:@"APICall"];

[apiService apiCall:^(BOOL success) {
    XCTAssert(success);
    [apiCallExpectation fulfill];
}];

[self waitForExpectationsWithTimeout:5 handler:^(NSError *error) {
    [apiCallExpectation closeWithCompletionHandler:nil];
}];

推荐阅读