gpt4 book ai didi

ios - 如何测试 AFNetworking

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:47:29 26 4
gpt4 key购买 nike

*编辑 - 我最初想使用 Nocilla 测试 AFNetworking,但最终使用 OHHTTPStubs 来完成这项工作。我已经使用 OHHTTPStubs *

回答了下面的原始问题

原始问题:

我想测试我们应用程序的 APIClient - 下面详细介绍了需要测试的方法之一的基本原理。因此,我需要模仿 AFNetworking 中的 HTTPRequestOperationWithRequest: 调用。 Nocilla 似乎是执行此操作的一个选项(比 OCMock 更合适)。我已经查看了 the github page它处理 NocillaAFNetworking 但我不确定如何将它应用于我的问题 - 语法不是很熟悉。

  • 所以我想知道是否有人可以给我一些提示,告诉我在这种情况下如何使用 Nocilla
  • 此外,必须将 KiwiNocilla 一起使用吗?

提前致谢:)

    -(AFHTTPRequestOperation *)getBroadcastsForChannel:(TINChannel *)channel
startTime:(NSDate *)date
limit:(NSNumber *)limit
pastLimit:(NSNumber *)pastLimit
fields:(NSArray *)fieldsArray
interval:(TINBroadcastsInterval)interval
completionBlock:(void (^)(NSArray *broadcasts, NSError *error))block {

// Some setup here

NSURLRequest *request = [self requestWithMethod:@"GET" path:path parameters:params];

AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {

NSError *error;

NSDictionary *responseDic = [self parseResponse:responseObject error:&error];

if (error) {
if (block) {
block([NSArray array], error);
}
return;
}

// Parse the response object here

if (block) {
block([NSArray arrayWithArray:broadcastsOutput], nil);
}

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

if (block) {
block([NSArray array], error);
}
}];

[self enqueueHTTPRequestOperation:operation];

return operation;

}

最佳答案

我最终使用另一个库 OHHTTPStubs 解决了这个问题。可以通过 AFNetworking 轻松返回模拟对象,排除某些请求并改变响应/请求时间。有据可查here .这里还有 github page .像这样删除 stub :

[OHHTTPStubs removeStub:stub];

下面是一些示例代码:

// A mockJSON file is loaded here
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"sampleJSON18.json"];
NSData* data = [NSData dataWithContentsOfFile:filePath];
NSError* error = nil;
id mockJSON = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

//*** stub out AFNetworkingRequestOperation and return custom NSDictionary "mockJSON", in order to see how it is handled
id<OHHTTPStubsDescriptor> stub = nil;
stub = [OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return [[request.URL path]isEqualToString:@"/v1/epg/packages/59/broadcasts"]; // this means that any request which is equal to the above string is stubbed, return YES to stub *all* requests
} withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *request) {
// The `stubbing` procedure occurs in this block.
return [[OHHTTPStubsResponse responseWithJSONObject:mockJSON statusCode:200 headers:nil] requestTime:1.0f responseTime:5.0f]; // one can vary the request/responseTime here
}];

stub.name = @"getChannelsWithBroadcastsForDay";

AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
// The response object is now the mockJSON object, i.e. the original request is stubbed out

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// handle failure
}

}];

[self enqueueHTTPRequestOperation:operation];
return operation;

}

关于ios - 如何测试 AFNetworking,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19336476/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com