我有一个在内部调用 API 的方法。该方法没有任何完成处理程序。
-(void) methodToBeTested{
[self callAPIWithCompletionHandler:^(NSArray *data,NSError *error)
{
//Here I get the response and sets the models.
}];
}
现在我需要测试 API 调用后设置的模型的方法“methodToBeTested”基础。
有什么建议吗?
最佳答案
例子:
XCTestExpectation *documentOpenExpectation = [self expectationWithDescription:@"document open"];
NSURL *URL = [[NSBundle bundleForClass:[self class]]
URLForResource:@"TestDocument" withExtension:@"mydoc"];
UIDocument *doc = [[UIDocument alloc] initWithFileURL:URL];
[doc openWithCompletionHandler:^(BOOL success) {
XCTAssert(success);
[documentOpenExpectation fulfill];
}];
[self waitForExpectationsWithTimeout:1 handler:^(NSError *error) {
[doc closeWithCompletionHandler:nil];
}];
查看 Testing with Xcode 文档下的 Writing Tests of Asynchronous Operations。 https://developer.apple.com/library/content/documentation/DeveloperTools/Conceptual/testing_with_xcode/chapters/04-writing_tests.html
关于ios - 如何在 Obj C 中编写没有完成 block 的异步方法的单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38891918/