Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vcr breaks from multiple web requests

A project that I am working on has integration tests which actually go out and hit a 3rd party api over the wire... Running these tests takes a very long time. I suggested that we implement something like VCR so that the data the tests interact with can be captured as fixtures, and improve reliability and speed of these tests.

I don't have access to the codebase at this moment, but I believe the tests were doing something like this:

before do
  login_some_user
end

after do
  make_web_request_to_clear_items_in_cart
end

it "adds some items to the user's cart" do
  make_web_request_to_add_item_to_a_cart
end

So basically the before block was making a web request, the example was making a totally different request, and an after block (which I know is not ideal to use) made a 3rd request to clean up records that had been created by the example.

I setup an around block in spec_helper that captures web requests and stores them named after the example. However, after running the tests repeatedly I found that they became extremely flakey, sometimes passing, sometimes not.. I tried wrapping the before and after blocks in a separate VCR.use_cassette block call, but it made no difference.

I am wondering if I am missing something, and if there's a way to handle multiple requests like this or what?

like image 739
patrick Avatar asked May 08 '13 04:05

patrick


1 Answers

VCR can handle multiple requests. From you pseudo-code, you would have the following:

it "adds some items to the user's cart" do
 VCR.use_cassette "your_path/cassette_name" do
   login_some_user
   make_web_request_to_add_item_to_a_cart
   make_web_request_to_clear_items_in_cart
 end
end

The cassette your_path/cassette_name would contain all 3 web requests.

Your issue likely comes from using before and after blocks. See Myron Marston explanation of how VCR handles the before(:all) hook.

like image 166
Jeff Devine Avatar answered Oct 09 '22 06:10

Jeff Devine