absicw
Last Updated: February 25, 2016
·
1.289K
· joshteng
10504ee74cc7e9984600710e51f08fc9

Recipe: Using VCR for specs

Add to .gitignore

spec/vcr/

Gemfile

group :test do
  gem 'vcr'
  gem 'webmock'
end

/spec/support/vcr.rb

VCR.configure do |c|
  c.cassette_library_dir = Rails.root.join("spec", "vcr")
  c.hook_into :webmock # or :fakeweb
  #c.filter_sensitive_data('APIKEY') { ENV["MAILCHIMP_KEY"] } #use this to filter out sensitive information that you don't want VCR to store in spec/vcr files
  #c.filter_sensitive_data('LISTID') { ENV["MAILCHIMP_LIST_ID"] }
end

RSpec.configure do |c|
  c.treat_symbols_as_metadata_keys_with_true_values = true
  c.around(:each, :vcr) do |example|
    name = example.metadata[:full_description].split(/\s+/, 2).join("/").underscore.gsub(/[^\w\/]+/, "_")
    options = example.metadata.slice(:record, :match_requests_on).except(:example_group)
    VCR.use_cassette(name, options) { example.call }
  end
end

Your spec

it "subscribes lead", :vcr, record: :once do
  lead.add_to_mailchimp
  subscribers = Mailchimp::Lists.new(MAILCHIMP).members(ENV["MAILCHIMP_LIST_ID"])["data"] #retrieving mailchimp list
  subscribers_email = subscribers.map { |subscriber| subscriber["email"] }

  #refactor into macro?
  Mailchimp::Lists.new(MAILCHIMP).unsubscribe(ENV["MAILCHIMP_LIST_ID"], {email: lead.email }, true, false, false) #remove lead from mailchimp list

  subscribers_email.should include(lead.email)
end
Say Thanks
Respond