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

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

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

点赞