Skip to content

Instantly share code, notes, and snippets.

@luketheobscure
Last active December 17, 2015 09:29
Show Gist options
  • Save luketheobscure/5587516 to your computer and use it in GitHub Desktop.
Save luketheobscure/5587516 to your computer and use it in GitHub Desktop.
Unit testing AFNetworking blocks can be tricky since SenTestingKit won't wait for the completion blocks to finish. Here's how I test my functions that deal with networking. Slightly adapted from https://gist.github.com/andrewroycarter/2254570
/*
Testing the af.updateFeatureFlags method, which returns an AFJSONRequestOperation operation
*/
- (void) testFeatureFlags{
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
[af.sharedHTTPClient enqueueBatchOfHTTPRequestOperations:@[af.updateFeatureFlags] progressBlock:nil completionBlock:^(NSArray *operations) {
dispatch_semaphore_signal(semaphore);
}];
while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW)) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:10]];
}
STAssertFalse([af.featureFlags[@"fake_flag"] boolValue], @"Fake flag should be false");
STAssertTrue([af.featureFlags[@"control_flag"] boolValue], @"Control flag should be true");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment