Skip to content

Instantly share code, notes, and snippets.

@chebyte
Created June 27, 2012 18:56
Show Gist options
  • Save chebyte/3006013 to your computer and use it in GitHub Desktop.
Save chebyte/3006013 to your computer and use it in GitHub Desktop.
use vcr with rspec 2
require 'sendgrid_webapi'
require "fakeweb"
require 'vcr'
VCR.configure do |c|
c.cassette_library_dir = 'spec/cassettes'
c.hook_into :fakeweb
c.configure_rspec_metadata!
end
RSpec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true
# Add VCR to all tests
config.around(:each) do |example|
options = example.metadata[:vcr] || {}
if options[:record] == :skip
VCR.turned_off(&example)
else
name = example.metadata[:full_description].split(/\s+/, 2).join("/").gsub!(/(.)([A-Z])/,'\1_\2').downcase!.gsub(/[^\w\/]+/, "_")
VCR.use_cassette(name, options, &example)
end
end
end
#then into the test you have to add :vcr to describe or it method like this:
describe "test", :vcr do
it "some test" do
end
end
#OR you can add this automatically to all test with the following code
RSpec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true
# Add VCR to all tests
config.around(:each) do |example|
options = example.metadata[:vcr] || {}
if options[:record] == :skip
VCR.turned_off(&example)
else
name = example.metadata[:full_description].split(/\s+/, 2).join("/").gsub!(/(.)([A-Z])/,'\1_\2').downcase!.gsub(/[^\w\/]+/, "_")
VCR.use_cassette(name, options, &example)
end
end
end
@pencilcheck
Copy link

Now you can remove all those in RSpec.configure and add VCR config allow_http_connections_when_no_cassette = true
then it will work automagically

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment