【问题标题】:How to Write Unit test of a async method without completion Block in Obj C如何在 Obj C 中编写没有完成块的异步方法的单元测试
【发布时间】:2016-12-17 22:19:50
【问题描述】:

我有一个内部调用 API 的方法。该方法没有任何完成处理程序。

-(void) methodToBeTested{
    [self callAPIWithCompletionHandler:^(NSArray *data,NSError *error)
     {
         //Here I get the response and sets the models.
     }];
}

现在我需要测试API调用后设置的模型的方法“methodToBeTested”基础。

有什么建议吗?

【问题讨论】:

  • 老实说,这看起来更像是一个集成测试。对于单元测试,如果需要为其他测试组件做准备,通常会存根此方法并返回模拟数据。

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


【解决方案1】:

例子:

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];
}];

查看使用 Xcode 文档进行测试下的异步操作的编写测试。 https://developer.apple.com/library/content/documentation/DeveloperTools/Conceptual/testing_with_xcode/chapters/04-writing_tests.html

【讨论】:

  • 在问题中说他不能使用完成块,但在答案中期望你需要一个完成块
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多