VCR
Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.
Synopsis
require 'rubygems'
require 'test/unit'
require 'vcr'
VCR.configure do |c|
c.cassette_library_dir = 'fixtures/vcr_cassettes'
c.hook_into :webmock # or :fakeweb
end
class VCRTest < Test::Unit::TestCase
def test_example_dot_com
VCR.use_cassette('synopsis') do
response = Net::HTTP.get_response(URI('http://www.iana.org/domains/example/'))
assert_match /Example Domains/, response.body
end
end
end
Run this test once, and VCR will record the http request to fixtures/vcr_cassettes/synopsis.yml
. Run it again, and VCR
will replay the response from iana.org when the http request is made. This test is now fast (no real HTTP requests are
made anymore), deterministic (the test will continue to pass, even if you are offline, or iana.org goes down for
maintenance) and accurate (the response will contain the same headers and body you get from a real request).
Features
- Automatically records and replays your HTTP interactions with minimal setup/configuration code.
- Supports and works with the HTTP stubbing facilities of multiple libraries. Currently, the following are supported:
- Supports multiple HTTP libraries:
- Patron (when using WebMock)
- Curb (when using WebMock -- only supports Curl::Easy at the moment)
- HTTPClient (when using WebMock)
- em-http-request (when using WebMock)
- Net::HTTP (when using FakeWeb and WebMock)
- Typhoeus (Typhoeus::Hydra, but not Typhoeus::Easy or Typhoeus::Multi)
- Excon
- Faraday
- And of course any library built on Net::HTTP, such as Mechanize, HTTParty or Rest Client.
- Request matching is configurable based on HTTP method, URI, host, path, body and headers, or you can easily implement a custom request matcher to handle any need.
- The same request can receive different responses in different tests--just use different cassettes.
- The recorded requests and responses are stored on disk in a serialization format of your choice (currently YAML and JSON are built in, and you can easily implement your own custom serializer) and can easily be inspected and edited.
- Dynamic responses are supported using ERB.
- Automatically re-records cassettes on a configurable regular interval to keep them fresh and current.
- Disables all HTTP requests that you don't explicitly allow.
- Simple cucumber integration is provided using tags.
- Includes convenient RSpec macro and integration with RSpec 2 metadata.
- Known to work well with many popular ruby libraries including RSpec 1 & 2, Cucumber, Test::Unit, Capybara, Mechanize, Rest-Client and HTTParty.
- Includes Rack and Faraday middleware.
Usage
Browse the documentation for usage info.
The VCR talk given at Philly.rb also contains good usage info.
Release Policy
VCR follows the principles of semantic versioning. The cucumber features define VCR's public API. Patch level releases contain only bug fixes. Minor releases contain backward-compatible new features. Major new releases contain backwards-incompatible changes to the public API.
Ruby Interpreter Compatibility
VCR has been tested on the following ruby interpreters:
- MRI 1.8.7
- MRI 1.9.2
- REE 1.8.7
- JRuby
- Rubinius
Development
- Source hosted on GitHub.
- Direct questions and discussions to the mailing list.
- Report issues on GitHub Issues.
- Pull requests are very welcome! Please include spec and/or feature coverage for every patch, and create a topic branch for every separate change you make.
- See the Contributing guide for instructions on running the specs and features.
- Documentation is generated with YARD (cheat sheet). To generate while developing:
yard server --reload
If you find VCR useful, please recommend me on working with rails.
Thanks
- Aslak Hellesøy for Cucumber.
- Bartosz Blimke for WebMock.
- Chris Kampmeier for FakeWeb.
- Chris Young for NetRecorder, the inspiration for VCR.
- David Balatero for help with Typhoeus support.
- Wesley Beary for help with Excon support.
Thanks also to the following people who have contributed patches or helpful suggestions:
- Aaron Brethorst
- Avdi Grimm
- Bartosz Blimke
- Benjamin Oakes
- Ben Hutton
- Bradley Isotope
- Carlos Kirkconnell
- Eric Allam
- Flaviu Simihaian
- Justin Smestad
- Karl Baum
- Michael Lavrisha
- Nathaniel Bibler
- Oliver Searle-Barnes
- Paco Guzmán
- Ryan Bates
- Sathya Sekaran
- Wesley Beary
Similar Libraries
- Betamax (Groovy)
- Ephemeral Response (Ruby)
- Mimic (PHP/Kohana)
- Net::HTTP Spy (Ruby)
- NetRecorder (Ruby)
- Stale Fish (Ruby)
- WebFixtures (Ruby)
Copyright
Copyright (c) 2010-2011 Myron Marston. See LICENSE for details.