3

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

原始问题:

我想测试我们应用程序的 APIClient - 下面详细介绍了需要测试的方法之一。所以我需要模仿HTTPRequestOperationWithRequest:来自AFNetworking. Nocilla似乎是这样做的一种选择(比 更合适OCMock)。我已经查看了处理的github 页面,但我不确定如何将其应用于我的问题 - 语法不是很熟悉。NocillaAFNetworking

  • 所以我想知道是否有人可以提示我Nocilla在这种情况下如何使用?
  • 另外,必须使用KiwiwithNocilla吗?

提前致谢 :)

    -(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;

}
4

1 回答 1

4

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

[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;

}
于 2013-10-21T14:30:32.077 回答