Eject All

31 views
Skip to first unread message

Tom Rossi

unread,
Apr 2, 2012, 4:44:17 PM4/2/12
to vcr-...@googlegroups.com
Is there something like a VCR.eject_all! method?  I'm having problems with tests that work independently, but not when I run them together with a Rake command.  I can't seem to find any cassettes that I have inserted in the setup without ejecting in the teardown.

Myron Marston

unread,
Apr 2, 2012, 4:55:01 PM4/2/12
to vcr-...@googlegroups.com
On Mon, Apr 2, 2012 at 1:44 PM, Tom Rossi <t...@themolehill.com> wrote:
Is there something like a VCR.eject_all! method?  I'm having problems with tests that work independently, but not when I run them together with a Rake command.  I can't seem to find any cassettes that I have inserted in the setup without ejecting in the teardown.

No, there's no #eject_all!.  No one's ever wanted it before.  In general, the recommended API is VCR.use_cassette which takes care of ejecting the cassette for you.

Do you see anything that suggests that cassettes are not being ejected?

Myron

Tom Rossi

unread,
Apr 2, 2012, 9:09:25 PM4/2/12
to vcr-...@googlegroups.com
I haven't had time to isolate it yet.  All my tests (Test::Unit) are following this pattern for starting VCR and stopping a cassette:

  setup do
    VCR.insert_cassette('basecamp settings', :record => :new_episodes, :match_requests_on => [:method, :uri, :authorization])
  end
  
  teardown do
    VCR.eject_cassette
  end

I tried to change the insert_cassette to use_cassette and got this error:

NoMethodError: undefined method `arity' for nil:NilClass
method call_block in vcr (2.0.0) lib/vcr/util/variable_args_block_caller.rb at line 5
method use_cassette in vcr (2.0.0) lib/vcr.rb at line 150

You think I am doing something wrong?

Thanks,
Tom

Myron Marston

unread,
Apr 2, 2012, 11:11:05 PM4/2/12
to vcr-...@googlegroups.com
On Mon, Apr 2, 2012 at 6:09 PM, Tom Rossi <t...@themolehill.com> wrote:
I haven't had time to isolate it yet.  All my tests (Test::Unit) are following this pattern for starting VCR and stopping a cassette:

  setup do
    VCR.insert_cassette('basecamp settings', :record => :new_episodes, :match_requests_on => [:method, :uri, :authorization])
  end
  
  teardown do
    VCR.eject_cassette
  end

You can certainly use VCR this way, but if this Test::Unit class has multiple tests that make different HTTP requests (or different sequence of HTTP requests), you may run into trouble.  VCR is designed to be use very granularly--where one cassette gets used for one sequence of requests.  Think of a "sequence of requests" being the one or more HTTP requests that get triggered by a single action or event in your application domain.  Each cassette provides a sandbox.   The HTTP interactions recorded to one cassette are guaranteed to only be used when that cassette is active.  When you re-use the same cassette for multiple request sequences you're liable to wind up situations where VCR doesn't act as you expect.

So, I recommend wrapping your individual tests (or even individual parts of single tests) in its own VCR.use_cassette block, recording to its own cassette (or to a common one that multiple tests that use the same request sequence share).


I tried to change the insert_cassette to use_cassette and got this error:

NoMethodError: undefined method `arity' for nil:NilClass
method call_block in vcr (2.0.0) lib/vcr/util/variable_args_block_caller.rb at line 5
method use_cassette in vcr (2.0.0) lib/vcr.rb at line 150

You think I am doing something wrong?

Sorry if this confused you...but VCR.use_cassette requires a block.  If you can't do the block form (because of separate setup/teardown hooks like in your example), then the insert_cassette/eject_cassette thing is the way to go.  FWIW, I just pushed a commit that should improve the error message here:

https://github.com/myronmarston/vcr/commit/4f900a784c23f1eaee9c146a8a65c548ef5b5acf

Anyhow, it's hard for me to help troubleshot this without something executable I can play with.  If you can get me something executable I'll take a look.

If you do want VCR.eject_all! (to eliminate that as the source of the problem), you can easily add it:

def VCR.eject_all!
  while current_cassette
    eject_cassette
  end
end

Myron
Reply all
Reply to author
Forward
0 new messages