* Nocilla编辑--我最初想用Nocilla测试,但最终使用 OHHTTPStubs 来完成这项工作。我使用OHHTTPStubs *回答了下面的原始问题
原题:
我想测试我们的应用程序的APIClient --需要测试的方法之一的基本原理如下所示。因此,我需要模拟来自AFNetworking的AFNetworking调用。Nocilla似乎是这样做的一个选项(比OCMock更合适)。我已经检查了github页面,它处理了Nocilla和AFNetworking,但是我不知道如何将它应用到我的问题上--语法不是很熟悉。
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;
}发布于 2013-10-21 14:30:32
最后,我使用另一个库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;
}https://stackoverflow.com/questions/19336476
复制相似问题