首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何测试AFNetworking

如何测试AFNetworking
EN

Stack Overflow用户
提问于 2013-10-12 16:19:54
回答 1查看 3.1K关注 0票数 3

* Nocilla编辑--我最初想用Nocilla测试,但最终使用 OHHTTPStubs 来完成这项工作。我使用OHHTTPStubs *回答了下面的原始问题

原题:

我想测试我们的应用程序的APIClient --需要测试的方法之一的基本原理如下所示。因此,我需要模拟来自AFNetworkingAFNetworking调用。Nocilla似乎是这样做的一个选项(比OCMock更合适)。我已经检查了github页面,它处理了NocillaAFNetworking,但是我不知道如何将它应用到我的问题上--语法不是很熟悉。

  • 所以,我想知道是否有人能给我一个提示,说明我如何在这种情况下使用Nocilla
  • 另外,必须将KiwiNocilla结合使用吗?

(预先谢谢:)

代码语言:javascript
复制
    -(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;

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-21 14:30:32

最后,我使用另一个库OHHTTPStubs解决了这个问题。您可以通过AFNetworking轻松地返回模拟对象,删除某些请求&更改响应/请求时间。这是一个很好的文档这里。这里还有github页面。存根被移除如下:

代码语言:javascript
复制
[OHHTTPStubs removeStub:stub];

下面是一些示例代码:

代码语言:javascript
复制
// 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;

}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19336476

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档