Skip to content

Instantly share code, notes, and snippets.

@cutalion
Last active December 23, 2015 23:59
Show Gist options
  • Save cutalion/6713504 to your computer and use it in GitHub Desktop.
Save cutalion/6713504 to your computer and use it in GitHub Desktop.
Temporary turn VCR off

I often use VCR gem. And I always configure it to use rspec metadata to turn it on with just :vcr symbol.

When I know that some module will always hit the external service, I turn VCR on at the very top describe.

describe "Something", :vcr do
  it "should make coffee" do
  end
end

And when I add a new example, I want all requests to be live, until I get my test pass. I can run any code without VCR in VCR.turned_off do ... end block. I must eject the loaded cassette before with VCR.eject_cassette (there must be a reason why it can't be done automatically).

And finally since I'm using webmock (by default), I have to ask it to let me do real requests with WebMock.allow_net_connect!

I think it's not fair when turning something on is easy, but turning it off isn't.

I would write

describe "Something", :vcr do
  it "should make coffee" do
  end

  it "should be easy", :vcr_off do
  end
end
VCR.configure do |c|
c.cassette_library_dir = 'spec/fixtures/vcr_cassettes'
c.hook_into :webmock # or :fakeweb
c.ignore_localhost = true
c.default_cassette_options = { :record => :new_episodes }
c.configure_rspec_metadata!
end
RSpec.configure do |c|
c.before :each, vcr_off: true do
WebMock.allow_net_connect!
end
c.after :each, vcr_off: true do
WebMock.disable_net_connect!
end
c.around :each, vcr_off: true do |ex|
VCR.eject_cassette
VCR.turned_off do
ex.run
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment