VCR.configure do |config| config.cassette_library_dir = "fixtures/vcr_cassettes" # fixtures/vcr_cassettes config.hook_into :webmock end
require 'rubygems' require 'test/unit' require 'vcr' class VCRTest < Test::Unit::TestCase def test_example_dot_com VCR.use_cassette("synopsis") do # synopsis.yml response = Net::HTTP.get_response(URI('http://www.iana.org/domains/reserved')) assert_match /Example domains/, response.body end end end
VCR will record the HTTP request to
fixtures/vcr_cassettes/synopsis.yml. ( You can use a different cassette library directory (e.g.,
Run it again, and VCR will replay the response from iana.org when the HTTP request is made.
This test is now
- fast (no real HTTP requests are made anymore)
- deterministic (the test will continue to pass, even if you are offline, or iana.org goes down for maintenance)
- accurate (the response will contain the same headers and body you get from a real request).
VCR.use_cassette('whatever') do expect do post :create, post_params end.to change(Model, :count).by(1) end