Home About Github Twitter

Addendum to UI Testing with Stubbed Network Data

This post will only be helpful to you if you intend to use Joe Masilotti’s strategy for stubbing network data in your UI Tests.

You will run into a problem when you try to set a key of app.launchEnvironment to a string with an equals sign in it, like this one:


It is truncated at the first equals sign. Set a break point in your app code after starting your UI test and run:

po NSProcessInfo.processInfo().environment

In the debugger to see (among a bunch of other key value pairs):


This means that your fake .resume() method won’t get any json.


Don’t put any equals signs in an app.launchEnvironment key:

let googleApiUrl = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=42.3535418,-71.0613433&destinations=42.3529193,-71.0577234&mode=walking".stringByReplacingOccurrencesOfString("=", withString: "EQUALS")

app.launchEnvironment[googleApiUrl] = "my_json_stuffs"

And in your SeededDataTask class:

import Foundation

class SeededDataTask: NSURLSessionDataTask {
    private let url: NSURL
    private let completion: DataCompletion

    init(url: NSURL, completion: DataCompletion) {
        self.url = url
        self.completion = completion

    override func resume() {
        let allowedURL = url.absoluteString.stringByReplacingOccurrencesOfString("=", withString: "EQUALS")

        if let json = NSProcessInfo.processInfo().environment[allowedURL] {
            let response = NSHTTPURLResponse(URL: url, statusCode: 200, HTTPVersion: nil, headerFields: nil)
            let data = json.dataUsingEncoding(NSUTF8StringEncoding)
            completion(data, response, nil)