ios - 如何在 Obj C 中编写没有完成 block 的异步方法的单元测试

标签 ios objective-c asynchronous objective-c-blocks xctest

我有一个在内部调用 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/

相关文章:

node.js - 有没有办法从 node.js 同步调用 AWS Lambda?

ios - 解析 Facebook 登录检查首次登录

ios - 来自另一个 UIViewController Nib 内的 Nib 的自定义 UIView - IBOutlets 为零

iphone - 使用核心数据的奇怪错误

objective-c - 你必须在 UIButton 上调用 removeTarget 吗?

Java NIO 非阻塞模式 vs node.js 异步操作

c# - 我能否在同步和异步 lambda 的类似方法中避免重复代码?

ios - 无法在数组中插入对象,我想将其添加到用户默认值

ios - 选择一个 UITableViewCell 在滚动后选择另一个单元格

iphone - NSObject 类的键值编码是否兼容?