Skip to content

Instantly share code, notes, and snippets.

Created March 27, 2012 11:41
What would you like to do?
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");
while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
beforeDate:[NSDate dateWithTimeIntervalSinceNow:10]];
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