我有一个iOS应用程序正在工作,它连接到第三方Web服务。我有大约50个不同的调用,并且想用Kiwi编写单元测试,但是我不知道从哪里开始。如何使用AFNetworking进行单元测试api调用
由于我不负责API,我只需使用正确的GET或POST方法检查我的电话是否指向了正确的URL。
有什么办法可以正确地测试这个吗?
继承人我的电话的一个示例:
+ (void)listsForUser:(NSString *)username
response:(void (^)(NSArray *lists))completion
{
NSString *path = [NSString stringWithFormat:@"user/list.json/%@/%@", TRAKT_API_KEY, username];
[TNTraktAPIManager requestWithMethod:GET
path:path
parameters:nil
callback:^(id response) {
completion(response);
}];
}
如果设置助手类shouldEventually
期望和使用receive:(SEL)withArguments:(id)...
形式,然后调用下面的辅助方法,
+ (void)requestWithMethod:(HTTPMethod)method
path:(NSString *)path
parameters:(NSDictionary *)params
callback:(void (^)(id response))completion
{
NSString *methodString = @"POST";
if (method == GET) {
methodString = @"GET";
}
// Setup request
NSURLRequest *request = [[TraktAPIClient sharedClient] requestWithMethod:methodString
path:path
parameters:params];
// Setup operation
AFJSONRequestOperation *operation =
[AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request,
NSHTTPURLResponse *response,
id JSON) {
id jsonResults = JSON;
completion(jsonResults);
} failure:^(NSURLRequest *request,
NSHTTPURLResponse *response,
NSError *error,
id JSON) {
id jsonResults = JSON;
completion(jsonResults);
NSLog(@"%s: error: %@", __PRETTY_FUNCTION__, error);
}];
// TODO: Queue operations
[operation start];
}
关于使用Kiwi和AFImageDownloader的相关文章http://ashfurrow.com/blog/unit-testing-objective-c-using-kiwi – 2013-02-10 16:16:59