測試非同步程式碼塊

- (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];
}
  • 如果需要,將虛擬資料提供給測試方法。
  • 測試將在此處暫停,執行執行迴圈,直到達到超時或滿足所有期望。
  • 超時是非同步塊響應的預期時間。