Skip to content

Instantly share code, notes, and snippets.

@levigroker
Created January 19, 2012 23:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save levigroker/1643797 to your computer and use it in GitHub Desktop.
Save levigroker/1643797 to your computer and use it in GitHub Desktop.
Sample GHUnit Asynchronous Test
#import <GHUnitIOS/GHUnit.h>
@interface RemoteDataServiceTest : GHAsyncTestCase { }
@end
@implementation RemoteDataServiceTest
- (BOOL)shouldRunOnMainThread
{
// By default NO, but if you have a UI test or test dependent on running on the main thread return YES.
// Also an async test that calls back on the main thread, you'll probably want to return YES.
return NO;
}
- (void)testAsynchronousOperation
{
// Call prepare to setup the asynchronous action.
// This helps in cases where the action is synchronous and the
// action occurs before the wait is actually called.
[self prepare];
[RemoteDataService doNiftyStuffRemotelyWithCompletedBlock:^(id result){
if (!result)
{
GHTestLog(@"result from remote service was unexpectedly nil.");
[self notify:kGHUnitWaitStatusFailure forSelector:_cmd];
return;
}
GHTestLog(@"We got back: %@", result);
[self notify:kGHUnitWaitStatusSuccess forSelector:_cmd];
} errorBlock:^(NSError *error){
GHTestLog(@"Error while doing nifty stuff: %@", [error localizedDescription]);
[self notify:kGHUnitWaitStatusFailure forSelector:_cmd];
}];
// Wait until notify called for timeout (seconds); If notify is not called with kGHUnitWaitStatusSuccess then
// we will throw an error.
[self waitForStatus:kGHUnitWaitStatusSuccess timeout:30.0];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment