Search code examples
ruby-on-railsrubyrspecconventionvcr

VCR+rspec metadata: how to use inferred cassette name like use_vcr_cassette


The VCR testing gem provides a macro called use_vcr_cassette that has a handy convention to infer the cassette name based on the example name. Since the use_vcr_cassette macro is deprecated in the latest versions of VCR in favor of rspec metadata (i.e. :vcr), is there still any way to automatically pick up the cassette name without having to go specify it in every spec?

(BTW I'm using this with a Rails application.)


Solution

  • Configuring RSpec to enable configure_rspec_metadata! will enable automatic cassette naming.

    require 'vcr'
    
    VCR.configure do |c|
      c.cassette_library_dir = 'spec/cassettes'
      c.hook_into :fakeweb
      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