我想存根一个使用 Kiwi 将块作为参数的方法。这是带有代码的完整解释:
我有一个名为的类TestedClass
,它有一个testedMethod
依赖于NetworkClass
通过 AFNetworking 调用到服务器的类的方法,并通过块返回其响应。翻译成代码:
@interface TestedClass : NSObject
-(void)testMethod;
@end
-(void)testMethod
{
NetworkClass *networkClass = [[NetworkClass alloc] init];
[networkClass networkMethod:^(id result)
{
// code that I want to test according to the block given which I want to stub
...
}];
}
typedef void (^NetworkClassCallback)(id result);
@interface NetworkClass : NSObject
-(void)networkMethod:(NetworkClassCallback)handler;
@end
-(void) networkMethod:(NetworkClassCallback)handler
{
NSDictionary *params = @{@"param":@", @"value"};
NSString *requestURL = [NSString stringWithFormat:@"www.someserver.com"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURLURLWithString:requestURL]];
NSURLRequest *request = [httpClient requestWithMethod:@"GET" path:requestURL parameters:params];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
handler(responseObject);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
handler(nil);
}];
[operation start];
}
我如何使用 Kiwi 存根networkMethod
与块以进行单元测试testMethod
?
更新:找到如何在 Kiwi 中执行此操作,请参阅下面的答案。