DeejUK
DeejUK

Reputation: 13501

Turn VCR off for Specific Specs

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?

Upvotes: 13

Views: 9047

Answers (7)

Constantin De La Roche
Constantin De La Roche

Reputation: 3310

Configuring allow_http_connections_when_no_cassette did the job for me.

# spec_helper.rb
require "vcr"

VCR.configure do |c|
    # VCR config...
    c.allow_http_connections_when_no_cassette = true # add this line
end

If :vcr is not present in the spec, it does not check requests.

Upvotes: 0

SMAG
SMAG

Reputation: 798

Casey's Answer As a Shared Context

so you can reuse this logic

shared_context :vcr_allows_http_connections 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
end

then you can use it like so:

RSpec.describe :allow_all, type: :request do
  include_context :vcr_allows_http_connections

  # specs with http connections for the rest of the describe block
end

# OR

RSpec.describe :allow_some, type: :request do
  # specs with without http connections or vcr cassettes

  include_context :vcr_allows_http_connections do
    # specs with http connections only for this block
  end
end

Upvotes: 1

KNejad
KNejad

Reputation: 2508

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.

Upvotes: 3

Allison
Allison

Reputation: 2333

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.

Upvotes: 3

Jimbali
Jimbali

Reputation: 2538

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

Upvotes: 6

Casey Davidson
Casey Davidson

Reputation: 349

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.

Upvotes: 14

Anthony
Anthony

Reputation: 15967

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.

Upvotes: 11

Related Questions