Tuesday, September 27, 2016

Writing Tests of Asynchronous Operations- Testing with Xcode


XCTest에서 expectation 을 통해 Async Op.을 테스트 할수 있다.
XCTestCase+AsynchronousTesting.h 참조.


// Test that the document is opened. Because opening is asynchronous,

// use XCTestCase's asynchronous APIs to wait until the document has

// finished opening.

- (void)testDocumentOpening

{

    // Create an expectation object.

    // This test only has one, but it's possible to wait on multiple expectations.

    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);

        // Possibly assert other things here about the document after it has opened...



        // Fulfill the expectation-this will cause -waitForExpectation

        // to invoke its completion handler and then return.

        [documentOpenExpectation fulfill];

    }];



    // The test will pause here, running the run loop, until the timeout is hit

    // or all expectations are fulfilled.

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

        [doc closeWithCompletionHandler:nil];

    }];

}


No comments:

Post a Comment