myronmarston/vcr — Issue #119: Weird issue with Rspec and the "You cannot nest multiple cassettes with the same name." error
Author: keithpitt
Created: 2011-12-28 10:22:28+00:00
State: open

I have some code that looks something like this:

```ruby
describe Booking::Service::Manager do

context "#book!" do

# More specs...

context "when cancelling a VAN fails" do

use_vcr_cassette :match_requests_on => [ :uri, :headers ]

before :each do
ServiceStubber.unsuccessfull_cancel_reservation_request
booking.credit_card = CREDIT_CARDS[:returns_success]
booking.service.authorize!
@responses = booking.service.book!
booking.reload
end

it "should not have succeeded" do
@responses.should_not be_success
end

it "set the booking state to failed" do
booking.state.should == "failed"
end

it "should generate the correct audit log" do
messages = booking.audits.map(&:description)

messages.should == ["Foo", "Bar"]
end

end

# More specs...

end

end
```
When I run this spec as part of "rake spec" I get this error:

```
6) Booking::Service::Manager#book! when sending a reservation fails should create the correct audit log
Failure/Error: Unable to find matching line from backtrace
ArgumentError:
There is already a cassette with the same name (Booking::Service::Manager/#book!/when sending a reservation fails). You cannot nest multiple cassettes with the same name.
# /usr/local/rvm/gems/ruby-1.9.2-p290-patched/gems/vcr-1.11.3/lib/vcr.rb:53:in `insert_cassette'
# /usr/local/rvm/gems/ruby-1.9.2-p290-patched/gems/vcr-1.11.3/lib/vcr/test_frameworks/rspec.rb:9:in `block in use_vcr_cassette'
# /usr/local/rvm/gems/ruby-1.9.2-p290-patched/bundler/gems/rspec-core-9930ff48ee1d/lib/rspec/core/hooks.rb:35:in `instance_eval'
# /usr/local/rvm/gems/ruby-1.9.2-p290-patched/bundler/gems/rspec-core-9930ff48ee1d/lib/rspec/core/hooks.rb:35:in `run_in'
# /usr/local/rvm/gems/ruby-1.9.2-p290-patched/bundler/gems/rspec-core-9930ff48ee1d/lib/rspec/core/hooks.rb:70:in `block in run_all'
# /usr/local/rvm/gems/ruby-1.9.2-p290-patched/bundler/gems/rspec-core-9930ff48ee1d/lib/rspec/core/hooks.rb:70:in `each'
# /usr/local/rvm/gems/ruby-1.9.2-p290-patched/bundler/gems/rspec-core-9930ff48ee1d/lib/rspec/core/hooks.rb:70:in `run_all'
# /usr/local/rvm/gems/ruby-1.9.2-p290-patched/bundler/gems/rspec-core-9930ff48ee1d/lib/rspec/core/hooks.rb:116:in `run_hook'
# /usr/local/rvm/gems/ruby-1.9.2-p290-patched/bundler/gems/rspec-core-9930ff48ee1d/lib/rspec/core/example_group.rb:233:in `block in run_before_each_hooks'
# /usr/local/rvm/gems/ruby-1.9.2-p290-patched/bundler/gems/rspec-core-9930ff48ee1d/lib/rspec/core/example_group.rb:233:in `each'
# /usr/local/rvm/gems/ruby-1.9.2-p290-patched/bundler/gems/rspec-core-9930ff48ee1d/lib/rspec/core/example_group.rb:233:in `run_before_each_hooks'
# /usr/local/rvm/gems/ruby-1.9.2-p290-patched/bundler/gems/rspec-core-9930ff48ee1d/lib/rspec/core/example.rb:149:in `run_before_each'
# /usr/local/rvm/gems/ruby-1.9.2-p290-patched/bundler/gems/rspec-core-9930ff48ee1d/lib/rspec/core/example.rb:51:in `block in run'
# /usr/local/rvm/gems/ruby-1.9.2-p290-patched/bundler/gems/rspec-core-9930ff48ee1d/lib/rspec/core/example.rb:111:in `with_around_hooks'
# /usr/local/rvm/gems/ruby-1.9.2-p290-patched/bundler/gems/rspec-core-9930ff48ee1d/lib/rspec/core/example.rb:49:in `run'
# /usr/local/rvm/gems/ruby-1.9.2-p290-patched/bundler/gems/rspec-core-9930ff48ee1d/lib/rspec/core/example_group.rb:306:in `block in run_examples'
# /usr/local/rvm/gems/ruby-1.9.2-p290-patched/bundler/gems/rspec-core-9930ff48ee1d/lib/rspec/core/example_group.rb:302:in `map'
# /usr/local/rvm/gems/ruby-1.9.2-p290-patched/bundler/gems/rspec-core-9930ff48ee1d/lib/rspec/core/example_group.rb:302:in `run_examples'
# /usr/local/rvm/gems/ruby-1.9.2-p290-patched/bundler/gems/rspec-core-9930ff48ee1d/lib/rspec/core/example_group.rb:274:in `run'
# /usr/local/rvm/gems/ruby-1.9.2-p290-patched/bundler/gems/rspec-core-9930ff48ee1d/lib/rspec/core/example_group.rb:275:in `block in run'
# /usr/local/rvm/gems/ruby-1.9.2-p290-patched/bundler/gems/rspec-core-9930ff48ee1d/lib/rspec/core/example_group.rb:275:in `map'
# /usr/local/rvm/gems/ruby-1.9.2-p290-patched/bundler/gems/rspec-core-9930ff48ee1d/lib/rspec/core/example_group.rb:275:in `run'
# /usr/local/rvm/gems/ruby-1.9.2-p290-patched/bundler/gems/rspec-core-9930ff48ee1d/lib/rspec/core/example_group.rb:275:in `block in run'
# /usr/local/rvm/gems/ruby-1.9.2-p290-patched/bundler/gems/rspec-core-9930ff48ee1d/lib/rspec/core/example_group.rb:275:in `map'
# /usr/local/rvm/gems/ruby-1.9.2-p290-patched/bundler/gems/rspec-core-9930ff48ee1d/lib/rspec/core/example_group.rb:275:in `run'
# /usr/local/rvm/gems/ruby-1.9.2-p290-patched/bundler/gems/rspec-core-9930ff48ee1d/lib/rspec/core/command_line.rb:24:in `block (2 levels) in run'
# /usr/local/rvm/gems/ruby-1.9.2-p290-patched/bundler/gems/rspec-core-9930ff48ee1d/lib/rspec/core/command_line.rb:24:in `map'
# /usr/local/rvm/gems/ruby-1.9.2-p290-patched/bundler/gems/rspec-core-9930ff48ee1d/lib/rspec/core/command_line.rb:24:in `block in run'
# /usr/local/rvm/gems/ruby-1.9.2-p290-patched/bundler/gems/rspec-core-9930ff48ee1d/lib/rspec/core/reporter.rb:12:in `report'
# /usr/local/rvm/gems/ruby-1.9.2-p290-patched/bundler/gems/rspec-core-9930ff48ee1d/lib/rspec/core/command_line.rb:21:in `run'
# /usr/local/rvm/gems/ruby-1.9.2-p290-patched/bundler/gems/rspec-core-9930ff48ee1d/lib/rspec/core/runner.rb:80:in `run_in_process'
# /usr/local/rvm/gems/ruby-1.9.2-p290-patched/bundler/gems/rspec-core-9930ff48ee1d/lib/rspec/core/runner.rb:69:in `run'
# /usr/local/rvm/gems/ruby-1.9.2-p290-patched/bundler/gems/rspec-core-9930ff48ee1d/lib/rspec/core/runner.rb:10:in `block in autorun'
```

Now this error is pretty self-explanitory, but I don't understand _why_ I'm getting it. After digging into the VCR code, I get the error when VCR tries to insert a cassette that has already been inserted. I'm *only* using VCR with the above syntax. I'm not calling VCR manually anywhere in my specs.

I put some debugging code in the Rspec adapter, a `puts` in the `before :each` and `after :each` call. I noticed that `before :each` was being called in a row, with the `after :each` - and because of this - it was inserting the cassette twice. I'm wracking my brain over this. Do you have any ideas of things I can look at / try?

Cheers,

Keith