I am trying to update my asynchronous unit tests to use the new XCTestExpectation interface instead of manually starting the run loop.
My unit tests previously used the functions waitForBlock , finishBlock and waitForTimeInterval: which are just a convenient method called finishBlock after the specified time. I am trying to update this setting to use expectations.
Tests using waitForBlock + finishBlock semantics work just as expected after replacing with waitForExpectationsWithTime:handler: and fulfill , but my solution to replace waitForTimeInterval: does not seem to work.
- (void)waitForTimeInterval:(NSTimeInterval)delay { XCTestExpectation *expectation = [self expectationWithDescription:@"wait"]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [expectation fulfill]; }); [self waitForExpectationsWithTimeout:delay + 1 handler:nil]; }
Edit:
It seems that this code really works ... so it was probably just the Xcode 6 that it burst into with me this afternoon.
I feel this should be fairly straightforward: create a wait, configure an asynchronous block that executes and wait. However, the dispatch_after block is never called.
My hunch is that waitForExpectationsWithTimeout:handler: blocks its current thread, which is the main queue, so the loop cycle never gets close to its asynchronous blocks. This seems reasonable, but I'm having problems with another way to implement this functionality.
I'm looking for either 1) additional information about XCTestExpectation , which may reveal a workaround, or 2) another idea for implementing this function.
ios unit-testing xcode6
Leffelmania
source share