Latest 0.2.0
Homepage https://github.com/aarons22/SwiftVCR
License MIT
Platforms ios 10.0
Authors

Build Status
Version
Platform

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

Installation

VCR is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod 'VCR'

In order to save tapes, you will need to define VCR_DIR in your scheme. This should point to the directory where you want tapes to be stored. We normally use this:

Name Value
VCR_DIR $(SOURCE_ROOT)/$(PROJECT_NAME)Tests/VCRTapes

Usage

Recording

let session = VCRSession()
session.insertTape("example", record: true)

A json file called example.json will be placed into the VCR_DIR (defined above).

Mulitple requests

You can record multiple requests in given session by inserting additional tapes. VCRSession will write out a json file for each tape inserted.

For any additional requests made, it will fallback to the actual URLSession request.

Playback

let session = VCRSession()
session.insertTape("example")

This will read the file example.json from the VCR_DIR and return it in place of making the request.

Dependency Injection

VCRSession is a an instance of URLSession, so it can be injected into any class that uses it. For example, given an http client:

class HTTPClient {
    let session: URLSession

    init(session: URLSession = URLSession.shared) {
        self.session = session
    }

    func request(_ request: URLRequest, completion: @escaping (Bool) -> Void) {
        let task = self.session.dataTask(with: request) { (data, response, error) in
            completion(true)
        }

        task.resume()
    }
}

You can inject VCRSession in place of the default:

let session = VCRSession()
session.insertTape("example")
let client = HTTPClient(session: session)

Author

Aaron Sapp, [email protected]

License

VCR is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "VCR",
    "version": "0.2.0",
    "summary": "VCR is a lightweight tool to record and playback http requests for mocking in swift.",
    "description": "VCR is a lightweight tool to record and playback http requests for mocking in swift. nInspired by VCR for Rails, this tool allows you to record requests via URLSession.",
    "homepage": "https://github.com/aarons22/SwiftVCR",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Aaron Sapp": "[email protected]"
    },
    "source": {
        "git": "https://github.com/aarons22/SwiftVCR.git",
        "tag": "0.2.0"
    },
    "social_media_url": "https://twitter.com/aaronsapp",
    "swift_version": "5.0",
    "platforms": {
        "ios": "10.0"
    },
    "source_files": "Source/**/*"
}

Pin It on Pinterest

Share This