我有一個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