Skip to content

Instantly share code, notes, and snippets.

@mattbrictson
Created September 11, 2015 15:55
Show Gist options
  • Save mattbrictson/72910465f36be8319cde to your computer and use it in GitHub Desktop.
Save mattbrictson/72910465f36be8319cde to your computer and use it in GitHub Desktop.
Using vcr with Minitest + Rails (test unit style)
group :test do
gem "minitest"
gem "vcr"
gem "webmock", "~> 1.15.0"
end
# test/test_helper.rb
# Load everything from test/support
Dir[File.expand_path("../support/**/*.rb", __FILE__)].each { |rb| require(rb) }
# test/support/vcr.rb
require "vcr"
VCR.configure do |config|
config.allow_http_connections_when_no_cassette = false
config.cassette_library_dir = File.expand_path("../../cassettes", __FILE__)
config.hook_into :webmock
config.ignore_request { ENV["DISABLE_VCR"] }
config.ignore_localhost = true
config.default_cassette_options = {
:record => :new_episodes
}
end
# Monkey patch the `test` DSL to enable VCR and configure a cassette named
# based on the test method. This means that a test written like this:
#
# class OrderTest < ActiveSupport::TestCase
# test "user can place order" do
# ...
# end
# end
#
# will automatically use VCR to intercept and record/play back any external
# HTTP requests using `cassettes/order_test/_user_can_place_order.yml`.
#
class ActiveSupport::TestCase
def self.test(test_name, &block)
return super if block.nil?
cassette = [name, test_name].map do |str|
str.underscore.gsub(/[^A-Z]+/i, "_")
end.join("/")
super(test_name) do
VCR.use_cassette(cassette) do
instance_eval(&block)
end
end
end
end
@fredplante
Copy link

Thanks Matt! I use the same approach now and it works perfectly well 👍

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