XCTest and asynchronous testing in Xcode 6

Dimillian picture Dimillian · Jul 11, 2014 · Viewed 21.1k times · Source

So Apple said in the release note of Xcode 6 that we can now do asynchronous testing directly with XCTest.

Anyone knows how to do it using Xcode 6 Beta 3 (Using objective-C or Swift)? I don't want the known semaphore method, but the new Apple way.

I searched into the released note and more but I found nothing. The XCTest header is not very explicit either.

Answer

jonbauer picture jonbauer · Sep 19, 2014

Obj-C example:

- (void)testAsyncMethod
{

    //Expectation
    XCTestExpectation *expectation = [self expectationWithDescription:@"Testing Async Method Works!"];

    [MyClass asyncMethodWithCompletionBlock:^(NSError *error, NSHTTPURLResponse *httpResponse, NSData *data) {

        if(error)
        {
            NSLog(@"error is: %@", error);
        }else{
            NSInteger statusCode = [httpResponse statusCode];
            XCTAssertEqual(statusCode, 200);
            [expectation fulfill];
        }

    }];


    [self waitForExpectationsWithTimeout:5.0 handler:^(NSError *error) {

        if(error)
        {
            XCTFail(@"Expectation Failed with error: %@", error);
        }

    }];
}