Skip to content

Instantly share code, notes, and snippets.

@daveshah
Created March 8, 2013 16:32
Show Gist options
  • Save daveshah/5117747 to your computer and use it in GitHub Desktop.
Save daveshah/5117747 to your computer and use it in GitHub Desktop.
Okay... so this is a REALLY ugly test but it gets the point of what I'm looking for across... (hopefully) I wanted to (as a sanity check) test the response of a web service. I'm using AFNetworking and the responses are by default asynchronous. To get around this in my tests I do a little something like this: I'm curious to know what others have …
-(void)testThatICanMakeAWebRequest
{
__block BOOL complete = NO;
__block NSString *response;
NSRunLoop *loop = [NSRunLoop currentRunLoop];
NSURL *url = [NSURL URLWithString:@"http://some.nifty.url.com"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc]initWithBaseURL:url];
[httpClient getPath:@"/myservice" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject){
response = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
complete = YES;
}failure:^(AFHTTPRequestOperation *operation, NSError *error){
STFail(@"The Request Failed!");
complete = YES;
}];
while(!complete && [loop runMode:NSDefaultRunLoopMode beforeDate: [NSDate distantFuture]]);
//Assert what I'm looking for in response
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment