I've written a library implementing a "Promise". It turned out that this concept is not only useful for solving complex asynchronous problems, but also for testing:
Suppose, you have some async method and want to check whether a completion handler will be called, and whether it returns the expected result. As a bonus, you also want to set a timeout which the tester waits for a completion handler. If it it expires, the test should fail.
A generic completion hander block:
typedef void (^completion_block_t)(id result);
Your testie, an async method which may be run loop based:
- (void) asyncFooWithCompletion:(completion_block_t)completionHandler;
Your test may look as follows:
- (void) testAsyncFooWithCompletion
{
// Your test - executing on the main thread.
RXPromise* handlerPromise = [RXPromise new];
[foo asyncFooWithCompletion:^(id result){
// possibly perform assertions
...
if (result is expected) {
// expected in this test scenario
// resolve promise with a value indicating success:
[handlerPromise fulfillWithValue:@"OK"];
}
else {
// unexpected in this test scenario
// resolve promise with an error:
[handlerPromise rejectWithReason:@"Unexpected result"];
}
}];
Note: asyncFooWithCompletion:
s workload may be scheduled on a run loop, which executes on the corresponding thread - e.g. the main thread.
The completion handler may also be executed on the same run loop, e.g. on the main thread.
// Set a timeout. If the timeout expires before the handler get called,
// the promise will be resolved with a "timeout error":
[handlerPromise setTimeout:5.0];
// Register handlers, which get called when _handlerPromise_ will be resolved.
// We did perform a all assertions in the completion handler. Thus here,
// just catch unexpected failures - including a timeout:
[handlerPromise.thenOn(dispatch_get_main_queue(), nil,
^id(NSError* error) {
// We are on the main thread
ADD_FAILURE() << [[error description] UTF8String];
return error;
})
runLoopWait]; // wait on the current run loop.
// if we reach here, assertions have been executed (on the main thread)
// and all handlers have been returned.
} // end test