Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@shawnwall
Created March 27, 2012 11:41
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shawnwall/2215212 to your computer and use it in GitHub Desktop.
Save shawnwall/2215212 to your computer and use it in GitHub Desktop.
Unit testing blocks with AFNetworking example
/*
* SenTestingKit does not wait for blocks to finish by default so your test would simply complete
* as successful. You need to use a semaphore and keep the run loop going in order for the test
* to properly run.
*/
- (void)testGetSpots {
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
CLLocation location = [[CLLocation alloc] initWithLatitude:70.0 longitude:50.0];
[Spot spotsWithURLString:@"/spots" near:location parameters:[NSDictionary dictionaryWithObject:@"128" forKey:@"per_page"] block:^(NSArray *records) {
//sample assert
STAssertNotNil(records,@"Could not load spots");
dispatch_semaphore_signal(semaphore);
}];
while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
beforeDate:[NSDate dateWithTimeIntervalSinceNow:10]];
dispatch_release(semaphore);
}
@holysin
Copy link

holysin commented Jan 15, 2013

hi, buddy, i meet the same problem, can you explain why u use the while(....){....}, but not use the simple dispatch_semaphore_wait(semaphore), thank you very much, I think others who use afnetworking and want get the notify from the success block wil be appreciated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment