Ios 如何使用AFNetworking对api调用进行单元测试
我正在开发一个iOS应用程序,它连接到第三方web服务。我有大约50个不同的调用,我想用Kiwi编写单元测试,但我不知道从哪里开始 因为我不负责API,所以我只需要使用正确的GET或POST方法检查我的调用是否指向正确的URL 有什么方法可以正确地测试这一点吗 以下是我的一个电话示例:Ios 如何使用AFNetworking对api调用进行单元测试,ios,afnetworking,kiwi,Ios,Afnetworking,Kiwi,我正在开发一个iOS应用程序,它连接到第三方web服务。我有大约50个不同的调用,我想用Kiwi编写单元测试,但我不知道从哪里开始 因为我不负责API,所以我只需要使用正确的GET或POST方法检查我的调用是否指向正确的URL 有什么方法可以正确地测试这一点吗 以下是我的一个电话示例: + (void)listsForUser:(NSString *)username response:(void (^)(NSArray *lists))completion {
+ (void)listsForUser:(NSString *)username
response:(void (^)(NSArray *lists))completion
{
NSString *path = [NSString stringWithFormat:@"user/list.json/%@/%@", TRAKT_API_KEY, username];
[TNTraktAPIManager requestWithMethod:GET
path:path
parameters:nil
callback:^(id response) {
completion(response);
}];
}
它调用以下帮助器方法
+ (void)requestWithMethod:(HTTPMethod)method
path:(NSString *)path
parameters:(NSDictionary *)params
callback:(void (^)(id response))completion
{
NSString *methodString = @"POST";
if (method == GET) {
methodString = @"GET";
}
// Setup request
NSURLRequest *request = [[TraktAPIClient sharedClient] requestWithMethod:methodString
path:path
parameters:params];
// Setup operation
AFJSONRequestOperation *operation =
[AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request,
NSHTTPURLResponse *response,
id JSON) {
id jsonResults = JSON;
completion(jsonResults);
} failure:^(NSURLRequest *request,
NSHTTPURLResponse *response,
NSError *error,
id JSON) {
id jsonResults = JSON;
completion(jsonResults);
NSLog(@"%s: error: %@", __PRETTY_FUNCTION__, error);
}];
// TODO: Queue operations
[operation start];
}
如果您在helper类上设置了一个
shouldnually
期望值,并使用receive:(SEL)with arguments:(id).
表单,那么您可以检查收到的参数是否是您期望的参数
两个值得了解的问题是在打电话之前设定期望值;使用
应该最终表单,而不是应该,以便测试延迟足够长的时间以进行调用。我强烈建议检查API类的单元测试
单元测试应该是确定性的,在混合中添加一个潜在的不可预测的API会使测试条件不确定性
Ohttpsubs将允许您存根对传出HTTP请求的响应。基本上,它会拦截您的HTTP流量,如果请求符合您设置的条件,它会给出一个您用JSON声明的固定响应(而不是来自API的不可预测的结果)。这允许您在测试类中配置不同的响应场景:即404错误、部分响应数据等。。在测试中使用
下面是一个例子:
我创建了这个JSON存根并保存为JSON文件:
{
"devices" : [
{
"alarm" : {
"alarm_id" : 1,
"attack_time" : "<null>",
"defined_time" : "2014-04-14T04:21:36.000Z",
"device_id" : 7,
"is_protected" : 0
},
"device_type" : "iPhone",
"id" : 7,
"os_version" : "7.1"
}
],
"email" : "mystubemail@gmail.com",
"facebook_id" : 5551212,
"id" : 3,
"name" : "Daffy Duck"
}
我不确定Kiwi是否允许异步测试,但如果不允许,我也建议研究Specta和匹配的框架Expecta。它们允许超简单的异步单元测试,当与OHttpStubs结合时,它提供了单元测试API调用所需的一切 关于使用猕猴桃和AFImageDownloader的相关帖子很好了解。我在设定期望值!这是一个容易犯的错误。如果每次我都有$currencyUnit$的话。。。!
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
BOOL stringFound;
if ([[request.URL description] rangeOfString:[NSString stringWithFormat:@"%@devices/register?", BASEURL]].location != NSNotFound)
{
stringFound = YES;
}
NSLog(@"%d", stringFound);
return stringFound;
} withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *request) {
// Stub it with our "RegisterDevice.json" stub file
return [[OHHTTPStubsResponse responseWithFileAtPath:OHPathForFileInBundle(@"RegisterDevice.json",bundle)
statusCode:200 headers:@{@"Content-Type":@"text/json"}] requestTime:1.0 responseTime:1.0];
}];