Latest 1.0.0
License MIT
Platforms osx 10.9, ios 8.0, requires ARC

Build Status

Are you a dependency injection devotee? Let’s mock NSURLSession together.


  • No need to modify production code to mock NSURLSession
  • Customizable URL matching logic to mock responses
  • Testable that the mocked responses are surely called


CocoaPods (iOS 8+, OS X 10.9+)

You can use Cocoapods to install MockURLSession by adding it to your Podfile:

platform :ios, '8.0'

target 'MyAppTest' do
    pod 'MockURLSession'

Note that this requires CocoaPods version 36, and your iOS deployment target to be at least 8.0.


Quick glance

Let’s take a case to test MyApp below.

class MyApp {
    static let apiUrl = NSURL(string: "")!
    let session: NSURLSession
    var data: NSData?
    var error: NSError?
    init(session: NSURLSession = NSURLSession.sharedSession()) {
        self.session = session
    func doSomething() {
        session.dataTaskWithURL(MyApp.apiUrl) { (data, _, error) in
   = data
            self.error = error

In the test code,

import MockURLSession

and write testing by any flamewrorks you prefer sush as XCTest (Written by print here).

// Initialization
let session = MockURLSession()
// Or, use shared instance as `NSURLSession` provides

// Setup a mock response
let data = "Foo 123".dataUsingEncoding(NSUTF8StringEncoding)!
session.registerMockResponse(MyApp.apiUrl, data:data)

// Inject the session to the target app code and the response will be mocked like below
let app = MyApp(session: session)

print(NSString(!, encoding:NSUTF8StringEncoding)!)  // Foo 123
print(app.error)    // nil

// Make sure that the data task is resumed in the app code
print(session.resumedResponse(MyApp.apiUrl) != nil)  // true

URL matching customization

// Customize URL matching logic if you prefer
class Normalizer: MockURLSessionNormalizer {
    func normalizeUrl(url: NSURL) -> NSURL {
        // Fuzzy matching example
        let components = NSURLComponents() =
        components.path = url.path
        return components.URL!
// Note that you should setup the normalizer before registering mocked response
let data = NSKeyedArchiver.archivedDataWithRootObject(["username": "abc", "age": 20])
let session = MockURLSession()
session.normalizer = Normalizer()
session.registerMockResponse(MyApp.apiUrl, data:data)



Development tips

A long way to bump up spec version

  1. Xcode: MockURLSession > Identity > Version
  2. Pod: s.version in MockURLSession.podspec
  3. Git: git tag 1.0.0 && git push origin --tag
  4. Release by pod trunk push MockURLSession.podspec

Latest podspec

    "name": "MockURLSession",
    "version": "1.0.0",
    "summary": "MockURLSession provides a way to mock NSURLSession.",
    "homepage": "",
    "license": {
        "type": "MIT"
    "authors": {
        "Kenta Yamamoto": ""
    "source": {
        "git": "",
        "tag": "1.0.0"
    "source_files": "MockURLSession/*.swift",
    "requires_arc": true,
    "platforms": {
        "osx": "10.9",
        "ios": "8.0"

Pin It on Pinterest

Share This