AFNetworking多个http请求嵌套应该怎么处理
2个回答
展开全部
NSURL*url=[NSURLURLWithString:K_THINKERBELL_SERVER_URL];AFHTTPClient*httpClient=[[AFHTTPClientalloc]initWithBaseURL:url];Account*ac=[[Accountalloc]init];NSMutableURLRequest*request=[httpClientrequestWithMethod:@"GET"path:[NSStringstringWithFormat:@"/user/%@/event/%@",ac.uid,eventID]parameters:nil];AFHTTPRequestOperation*operation=[httpClientHTTPRequestOperationWithRequest:requestsuccess:^(AFHTTPRequestOperation*operation,idresponseObject){NSError*error=nil;NSDictionary*JSON=[NSJSONSerializationJSONObjectWithData:responseObjectoptions:NSJSONReadingAllowFragmentserror:&error];if(error){}[self.delegateNextMeetingFound:[[Meetingalloc]init]meetingData:JSON];}failure:^(AFHTTPRequestOperation*operation,NSError*error){}];[httpClientenqueueHTTPRequestOperation:operation];问题是我想要创建的单元测试基于此数据,但我不想测试究竟将该请求。我想要一个预定义的结构将作为响应返回。我是新的单元测试、和的有点戳了OCMock,但不能弄清楚如何管理这。解决方法1:需要关于您的问题发表评论的几件事。首先,您的代码很难测试,因为它直接创建AFHTTPClient。我不知道是否这是因为它只是一个示例,但您应注入它相反(见下面的示例)。第二,创建的请求,然后AFHTTPRequestOperation,然后你排队它。这很好,但你可以得到相同使用AFHTTPClient方法getPath:parameters:success:failure:。我并没有与该建议HTTPstubbing工具(Nocilla)的经验,但我看到它基于NSURLProtocol。我知道有些人使用这种方法,但我更喜欢创建我自己的存根的响应对象和嘲笑http客户端,如你们所见下面的代码中。检索器是我们想要测试我们哪里注入AFHTTPClient的类。请注意我通过直接的用户和事件id,因为我想要的东西,简单且易于测试。然后在其他的地方你会账号uid将值传递给此方法,所以头文件看上去类似这样:#import@classAFHTTPClient;@protocolRetrieverDelegate;@interfaceRetriever:NSObject-(id)initWithHTTPClient:(AFHTTPClient*)httpClient;@property(readonly,strong,nonatomic)AFHTTPClient*httpClient;@property(weak,nonatomic)iddelegate;-(void)retrieveEventWithUserId:(NSString*)userIdeventId:(NSString*)eventId;@end@protocolRetrieverDelegate-(void)retriever:(Retriever*)retrieverdidFindEvenData:(NSDictionary*)eventData;@end执行文件:#import"Retriever.h"#import@implementationRetriever-(id)initWithHTTPClient:(AFHTTPClient*)httpClient{NSParameterAssert(httpClient!=nil);self=[superinit];if(self){_httpClient=httpClient;}returnself;}-(void)retrieveEventWithUserId:(NSString*)userIdeventId:(NSString*)eventId{NSString*path=[NSStringstringWithFormat:@"/user/%@/event/%@",userId,eventId];[_httpClientgetPath:pathparameters:nilsuccess:^(AFHTTPRequestOperation*operation,idresponseObject){NSDictionary*eventData=[NSJSONSerializationJSONObjectWithData:responseObjectoptions:0error:NULL];if(eventData!=nil){[self.delegateretriever:selfdidFindEventData:eventData];}}failure:nil];}@end和测试:#import#import"Retriever.h"//Collaborators#import//Testsupport#import@interfaceRetrieverTests:XCTestCase@end@implementationRetrieverTests-(void)setUp{[supersetUp];//Putsetupcodehere;itwillberunonce,beforethefirsttestcase.}-(void)tearDown{//Putteardowncodehere;itwillberunonce,afterthelasttestcase.[supertearDown];}-(void)test__retrieveEventWithUserIdEventId__when_the_request_and_the_JSON_parsing_succeed__it_calls_didFindEventData{//CreatingthemocksandtheretrievercanbeplacedinthesetUpmethod.idmockHTTPClient=[OCMockObjectmockForClass:[AFHTTPClientclass]];Retriever*retriever=[[Retrieveralloc]initWithHTTPClient:mockHTTPClient];idmockDelegate=[OCMockObjectmockForProtocol:@protocol(RetrieverDelegate)];retriever.delegate=mockDelegate;[[mockHTTPClientexpect]getPath:@"/user/testUserId/event/testEventId"parameters:nilsuccess:[OCMArgcheckWithBlock:^BOOL(void(^successBlock)(AFHTTPRequestOperation*,id)){//Herewecapturethesuccessblockandexecuteitwithastubbedresponse.NSString*jsonString=@"{\"somevalidJSON\":\"somevalue\"}";NSData*responseObject=[jsonStringdataUsingEncoding:NSUTF8StringEncoding];[[mockDelegateexpect]retriever:retrieverdidFindEventData:@{@"somevalidJSON":@"somevalue"}];successBlock(nil,responseObject);[mockDelegateverify];returnYES;}]failure:OCMOCK_ANY];//Methodtotest[retrieverretrieveEventWithUserId:@"testUserId"eventId:@"testEventId"];[mockHTTPClientverify];}@end最后要评论的一点是AFNetworking2.0版本发布所以考虑使用它,是否它涵盖您的要求。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询