Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turn VCR off for Specific Specs

Tags:

ruby

rspec

vcr

How can I tell VCR that I want it to completely ignore a spec file?

I've read a post on Google Groups that suggests either allowing real HTTP requests, or turning VCR off explicitly.

What would be much more usable in my opinion would be for VCR to not stick its nose in unless a spec has the :vcr metadata tag. I don't want to turn VCR off and back on again in before/after, as I don't know if it was on beforehand. I don't want to allow real HTTP requests across all specs, just some particular ones.

Is there any way to make VCR more selective?

like image 440
EngineerBetter_DJ Avatar asked Jun 08 '15 11:06

EngineerBetter_DJ


2 Answers

This isn't the most elegant solution, but you can use an instance variable to return the configuration to it's original setting

describe "A group of specs where you want to allow http requests" do
  before do
    VCR.configure do |c|
      @previous_allow_http_connections = c.allow_http_connections_when_no_cassette?
      c.allow_http_connections_when_no_cassette = true
    end
  end

  after do
    VCR.configure do |c|
      c.allow_http_connections_when_no_cassette = @previous_allow_http_connections
    end
  end

  # Specs in this block will now allow http requests to be made

end

I've found this helpful for while I'm working to initially get an API up and running, and want to be able to debug the requests that I'm making. Once I've got the API working correctly, I can remove the before and after blocks, and use VCR as normal.

like image 79
Casey Davidson Avatar answered Oct 09 '22 16:10

Casey Davidson


Sure, in your config block add:

VCR.configure do |c|
  c.allow_http_connections_when_no_cassette = true
end

This is AFAIK the only option VCR has regarding your test suite. See the docs.

Most likely though you should really be considering the record modes for behavior like this so it's actionable.

like image 45
Anthony Avatar answered Oct 09 '22 17:10

Anthony