Usage with RSpec metadata

VCR provides easy integration with RSpec using metadata. To set this up, call configure_rspec_metadata! in your VCR.configure block.

Once you've done that, you can have an example group or example use VCR by passing :vcr as an additional argument after the description string. It will set the cassette name based on the example's full description.

You can override the cassette name by passing a string (:vcr => 'my_cassette').

If you need to override the other options, you can pass a hash (:vcr => { ... }).

Background ()

Given a file named "spec/spec_helper.rb" with:

require 'vcr'

VCR.configure do |c|
  c.cassette_library_dir = 'spec/cassettes'
  c.hook_into :webmock
  c.configure_rspec_metadata!
end

RSpec.configure do |c|
  # so we can use `:vcr` rather than `:vcr => true`;
  # in RSpec 3 this will no longer be necessary.
  c.treat_symbols_as_metadata_keys_with_true_values = true
end

And a previously recorded cassette file "spec/cassettes/Group/optionally_raises_an_error.yml" with:

Use :vcr metadata

Given a file named "spec/vcr_example_spec.rb" with:

When I run rspec spec/vcr_example_spec.rb

Then it should pass with "5 examples, 0 failures"

And the file "spec/cassettes/VCR_example_group_metadata/records_an_http_request.yml" should contain "Hello"

And the file "spec/cassettes/VCR_example_group_metadata/records_another_http_request.yml" should contain "Hello"

And the file "spec/cassettes/my_cassette.yml" should contain "Hello"

And the file "spec/cassettes/VCR_example_group_metadata/in_a_nested_example_group/records_another_one.yml" should contain "Hello"

And the file "spec/cassettes/VCR_example_metadata/records_an_http_request.yml" should contain "Hello".

:allow_unused_http_interactions => false causes a failure if there are unused interactions

And a file named "spec/vcr_example_spec.rb" with:

When I run rspec spec/vcr_example_spec.rb

Then it should fail with an error like:

:allow_unused_http_interactions => false does not raise if the example already failed

And a file named "spec/vcr_example_spec.rb" with:

When I run rspec spec/vcr_example_spec.rb

Then it should fail with "boom"

And the output should not contain "There are unused HTTP interactions".

Pass a hash to set the cassette options

Given a file named "spec/vcr_example_spec.rb" with:

When I run rspec spec/vcr_example_spec.rb

Then it should pass with "2 examples, 0 failures".

Last updated

Was this helpful?