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
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With