Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unit test api calls using AFNetworking

I have an iOS app I'm working on, which connects to a third-party web service. I have around 50 different calls, and want to write unit tests using Kiwi, but I have no idea where to start.

As I'm not responsible for the API, I need to just check that my calls are pointing to the correct URL, using the correct GET or POST method.

Is there any way to test this properly?

Heres an example of one of my calls:

+ (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);
                                }];
}

Which calls the following helper method

+ (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];

}
like image 723
squarefrog Avatar asked Jan 14 '23 06:01

squarefrog


1 Answers

If you set a shouldEventually expectation on the helper class and use the receive:(SEL)withArguments:(id)... form, then you can check that the argument received is what you'd expect it to be.

Two gotchas worth knowing about are setting the expectation before making the call; and using the shouldEventually form rather than should so that the test is delayed long enough for the call to be made.

like image 96
TimD Avatar answered Jan 24 '23 04:01

TimD