[aerogear-dev] iOS testing - HTTP mocking

Christos Vasilakis cvasilak at gmail.com
Fri Jun 7 04:32:02 EDT 2013


Hi team,

for further improvements of our unit tests we have switched the http mocking mechanism we use (our own NSURLProtocol impl)  to the popular OHHTTPStubs[1] project,  a library currently recommended by the AFNetworking networking lib we use.

The basic mechanism is straightforward to use and encapsulated in one method:

return [OHHTTPStubsResponse responseWithData:data
                                  statusCode:status
                                responseTime:responseTime
                                     headers:headers];

in which a stubbed response is returned to the client.

Now, based on this mechanism,  we have abstracted a bit and created methods such as:

 + (void)mockResponse:(NSData*)data;
 + (void)mockResponseStatus:(int)status;
 + (void)mockResponseTimeout:(NSData*)data status:(int)status responseTime:(NSTimeInterval)responseTime;

This gives the advantages that a) clearly indicate what http scenario we are testing  and b) remove params that don't make sense for the particular scenario under testing e.g. that is we simulate a status of  (404) but we need to pass all params eg. data, interval, timeout, etc.  But this doesn't limit us, we can do that if we want and use the full blown method with all the params attached.

I have created two branches in my fork, one that uses a blocks approach inside the testing class [2] and one that the functionality is extracted in a helper class that the testing classes can use [3].  The second approach was created because there was common code and didn't want to duplicate it over the testing classes.  

I would be interesting to know what is your comments on it?

Thanks,
Christos


[1] https://github.com/AliSoftware/OHHTTPStubs
[2] https://github.com/cvasilak/aerogear-ios/blob/ohhttpstubs/AeroGear-iOS/AeroGear-iOSTests/AGRestAdapterTests.m#L33-L51
[3] https://github.com/cvasilak/aerogear-ios/blob/ohhttpstubs.helper/AeroGear-iOS/AeroGear-iOSTests/utils/AGHTTPMockHelper.m
[4] 
 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/aerogear-dev/attachments/20130607/2a1604b8/attachment.html 


More information about the aerogear-dev mailing list