I’m happy to introduce my next open-source project. Nocilla is a testing framework that will allow you to stub HTTP requests for testing your iOS and OS X apps. It has a nice DSL that will make your tests super easy to understand and maintain and it’s super easy to use.
Let say that your module will GET google.com and you want to cover your ass in case google.com returns a 404 (hey, nobody’s perfect):
stubRequest(@"GET", @"http://www.google.com").andReturn(404); NSAssert([google isDown] == true);
This is how it works: You need to write a piece of code that will handle communication via HTTP (or HTTPS, Nocilla works with both). You want to unit-test this code and handle all conceivable errors with the network. With Nocilla you can isolate your code from the real network. Nocilla replaces the dependency of the network in your code allowing you to have full control over which HTTP responses your code gets. This way you can set network scenarios that will be impossible to recreate with the real network. Nocilla is also way faster than the real network, speeding up your tests considerably.
The nicest part of Nocilla is its DSL, heavily inspired by WebMock. It’s a fluid interface in pure Objective-C (and yes, no square brackets). I’ll write a post on how I implemented the DSL because I’ve never seen this kind of API in Objective C and I think a lot of libraries will be able to take advantage of the powerfull technique I came up with.
The one limitation of the library is that it only works with NSURLConnection-based HTTP requests (AFNetworking, MKNetworkKit, NSURLConnection of course…). Nocilla has a pluggable architecture, so it’s super easy to create new hooks for other libraries like ASIHTTPRequest. In fact I started with ASIHTTPRequest and I shifted the work toward NSURLConnection. The hook for ASIHTTPRequest should not be too hard and it will be a “nice to have”, especially to help people migrate to other HTTP libraries (ASIHTTPRequest is no longer maintained).
Please note, Nocilla will block any HTTP request by design. If an HTTP request occurs while Nocilla is enabled and it’s not expected (not stubbed using the DSL) that request won’t hit the real network. After all, your unit tests should not hit the real network. An option will be available to enable real HTTP requests, but it’s not advised.
I hope you like it and use it. Contributions are also more than welcome.
Nocilla in Github | https://github.com/luisobo/Nocilla