RecorderSession
An NSURLSession test framework for iOS and macOS inspired by DVR and VCR (that's where the cassette metaphor comes from…).
It's not as mature as DVR yet, but there are some advantages:
- cassettes are stored in a
Cassettes.bundlefolder and don't pollute your Xcode project file - customizable validation options for comparing a request against the recorded request, e.g. excluding the host component to allow multple base URLs
- works great with Swift, but doesn't depend on it
- Objective-C support was the primary motivation for creating this framework
Usage
To allow NSURLSession testing, please make sure your API client's session can be either switched out or added in via dependency injection in the initializer.
Your test target should contain a Cassettes.bundle folder where all the recorded cassettes are stored.
import RecorderSession
let bundle = Bundle(for: type(of: self)).cassetteBundle
// Switch your URLSession with RecorderSession.
// The original URLSession will be used for recording cassettes.
let recorderSession = RCNRecorderSession(backing: originalURLSession, cassetteBundle: bundle)
let client = MyAPIClient(session: recorderSession)
// Load a cassette to record or replay
recorderSession.insertCassette(name: "GetItems")
// Perform your network request
client.getItems { items, error in
XCTAssertNotNil(items)
XCTAssertNil(error)
expectation.fulfill()
}When you run the test for the first time, the cassette will be written to disk and the execution will halt. You need to move the cassette file from the printed location to your cassette bundle. All following test runs will use the cassette instead.
Installation via Carthage
Carthage is the preferred, but not the only way to add this framework to your project. Add the following line to your Cartfile.private and run carthage update:
github "newmarcel/RecorderSession"
Naming
- "Cassette": A JSON file representing an HTTP request, response and body.
- "recording a cassette": Making a live HTTP request and save the request, response and payload to disk.
- "playing a cassette": Simulating a live HTTP request, response and body with the saved JSON "cassette" from disk.