Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up asynchronous unit testing using expectations in xCode 6?

What syntax do I use when I want to register an expectation for my asynchronous unit tests in xCode 6? There's nothing easily searchable on Google for this topic yet, and stuff that shows up is written in Swift. I'm looking for Objective-C example

like image 687
Alex Stone Avatar asked Aug 06 '14 16:08

Alex Stone


People also ask

How do I set unit tests in Xcode?

The easiest way to add a unit test target to your project is to select the Include Tests checkbox when you create the project. Selecting the checkbox creates targets for unit tests and UI tests. To add a unit test target to an existing Xcode project, choose File > New > Target.

What is the test method to test an asynchronous operation in XCTest?

XCTest provides two approaches for testing asynchronous code. For Swift code that uses async and await for concurrency, you mark your test methods async or async throws to test asynchronously.

What is expectation in unit test Swift?

The idea behind expectations is that we expect something to happen before completing the test. Since our test class extends XCTestCase we will be able to call the expectation(description:) function, in order to create an expectation for the test.

What is asynchronous testing?

Asynchronous code is common in modern Javascript applications. Testing it is mostly the same as testing synchronous code, except for one key difference: Jasmine needs to know when the asynchronous work is finished. Jasmine supports three ways of managing asynchronous work: async / await , promises, and callbacks.


1 Answers

I have found a nice blog post about it by Sean McCune: Asynchronous Testing With Xcode 6.

For Objective-C he puts an example like so:

- (void)testWebPageDownload
{
    XCTestExpectation *expectation =
        [self expectationWithDescription:@"High Expectations"];

    [self.pageLoader requestUrl:@"http://bignerdranch.com"
              completionHandler:^(NSString *page) {

        NSLog(@"The web page is %ld bytes long.", page.length);
        XCTAssert(page.length > 0);
        [expectation fulfill];
    }];

    [self waitForExpectationsWithTimeout:5.0 handler:^(NSError *error) {
        if (error) {
            NSLog(@"Timeout Error: %@", error);
        }
    }];
}

I suggest reading his post for further explanation.

like image 127
reggian Avatar answered Nov 15 '22 04:11

reggian