Objective-C Language Testing asynchronous block of code:


Example

- (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];
}
  • Feed the dummy data to the method under test if required.
  • The test will pause here, running the run loop, until the timeout is hit or all expectations are fulfilled.
  • Timeout is the expected time for the asynchronous block to response.