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) {
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.