0

I am doing a test with AFHTTPClient to test the backend response.

__block id testedResponseObject = nil;
[client getPath:path parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    testedResponseObject = responseObject;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    testedResponseObject = nil;
}];
[client.operationQueue waitUntilAllOperationsAreFinished];

STAssertNotNil(testedResponseObject, @"");

The problem with this is that it waits for all operations to finish but it does not execute the success block because it gets scheduled to dispatch_get_main_queue(). Is there a way to tell dispatch_get_main_queue() to finish its blocks from the main queue?

david
  • 3,553
  • 4
  • 28
  • 39

1 Answers1

1

Instead of relying on completionBlock, you can access the responseData (or whatever property) directly:

NSURLRequest *request = [client requestWithMethod:@"GET" path:path parameters:nil];
AFHTTPRequestOperation *operation = [client HTTPRequestOperationWithRequest:request success:nil failure:nil];
[client enqueuHTTPRequestOperation:operation];
[client.operationQueue waitUntilAllOperationsAreFinished];

STAssertNotNil(operation.responseData, @"");
mattt
  • 19,544
  • 7
  • 73
  • 84
  • Thanks that works. (I am using AFTHTTPClient so i get the operation from [client.operationQueue lastObject]) – david Jun 03 '13 at 10:14