ios – 如何单元测试AFNetworking请求

ios – 如何单元测试AFNetworking请求,第1张

概述我正在通过AFNetworking作为GET请求来检索 JSON数据,如下所示: NSURL *url = [NSURL URLWithString:K_THINKERBELL_SERVER_URL]; AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; Account *ac = [[Acc 我正在通过AFNetworking作为GET请求来检索 JSON数据,如下所示:
NSURL *url = [NSURL URLWithString:K_THINKERBELL_SERVER_URL];    AFhttpClIEnt *httpClIEnt = [[AFhttpClIEnt alloc] initWithBaseURL:url];    Account *ac = [[Account alloc]init];    NSMutableURLRequest *request = [httpClIEnt requestWithMethod:@"GET" path:[Nsstring stringWithFormat:@"/user/%@/event/%@",ac.uID,eventID]  parameters:nil];    AFhttpRequestoperation *operation = [httpClIEnt httpRequestoperationWithRequest:request                                                                            success:^(AFhttpRequestoperation *operation,ID responSEObject) {                                                                                NSError *error = nil;                                                                                NSDictionary *JsON = [NSJsONSerialization JsONObjectWithData:responSEObject options:NSJsONReadingallowFragments error:&error];                                                                                if (error) {                                                                                }                                                                                [self.delegate NextMeetingFound:[[Meeting alloc]init] meetingData:JsON];                                                                            }                                                                            failure:^(AFhttpRequestoperation *operation,NSError *error){                                                                            }];    [httpClIEnt enqueuehttpRequestoperation:operation];

事情是我想基于这个数据创建一个单元测试,但我不想让测试实际上会提出请求.我希望预定义的结构将作为响应返回.我是一个新的单元测试,并戳了一点Ocmock,但不知道如何管理这个.

解决方法 几个事情要评论你的问题.
首先,您的代码很难测试,因为它直接创建了AFhttpClIEnt.我不知道是不是因为它只是一个样本,但是你应该注入它(参见下面的示例).

其次,您正在创建请求,然后是AFhttpRequestoperation,然后将其排入队列.这很好,但您可以使用AFhttpClIEnt方法getPath获取相同的参数:参数:success:failure:.

我没有那个建议的http stubbing工具(Nocilla)的经验,但我看到它是基于NSURLProtocol.我知道有些人使用这种方法,但我更喜欢创建自己的stubbed响应对象,并模仿http客户端,就像在下面的代码中看到的那样.

RetrIEver是我们要测试我们注入AFhttpClIEnt的类别.
请注意,我直接传递用户和事件ID,因为我想保持简单易用的测试.然后在其他地方你可以将accout uID值传递给这个方法等等…
头文件看起来与此类似:

#import <Foundation/Foundation.h>@class AFhttpClIEnt;@protocol RetrIEverDelegate;@interface RetrIEver : NSObject- (ID)initWithhttpClIEnt:(AFhttpClIEnt *)httpClIEnt;@property (Readonly,strong,nonatomic) AFhttpClIEnt *httpClIEnt;@property (weak,nonatomic) ID<RetrIEverDelegate> delegate;- (voID) retrIEveEventWithUserID:(Nsstring *)userID eventID:(Nsstring *)eventID;@end@protocol RetrIEverDelegate <NSObject>- (voID) retrIEver:(RetrIEver *)retrIEver dIDFindEvendata:(NSDictionary *)eventData;@end

执行文件:

#import "RetrIEver.h"#import <AFNetworking/AFNetworking.h>@implementation RetrIEver- (ID)initWithhttpClIEnt:(AFhttpClIEnt *)httpClIEnt{    NSParameterassert(httpClIEnt != nil);    self = [super init];    if (self)    {        _httpClIEnt = httpClIEnt;    }    return self;}- (voID)retrIEveEventWithUserID:(Nsstring *)userID eventID:(Nsstring *)eventID{    Nsstring *path = [Nsstring stringWithFormat:@"/user/%@/event/%@",userID,eventID];    [_httpClIEnt getPath:path              parameters:nil                 success:^(AFhttpRequestoperation *operation,ID responSEObject)    {        NSDictionary *eventData = [NSJsONSerialization JsONObjectWithData:responSEObject options:0 error:NulL];        if (eventData != nil)        {            [self.delegate retrIEver:self dIDFindEventData:eventData];        }    }                 failure:nil];}@end

和测试:

#import <XCTest/XCTest.h>#import "RetrIEver.h"// Collaborators#import <AFNetworking/AFNetworking.h>// Test support#import <Ocmock/Ocmock.h>@interface RetrIEverTests : XCTestCase@end@implementation RetrIEverTests- (voID)setUp{    [super setUp];    // Put setup code here; it will be run once,before the first test case.}- (voID)tearDown{    // Put teardown code here; it will be run once,after the last test case.    [super tearDown];}- (voID) test__retrIEveEventWithUserIDEventID__when_the_request_and_the_JsON_parsing_succeed__it_calls_dIDFindEventData{    // Creating the mocks and the retrIEver can be placed in the setUp method.    ID mockhttpClIEnt = [OcmockObject mockForClass:[AFhttpClIEnt class]];    RetrIEver *retrIEver = [[RetrIEver alloc] initWithhttpClIEnt:mockhttpClIEnt];    ID mockDelegate = [OcmockObject mockForProtocol:@protocol(RetrIEverDelegate)];    retrIEver.delegate = mockDelegate;    [[mockhttpClIEnt expect] getPath:@"/user/testUserID/event/testEventID"                          parameters:nil                             success:[OCMArg checkWithBlock:^BOol(voID (^successBlock)(AFhttpRequestoperation *,ID))    {        // Here we capture the success block and execute it with a stubbed response.        Nsstring *JsonString = @"{\"some valID JsON\": \"some value\"}";        NSData *responSEObject = [JsonString dataUsingEnCoding:NSUTF8StringEnCoding];        [[mockDelegate expect] retrIEver:retrIEver dIDFindEventData:@{@"some valID JsON": @"some value"}];        successBlock(nil,responSEObject);        [mockDelegate verify];        return YES;    }]                             failure:Ocmock_ANY];    // Method to test    [retrIEver retrIEveEventWithUserID:@"testUserID" eventID:@"testEventID"];    [mockhttpClIEnt verify];}@end

最后要注意的是,AFNetworking 2.0版本被发布,所以考虑使用它,如果它涵盖了您的要求.

总结

以上是内存溢出为你收集整理的ios – 如何单元测试AFNetworking请求全部内容,希望文章能够帮你解决ios – 如何单元测试AFNetworking请求所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1096142.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存