Eric
Eric

Reputation: 6465

How can I configure VCR to use cassette names that match the request?

I am using rspec/capybara/VCR to record tapes. Currently my tapes are automatically named to include a simplified version of the hierarchy of the test that is being run, as is the default.

I would like to configure the cassette naming scheme so that it is dependent instead on the parameters of the request, so any PUT to /abc.com with a body of XYZ would use the same tape. My thought was to configure VCR with this:

config.around_http_request do |request|
  tape_name = Digest::SHA1.hexdigest [request.method, request.uri, request.headers.to_s, request.body.to_s].join('')
  puts "Using tape #{tape_name}"
  puts "on = #{VCR.turned_on?}"
  VCR.use_cassette(tape_name, :record => :new_episodes, &request)
end

But when I do that, eventually I get the errors such as:

There is already a cassette with the same name (5d971f35322c4e0cf7d379aa39a28ef12994552f).  You cannot nest multiple cassettes with the same name.

How can I name tapes with what they contain and prevent this error?

Upvotes: 3

Views: 2456

Answers (1)

alexb
alexb

Reputation: 908

I've just experienced the same problem and it seems that the root cause was that cassettes somehow got nested during some requests (i.e. like using a VCR.use_cassette('whatever') inside another VCR.use_cassette('whatever') block). So, I tried to put an eject_cassette prior to use_cassette and it seems to work fine, e.g. in this case:

VCR.eject_cassette(tape_name)
VCR.use_cassette(tape_name, :record => :new_episodes, &request)

Upvotes: 2

Related Questions