Ruby Weekly is a weekly newsletter covering the latest Ruby and Rails news.

VCR: A Recorder For All Your Tests’ HTTP Interactions

By Peter Cooper / January 4, 2011

haha-funny.pngVCR is a library by Myron Marston that records your test suite's HTTP interactions so that they can be quickly replayed during future test runs. The big win is that you get predictable, quick and accurate tests. If you need to update the data, just delete the fixtures VCR generates and you're good to go.

On the surface, VCR sounds like it copies the work of libraries like FakeWeb and Webmock, but VCR uses those libraries to provide a cleaner, more abstracted experience. VCR supports the mocking features of FakeWeb, Webmock, Typhoeus and Faraday out of the box, and further, supports multiple HTTP client libraries including Net::HTTP, Typhoeus, HTTPClient, Curb, Patron, em-http-request and Net::HTTP-based clients like HTTParty, RESTClient and Mechanize.

You can learn more at the VCR GitHub page (it has a good, basic README) or give it a try right now with the following:

sudo gem install webmock vcr

And then run the following code:

require 'rubygems'
require 'test/unit'
require 'vcr'

VCR.config do |c|
  c.cassette_library_dir = 'fixtures/vcr_cassettes'
  c.stub_with :webmock # or :fakeweb
end

class VCRTest <  Test::Unit::TestCase
  def test_example_dot_com
    VCR.use_cassette('synopsis', :record => :n

Comments

  1. Myron Marston says:

    Thanks, Peter :).

    It's worth mentioning that for the code example above to work on ruby 1.8.x, you'll need to add "require 'rubygems'" at the top.

  2. Peter Cooper says:

    I think most people will get that but good call.. I moved to 1.9.2 for almost everything already. One of the next big overarching topics I want to cover here on RI actually - not enough people are on 1.9 ;-)

Other Posts to Enjoy

Twitter Mentions