Search code examples
ruby-on-railsrubycapybaravcrpoltergeist

VCR Sharing requests between cassettes?


My VCR config is:

VCR.configure do |c|
  c.configure_rspec_metadata!
  c.cassette_library_dir = 'spec/cassettes'
  c.hook_into :webmock
  c.ignore_localhost = true
end

And a test example is:

it "creates a build", :vcr => {:cassette_name => "build/feature/create"} do
  visit new_build_path(build)

  fill_in("build_name", :with => "Test Build")
  click_button("Create Build")

  build = Build.first

  page.should have_content("Build was successfully created.")

  current_path.should == build_path(hub)
end

When running this test it calls out to several 3rd Party API's which requests get recorded via VCR. The problem I am having is that it appears that VCR is using requests from other cassettes while running which is causing intermittent failures for certain tests. I have checked the cassettes and sometimes (depending on order is how it appears) all requests will be recorded and played back perfectly. It is worth noting this is when the whole suite is run, they always work when run by themselves. I do not share cassettes between the specs that fail, the only thing that is shared is some common requests to the API's and I force the naming of the cassettes just to make sure it is using the correct ones. I hope this makes sense...

My main question is what can be causing this problem? When using record => :new_episodes the tests work perfectly as well but not when using the record => :once mode. This may be okay given the situation but I want to make sure I am not creating unnecessary requests and from my understanding record => :once should work given the requests for each spec should be isolated.

I know this may be hard to answer without more information so let me know if any will help. Thanks in advance!


Solution

  • As you said, it's hard to answer from the little bit of information you've given. I can give some suggestions for helping to troubleshoot, though.

    • You mention that you're using a capybara JS driver. Capybara's JS drivers are asynchronous, which means that you test thread may continue on before your app has processed a particular request -- so if the test script continues on and ejects or inserts a new VCR cassette, and then your app makes an HTTP request -- it could lead to race conditions.
    • VCR has a debug_logger option that will give you lots of insight into exactly what VCR is doing. It may answer your question.

    Good luck!