11

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?

DeejUK
  • 12,891
  • 19
  • 89
  • 169

5 Answers5

12

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.

Casey Davidson
  • 319
  • 3
  • 3
10

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.

Anthony
  • 15,435
  • 4
  • 39
  • 69
  • 1
    Thanks. Thing is, I have _one_ spec of many that I want VCR to ignore, and my configure block is in my `spec_helper.rb` that applies to everything. Any suggestions as to the best way of limiting the effect of this config to only one spec? – DeejUK Jun 08 '15 at 11:54
  • I'd move this a tag that the spec_helper can switch on. – Anthony Mar 25 '20 at 17:57
3

Based on Casey's answer I came up with this helper module:

module VcrHelpers
  def self.perform_without_cassette
    VCR.configure { |c| c.allow_http_connections_when_no_cassette = true }
    yield
  ensure
    VCR.configure { |c| c.allow_http_connections_when_no_cassette = false }
  end
end

It can then be called from any spec like this:

VcrHelpers.perform_without_cassette do
 some_http_request
end

See docs for including/requiring in tests.

jprio
  • 97
  • 1
  • 13
KNejad
  • 2,376
  • 2
  • 14
  • 26
  • Worked like a charm. Used it within a subject do end block. Absolutely stellar job @KNejad – jprio Dec 11 '22 at 04:52
3

There are a few methods that let you do this, here are some resources:

Something along these lines might work:

# In the beginning of your describe block
around do |example|
  VCR.turned_off { example.run }
end

or

let(:request) { 
  VCR.turned_off do
    get some_path(params)
  end
end

it { expect { request } .to .... }

You may need to use VCR.eject_cassette before using a turn off method depending on what you're doing in your spec.

Allison
  • 1,925
  • 1
  • 18
  • 25
2

In my case I din't want to allow real HTTP connections for non-VCR specs, I just wanted VCR to be disabled for those specs so that Webmock handled them directly instead. This is what worked for me:

RSpec.configure do |config|
  config.around do |example|
    if example.metadata.key?(:vcr)
      example.run
    else
      VCR.turned_off { example.run }
    end
  end
end
Jimbali
  • 2,065
  • 1
  • 20
  • 24