Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.
Pull request Compare This branch is 2 commits ahead, 1788 commits behind vcr:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.


gem install vcr

You'll also need either FakeWeb or WebMock:

gem install fakeweb


gem install webmock


require 'test/unit'
require 'vcr'

VCR.config do |c|
  c.cassette_library_dir = 'fixtures/vcr_cassettes'
  c.http_stubbing_library = :fakeweb # or :webmock

class VCRTest < Test::Unit::TestCase
  def test_example_dot_com
    response = VCR.use_cassette('synopsis', :record => :new_episodes) do
    assert_match /You have reached this web page by typing.*example\.com/, response.body

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 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 goes down for maintenance) and accurate (the response from 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 and WebMock are supported, and the adapter interface is easy to implement for any future libraries.
  • Supports multiple HTTP libraries: Net::HTTP (all HTTP stubbing libraries), Patron (WebMock only), HTTPClient (WebMock only) and em-http-request (WebMock only).
  • 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.
  • Disables all HTTP requests that you don't explicitly allow.
  • Simple cucumber integration is provided using tags.


Cassettes are the medium to which VCR records HTTP interactions, and the medium from which it replays them. While a cassette is in use, new HTTP requests (or "new episodes", as VCR calls them) either get recorded, or an error will be raised, depending on the cassette's :record mode (see below). When you use a cassette that contains previously recorded HTTP interactions, it registers them with the http stubbing library of your choice (fakeweb or webmock) so that HTTP requests get the recorded response. Between test runs, cassettes are stored on disk as YAML files in your configured cassette library directory.

Each cassette can be configured with a couple options:

  • :record: Specifies a record mode for this cassette.
  • :erb: Used for dynamic cassettes (see below for more details).

Record modes

VCR supports 3 record modes. You can set a default record mode in your configuration (see below) and a per-cassette record mode when inserting a cassette. The record modes are:

  • :new_episodes: Previously recorded HTTP interactions will be replayed. New HTTP interactions will be recorded.
  • :all: Previously recorded HTTP interactions will be ignored. All HTTP interactions will be recorded.
  • :none: Previously recorded HTTP interactions will be replayed. New HTTP interactions will result in an error.


require 'vcr'

VCR.config do |c|
  c.cassette_library_dir     = File.join(Rails.root, 'features', 'fixtures', 'cassette_library')
  c.http_stubbing_library    = :fakeweb
  c.ignore_localhost         = true
  c.default_cassette_options = { :record => :none }

This can go pretty much wherever, as long as this code is run before your tests, specs or scenarios. I tend to put it in spec/support/vcr.rb, test/support/vcr.rb or features/support/vcr.rb. You can set the following configuration options:

  • cassette_library_dir: VCR will save the cassette YAML files to this directory.
  • http_stubbing_library: Which http stubbing library to use. Currently :fakeweb and :webmock are supported. This is currently optional--VCR will try to guess based on the presence or absence of the FakeWeb or WebMock constants, but this is mostly done to assist users upgrading from VCR 0.3.1, which only worked with fakeweb and didn't have this option. I recommend you explicitly configure this.
  • ignore_localhost: Defaults to false. Setting it true does the following:
    • Localhost requests will proceed as normal. The "Real HTTP connections are disabled" error will not occur.
    • Localhost requests will not be recorded.
    • Previously recorded localhost requests will not be replayed.
  • default_cassette_options: The default options for your cassettes. These will be overridden by any options you set on each individual cassette.

Usage with your favorite ruby test/spec framework

VCR can easily be used with any ruby test or spec framework. Usually, you'll want to use VCR.use_cassette:

VCR.use_cassette('geocoding/Seattle, WA', :record => :new_episodes) do
  # do something that makes an HTTP request

Alternately, you can insert and eject a cassette with individual method calls from setup/before and teardown/after:

describe "Some object that makes an HTTP request" do
  before(:each) do
    VCR.insert_cassette('geocoding/Seattle, WA', :record => :new_episodes)

  it "does something that makes an HTTP request"

  after(:each) do

Usage with Cucumber

VCR provides additional support for cucumber. You can of course use VCR.use_cassette within a step definition, and that's the recommended way for any of your custom step definitions. But many times I find myself using generic step definitions provided by another library (such as the webrat/capybara web steps generated by cucumber-rails), and you don't want to modify these. VCR provides cucumber tagging support to help in these cases.

First, tag your scenario with something descriptive:

Scenario: Sign up with facebook connect

Then let VCR know about this tag, in features/support/vcr.rb (or some similar support file):

VCR.cucumber_tags do |t|
  t.tags '@facebook_http_request', '@twitter_status_update', :record => :none
  t.tags '@another_scenario_tag'

For each of the tags you specify in your cucumber_tags block, VCR will set up the appropriate Before and After hooks to use a cassette for the entire scenario. The tag (minus the '@') will be used as the cassette name, and it'll go in the cucumber_tags subdirectory of the configured cassette library directory.

If the last argument to #tags is a hash, VCR will use it as the options for the named cassettes.

Usage with Capybara

When you use any of the javascript-enabled drivers (selenium, celerity, culerity) with capybara, it'll need to ping the app running on localhost. Set the ignore_localhost option to true to allow this.

Cassette Customization

Cassettes are stored as simple plain text YAML files and can easily be edited to suit your needs. One common need is for a particular request to be stubbed using a regex rather than the raw URL. This is handy for URLs that contain non-deterministic portions (such as timestamps)--since the URL will be a bit different each time, the URL from the recorded request will not match the URL for future requests. You can simply change the URL to the YAML of the appropriate regex.

Figure out the yaml in irb:

>> require 'yaml'
=> true
>> puts /example\.com\/\d+/.to_yaml
--- !ruby/regexp /example\.com\/\d+/

Edit your cassette file:

request: !ruby/struct:VCR::Request 
  method: :get
  uri: !ruby/regexp /example\.com\/\d+/

Dynamic Cassettes

VCR's default recording and replaying is static. The exact response that is initially recorded will be replayed for all future requests. Usually this is fine, but in some cases you need something more dynamic. You can use ERB for this.

Enable ERB evaluation of a cassette using the :erb option:

VCR.use_cassette('user-subscription', :erb => true) do
  # do something that makes an HTTP request

You can use variables in your cassette's ERB by passing a hash:

VCR.use_cassette('user-subscription', :erb => { :user => User.last }) do
  # do something that makes an HTTP request

In your cassette:

request: !ruby/struct:VCR::Request 
  method: :get
  uri:<%= %>
response: !ruby/struct:VCR::Response 
  body: Hello, <%= %>!

FakeWeb or WebMock?

VCR works fine with either FakeWeb or WebMock. Overall, WebMock has more features, and you'll need to use WebMock if you want to use VCR with an HTTP library besides Net::HTTP. However, FakeWeb is currently about three times faster than WebMock, so you may want to stick with FakeWeb if you don't need WebMock's additional features. You can see the benchmarks for more details.

Suggested Workflow

First, configure VCR as I have above. I like setting the default record mode to :none so that no new HTTP requests are made without me explicitly allowing it.

When an HTTP request is made, you'll get an error such as:

Real HTTP connections are disabled. Unregistered request: get

Find the place that is making the HTTP request (the backtrace should help here). If you've already recorded this HTTP request to a cassette from a different test, you can simply re-use the cassette. Use VCR.use_cassette, as shown above. You may also want to refactor this into a helper method that sets up the VCR cassette and does whatever makes the HTTP request:

def set_user_address(user, address, city, state)
  VCR.use_cassette("geocoding/#{address}, #{city}, #{state}", :record => :new_episodes) do
    user.address.update_attributes!(:address => address, :city => city, :state => state)

In this case, I've used a dynamic cassette name based on the address being geocoded. That way, each separate address gets a different cassette, and tests that set the same user address will reuse the same cassette. I've also set the record mode to :new_episodes so that VCR will automatically record geocoding requests for a new address to a new cassette, without me having to change any code.

If the HTTP request that triggered the error is new, you'll have to record it for the first time. Simply use VCR.use_cassette with the record mode set to :new_episodes or :all. Run the test again, and VCR will record the HTTP interaction. I usually remove the record mode at this point so that it uses the default of :none in the future. Future test runs will get the recorded response, and if your code changes so that it is making a new HTTP request, you'll be notified by an error as shown above.

Ruby Version Compatibility

VCR has been tested on MRI 1.8.6, 1.8.7, 1.9.1 and 1.9.2-preview3 and jruby 1.5.1.

Notes, etc.

  • The objects serialized to the cassette YAML files changed with the 0.4 release. Cassettes recorded with older versions of VCR will not work with VCR 0.4.0 and later. However, VCR provides a rake task to migrate your old cassettes to the new format--see the changelog for more info.
  • The cassette name determines the name of the library file for the given cassette. Strings or symbols are fine, and you can include any characters, but spaces and invalid file name characters will be removed before the cassette reads or writes to its library file.
  • You can use a directory separator (i.e. '/') in your cassette names to cause it to use a subdirectory of the cassette library directory. The cucumber tagging support uses this.
  • VCR maintains a simple stack of cassettes. This allows you to nest them as deeply as you want. This is particularly useful when you have a cucumber step definition that uses a cassette, and you also want to use a cassette for the entire scenario using the tagging support.

Note on Patches/Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don't break it in a future version unintentionally.
  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.



Copyright (c) 2010 Myron Marston. See LICENSE for details.