Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.
require 'test/unit'
require 'vcr'
VCR.config do |c|
c.cassette_library_dir = 'fixtures/vcr_cassettes'
c.stub_with :webmock # or :fakeweb
end
class VCRTest < Test::Unit::TestCase
def test_example_dot_com
VCR.use_cassette('synopsis', :record => :new_episodes) do
response = Net::HTTP.get_response(URI.parse('http://example.com/'))
assert_match /You have reached this web page by typing.*example\.com/, 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 example.com 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 example.com goes down for
maintenance) and accurate (the response from example.com will contain the same headers and body you get from a real request).
- Automatically records and replays your HTTP interactions with minimal setup/configuration code.
- Supports multiple HTTP stubbing libraries. Currently FakeWeb, WebMock, Typhoeus and Faraday are supported.
- Supports multiple HTTP libraries:
- Patron (WebMock)
- Curb (WebMock -- only supports Curb::Easy at the moment)
- HTTPClient (WebMock)
- em-http-request (WebMock)
- Net::HTTP (FakeWeb and WebMock)
- Typhoeus (Typhoeus::Hydra, but not Typhoeus::Easy or Typhoeus::Multi)
- 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.
- The same request can receive different responses in different tests--just use different cassettes.
- The recorded requests and responses are stored on disk as YAML 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.
- Known to work well with many popular ruby libraries including RSpec 1 & 2, Cucumber, Test::Unit, Capybara, Mechanize, Rest-Client and HTTParty.
- Extensively tested on 7 different ruby interpretters.
- Includes Rack and Faraday middleware.
Browse the cucumber features or visit the wiki for usage info and documentation.
The VCR talk given at Philly.rb also contains good usage info.
- 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.
If you find VCR useful, please recommend me on working with rails.
- 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.
Thanks also to the following people who have contributed patches or helpful suggestions:
Copyright (c) 2010 Myron Marston. See LICENSE for details.