Created
March 27, 2012 11:41
-
-
Save shawnwall/2215212 to your computer and use it in GitHub Desktop.
Unit testing blocks with AFNetworking example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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