How to unit test api calls using AFNetworking

I have an iOS app I'm working on that connects to a third-party web service. I have about 50 different calls, and I want to write unit tests with Kiwi, but I don't know where to start.

Since I am not responsible for the API, I just need to verify that my calls point to the correct URL using the correct GET or POST method.

Is there any way to verify this correctly?

Here is 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); }]; } 

What causes 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]; } 
+4
source share
3 answers

If you set the shouldEventually in the helper class and use the receive:(SEL)withArguments:(id)... form, you can verify that the received argument is what you expect from it.

Two questions worth knowing set your expectation before making a call; and using the shouldEventually form, and should not, so that the test is delayed long enough for the call to be completed.

+5
source

I highly recommend checking out OHHTTPStubs for unit testing your API classes.

Unit tests must be deterministic and add the Internet to a potentially unpredictable API in the mix, which makes the test conditions non-deterministic.

OHTTPStubs allows you to disable response to outgoing HTTP requests. Basically, it intercepts your HTTP traffic, and if the request meets the criteria you set, it gives a complete response, which you declare in JSON (and not an unpredictable result from the API). This allows you to configure various response scripts in test classes: i.e. 404, partial answers, etc. For use in your tests.

Here is an example:

I created this JSON Stub and saved it as a JSON file:

 { "devices" : [ { "alarm" : { "alarm_id" : 1, "attack_time" : "<null>", "defined_time" : "2014-04-14T04:21:36.000Z", "device_id" : 7, "is_protected" : 0 }, "device_type" : "iPhone", "id" : 7, "os_version" : "7.1" } ], "email" : " mystubemail@gmail.com ", "facebook_id" : 5551212, "id" : 3, "name" : "Daffy Duck" } 

Whenever I request network requests in an API call, this JSON is returned because of this OHHTTPStub, which is declared in the test class to run before all tests.

 [OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) { BOOL stringFound; if ([[request.URL description] rangeOfString:[NSString stringWithFormat:@"%@devices/register?", BASEURL]].location != NSNotFound) { stringFound = YES; } NSLog(@"%d", stringFound); return stringFound; } withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *request) { // Stub it with our "RegisterDevice.json" stub file return [[OHHTTPStubsResponse responseWithFileAtPath:OHPathForFileInBundle(@"RegisterDevice.json",bundle) statusCode:200 headers:@{@"Content-Type":@"text/json"}] requestTime:1.0 responseTime:1.0]; }]; 

I'm not sure if Kiwi allows asynchronous testing, but if not, I also recommend looking at Specta and the corresponding Expecta structure. They allow super lightweight asynchronous unit testing, which in combination with OHHTTPStubs provides everything you need for unit test API calls.

+4
source

I would recommend Nocilla

Check out this other answer to a similar question for more information.

+3
source

All Articles