Skip to content

Instantly share code, notes, and snippets.

@mcls
Last active November 24, 2016 15:20
Show Gist options
  • Save mcls/d7c529244547e5d5dc9e1136a9571434 to your computer and use it in GitHub Desktop.
Save mcls/d7c529244547e5d5dc9e1136a9571434 to your computer and use it in GitHub Desktop.
VCR helpers and config for use with RSpec
# file: spec/support/vcr.rb
require 'vcr'
# More info:
# - https://github.com/vcr/vcr
# - https://relishapp.com/vcr/vcr/docs
VCR.configure do |config|
config.cassette_library_dir = File.expand_path(File.join(__dir__, "..", "support", "vcr_cassettes"))
config.hook_into :webmock
if ENV['VCR_DEBUG'] == 'true'
# config.allow_http_connections_when_no_cassette = true
config.debug_logger = STDOUT
config.after_http_request do |request, response|
if request.method == :post
puts "POST Request:#{request.uri}"
puts "#{request.body}" # or request.body
end
end
end
config.ignore_request do |request|
# Ignore capybara's __identify__ requests
uri = URI(request.uri)
uri.path.match(/^\/__identify__/).present?
end
end
module VCRHelpers
# Allows the VCR record mode to be set by an environment variable
# https://relishapp.com/vcr/vcr/v/3-0-1/docs/record-modes
RECORD_MODE = (ENV['VCR_MODE'] || :none).to_sym
DEFAULT_OPTS = {
allow_playback_repeats: true
}
# You can't change these
FORCED_OPTS = {
re_record_interval: 6.months,
record: RECORD_MODE,
}
def with_vcr(cassette_name, opts = {})
vcr_opts = DEFAULT_OPTS.merge(opts.merge(FORCED_OPTS))
VCR.use_cassette(cassette_name, vcr_opts) do
yield
end
end
def self.info_message
"VCR record mode is #{RECORD_MODE.inspect}. Can be changed by ENV['VCR_MODE'].\n" \
"More info: https://relishapp.com/vcr/vcr/v/3-0-1/docs/record-modes."
end
end
RSpec.configure do |config|
config.include VCRHelpers
config.before(:suite) do
puts VCRHelpers.info_message
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment