bsorrentino
5/2/2014 - 5:22 PM

IOS unit test: Make a REST call and wait for result

IOS unit test: Make a REST call and wait for result

- (void)testMBRequest
{
    NSURL *url = ....;
    
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    MBJSONRequest *jsonRequest = [[MBJSONRequest alloc] init];

    __block BOOL loaded = NO;
    
    [jsonRequest performJSONRequest:urlRequest completionHandler:^(id responseJSON, NSError *error) {
        
        XCTAssertNil(error, @"error on request: %@", error);
        
        NSArray *posts = responseJSON[@"posts"];
        
        XCTAssertNotNil(posts, @"posts not found!");
        
        loaded = YES;
    }];
    
    NSRunLoop *theRL = [NSRunLoop currentRunLoop];
    while (!loaded && [theRL runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]);
}