测试异步代码块

- (void)testDoSomethingThatTakesSomeTime{
XCTestExpectation *completionExpectation = [self expectationWithDescription:@"Long method"];
[self.someObject doSomethingThatTakesSomeTimesWithCompletionBlock:^(NSString *result) {
    XCTAssertEqualObjects(@"result", result, @"Result was not correct!");
    [completionExpectation fulfill];
}];
[self waitForExpectationsWithTimeout:5.0 handler:nil];
}
  • 如果需要,将虚拟数据提供给测试方法。
  • 测试将在此处暂停,运行运行循环,直到达到超时或满足所有期望。
  • 超时是异步块响应的预期时间。