我正在使用AFHTTPClient进行测试以测试后端响应。
__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, @"");
这个问题是它等待所有操作完成但它没有执行成功块,因为它被调度到dispatch_get_main_queue()。有没有办法告诉dispatch_get_main_queue()从主队列中完成它的块?
答案 0 :(得分:1)
您可以直接访问completionBlock
(或任何属性),而不是依赖responseData
:
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, @"");