Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.
Record your test suite’s HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.
Help Wanted
We’re looking for more maintainers. If you’d like to help maintain a well-used gem please spend some time reviewing pull requests, issues, or participating in discussions.
require 'rubygems'
require 'test/unit'
require 'vcr'
VCR.configure do |config|
config.cassette_library_dir = "fixtures/vcr_cassettes"
config.hook_into :webmock
end
class VCRTest < Test::Unit::TestCase
def test_example_dot_com
VCR.use_cassette("synopsis") do
response = Net::HTTP.get_response(URI('http://www.iana.org/domains/reserved'))
assert_match /Example domains/, response.body
end
end
end
Run this test once, and VCR will record the HTTP request to fixtures/vcr_cassettes/synopsis.yml
. 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) and accurate (the response will contain the same headers and body you get from a real request). You can use a different cassette library directory (e.g., “test/vcr_cassettes”).
NOTE: To avoid storing any sensitive information in cassettes, check out Filter Sensitive Data in the documentation.
Rails and Minitest:
Do not use ‘test/fixtures’ as the directory if you’re using Rails and Minitest (Rails will instead transitively load any files in that directory as models).
Features
The docs come in two flavors:
There is also a Railscast (from 2011), which will get you up and running in no-time http://railscasts.com/episodes/291-testing-with-vcr.
Release Policy
VCR follows the principles of semantic versioning. The API documentation defines VCR’s public API. Patch level releases contain only bug fixes. Minor releases contain backward-compatible new features. Major new releases contain backwards-incompatible changes to the public API.
Ruby Interpreter Compatibility
VCR versions 6.x are tested on the following ruby interpreters:
VCR 6.0.0 is the last version supporting >= 2.4. Upcoming releases will only explicitly support >= 2.6.
Development
yard server --reload
Ports in Other Languages
Related Projects
Similar Libraries in Ruby
Thanks also to the following people who have contributed patches or helpful suggestions:
Support us with a monthly donation and help us continue our activities. [Become a backer]
Become a sponsor and get your logo on our README on Github with a link to your site. [Become a sponsor]