Latest 1.0.0
Homepage https://github.com/jedlewison/Interceptor
License MIT
Platforms ios 8.0, requires ARC

Interceptor is mini-framework for mocking network responses by intercepting NSURLSessionDataTasks and responding with mock data. It lets you run unit tests with mock network responses without injecting mock services or changing any code in the host app.

Usage:

  1. Add Interceptor to your test target
  2. Before each test case, set the InterceptorSession.sharedInstance.dataSource to an object that can respond with appriate mock responses.

For example, here’s how you might set up a test using Quick & Nimble:

class TestAppSpec: QuickSpec {
    override func spec() {
        var networkModel = NetworkModel()

        context("NSURLSession") {
            beforeEach {
                MockURLSession.sharedInstance.dataSource = self
                networkModel.startURL(nordstromURL)
            }
            it("the contents of the data should be the string 'nordstrom response'") {
                expect(networkModel.requestResult).toEventually(equal("nordstrom response"))
            }
        }

And here’s the dataSource implementation:

extension TestAppSpec: InterceptorResponding {
    func finalizeMockResponseValues(initialValues: MockResponseValues, forRequest request: NSURLRequest) {
        switch initialValues.URL {
        case nordstromURL:
            initialValues.setData(withString: "nordstrom response")
        case uberURL:
            initialValues.setData(withString: "uber response")
        case bestBuyURL:
            initialValues.setData(withString: "best buy response")
        default:
            break
        }
    }
}

You set the mock response on the MockResponseValues object passed to the dataSource. It’s initial values reflect the URL of the request and a 200 status code, but empty data. You can set data directly, as data, or by using one of its convenience functions.

After you set the response, it is delivered to the NSURLSessionDataTask as an NSHTTPURLResponse along with a NSData object. From the app’s perspective, it was a real network request — the only code changes required are in the test suite, no injection necessary.

More

Interceptor works in both Swift and Objective-C. If for some reason you need to be able to perform real network requests during your tests (not a good idea!), you can turn the InterceptorSession.sharedInstance.active to false.

Warning

Please make sure to never, ever add Interceptor to your app target. It works by swizzling NSURLSession and abusing the NSURLCache, which is fine for unit tests when you really don’t want to be making actual network calls, but is an absolutely terrible idea for an actual app.

Latest podspec

{
    "name": "Interceptor",
    "version": "1.0.0",
    "summary": "Intercept NSURLSessionDataTask requests and respond with mock data",
    "description": "A mini-framework for mocking network responses without injecting mock servicen                            by intercepting NSURLSessionDataTasks and responding with mock data.",
    "authors": "Jed Lewison",
    "homepage": "https://github.com/jedlewison/Interceptor",
    "license": "MIT",
    "source": {
        "git": "https://github.com/jedlewison/Interceptor.git",
        "tag": "1.0.0"
    },
    "platforms": {
        "ios": "8.0"
    },
    "requires_arc": true,
    "source_files": "Interceptor/*"
}

Pin It on Pinterest

Share This