Last active
December 17, 2015 09:29
-
-
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
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
/* | |
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