Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/venmo/DVR
Network testing for Swift
https://github.com/venmo/DVR
Last synced: 3 months ago
JSON representation
Network testing for Swift
- Host: GitHub
- URL: https://github.com/venmo/DVR
- Owner: venmo
- License: mit
- Created: 2015-06-19T01:43:16.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2024-03-26T22:02:33.000Z (7 months ago)
- Last Synced: 2024-05-29T12:03:16.718Z (5 months ago)
- Language: Swift
- Homepage: http://blog.soff.es/network-testing-in-swift-with-dvr
- Size: 199 KB
- Stars: 650
- Watchers: 36
- Forks: 86
- Open Issues: 20
-
Metadata Files:
- Readme: Readme.markdown
- License: LICENSE
Awesome Lists containing this project
- awesome-ios - DVR - Network testing for Swift. (Testing / Other Testing)
- awesome-swift - DVR - A simple network testing framework. (Libs / Testing)
- awesome-swift - DVR - A simple network testing framework. (Libs / Testing)
- awesome-ios-star - DVR - Network testing for Swift. (Testing / Other Testing)
- fucking-awesome-swift - DVR - A simple network testing framework. (Libs / Testing)
- awesome-swift-cn - DVR - A simple network testing framework for Swift. (Libs / Testing)
- awesome-swift - DVR - Network testing for Swift ` 📝 17 days ago ` (Testing [🔝](#readme))
README
# DVR
[![Version](https://img.shields.io/github/release/venmo/DVR.svg)](https://github.com/venmo/DVR/releases)
![Status](https://travis-ci.org/venmo/DVR.svg?branch=master)
![Swift Version](https://img.shields.io/badge/swift-4.2-orange.svg)
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
[![Swift Package Manager](https://img.shields.io/badge/SPM-compatible-brightgreen.svg)](https://swift.org/package-manager/)DVR is a simple Swift framework for making fake `NSURLSession` requests for iOS,
watchOS, and OS X based on [VCR](https://github.com/vcr/vcr).Easy [dependency injection](https://en.wikipedia.org/wiki/Dependency_injection) is the main design goal. The API is the same as `NSURLSession`. `DVR.Session` is a subclass of `NSURLSession` so you can use it as a drop in replacement anywhere. (Currently only data tasks are supported.)
## Usage
```swift
let session = Session(cassetteName: "example")
let task = session.dataTask(with: request) { data, response, error in
// Do something with the response
}// Nothing happens until you call `resume` as you'd expect.
task.resume()
```This will playback the `example` cassette. The completion handler exactly the same way it normally would. In this example, DVR will look for a cassette named `example.json` in your test bundle.
If the recording of the request is missing, it will record and save it to disk. After saving to disk, it will assert with path of the recorded file. This causes the tests to stop so you can add the cassette to your test target and rerun your tests.
### Recording Multiple Requests
By default, a DVR session only records one request. You can record multiple requests in the same cassette if you tell DVR when to start and stop recording.
``` swift
let session = Session(cassetteName: "multiple")// Begin recording multiple requests
session.beginRecording()session.dataTask(with: URLRequest(url: URL(string: "http://apple.com")!)) { data, response, error in
// Do something with the responsesession.dataTask(with: URLRequest(url: URL(string: "http://google.com")!)) { data, response, error in
// Do something with the response
}.resume()// Finish recording multiple requests
session.endRecording() {
// All requests have completed
}
}.resume()
```If you don't call `beginRecording` and `endRecording`, DVR will call these for your around the first request you make to a session. You can call `endRecording` immediately after you've submitted all of your requests to the session. The optional completion block that `endRecording` accepts will be called when all requests have finished. This is a good spot to fulfill XCTest expectations you've setup or do whatever else now that networking has finished.