>*Edit - 我最初想使用 Nocilla
测试AFNetworking
,但最终使用 OHHTTPStubs
来完成这项工作。我已经回答了下面的原始问题,使用 OHHTTPStubs
*
原始问题:
我想测试我们应用程序的 APIClient - 下面详细介绍了需要测试的方法之一的基本内容。所以我需要模仿AFNetworking
的HTTPRequestOperationWithRequest:
电话. Nocilla
似乎是执行此操作的一种选择(比OCMock
更合适)。我已经查看了处理Nocilla
和AFNetworking
的 github 页面,但我不确定如何将其应用于我的问题 - 语法不是很熟悉。
- 所以我想知道是否有人可以给我一个提示,在这种情况下我如何使用
Nocilla
? - 另外,必须将
Kiwi
与Nocilla
一起使用吗?
提前致谢:)
-(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
轻松返回模拟对象,存根某些请求并改变响应/请求时间。这里有很好的记录。这里也是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;
}