Skip to content

Instantly share code, notes, and snippets.

@nisevi
Forked from myronmarston/ways_to_use_vcr.rb
Created May 30, 2018 20:42
Show Gist options
  • Save nisevi/0d258a459dd2b01b50b250eb4364d694 to your computer and use it in GitHub Desktop.
Save nisevi/0d258a459dd2b01b50b250eb4364d694 to your computer and use it in GitHub Desktop.
Ways to use VCR for a request made by a let block
# 1) Use VCR.use_cassette in your let block. This will use
# the cassette just for requests made by creating bar, not
# for anything else in your test.
let(:foo) { VCR.use_cassette("foo") { create(:bar) } }
it "uses foo" do
foo
end
# 2) Wrap the it block that uses #foo in VCR.use_cassette.
# This will use the cassette for requests made by creating
# bar and for any other HTTP requests made in this test.
let(:foo) { create(:bar) }
it "uses foo" do
VCR.use_cassette("foo") do
foo
end
end
# 3) Use RSpec metadata. This will use the cassette for
# the duration of the example, so that it's used for any
# requests made while the example is running (including
# when it calls #foo). Note: this will only work i you
# are using RSpec 2.x.
VCR.configure do |c|
c.configure_rspec_metadata!
end
let(:foo) { create(:bar) }
it "uses foo", :vcr => true do
foo
end
# 4) Use RSpec metadata at the `describe` (or `context`) level.
# This is like the last one, but it'll use a separate
# cassette for _each_ of the examples in the example group
# named after the examples themselves. Again,
# this will only work if you're using RSpec 2.x.
VCR.configure do |c|
c.configure_rspec_metadata!
end
describe "something", :vcr => true do
let(:foo) { create(:bar) }
it "uses foo" do
foo
end
end
# 5) Use the RSpec macro. This will use the same cassette
# for each of the examples in the example group. This may
# be what you want (i.e. if the same HTTP request is made by
# each example, and you are making different assertions about
# the result), but if the examples make different HTTP
# requests then you probably don't want them to share a cassette.
# This will only work if you're using RSpec.
RSpec.configure do |c|
c.extend VCR::RSpec::Macros
end
describe "something" do
use_vcr_cassette "foo"
let(:foo) { create(:bar) }
it "uses foo" do
foo
end
end
# 6) Use before/after hooks to insert and eject the cassette. This
# carries with it the same caveats as with the last approach: the
# same cassette will be used for each example in the group. This
# should work with RSpec or minitest/spec.
describe "something" do
before(:each) do
VCR.insert_cassette("foo")
end
after(:each) do
VCR.eject_cassette
end
let(:foo) { create(:bar) }
it "uses foo" do
foo
end
end
# 7) Use an around hook instead. This is just like the last one,
# but using one hook instead of two. This will only work if you're
# using RSpec 2.x.
describe "something" do
around(:each) do |example|
VCR.use_cassette("foo", &example)
end
let(:foo) { create(:bar) }
it "uses foo" do
foo
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment