VCR 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 => :new_episodes) do
response = Net::HTTP.get_response(URI.parse('http://example.com/'))
assert_match /You have reached this web page by typing.*example\.com/, response.body
end
end
end
The first run will fetch example.com and save the completed response into a fixtures file generated at ./fixtures/vcr_cassettes/synopsis.yml. Future runs will then look for that fixture file and use the earlier response if present.
[ad] New Relic is the #1 on-demand application performance management app in the Ruby world and now they've expanded to PHP. Yep, you can now ensure the health and availability of your PHP apps too and the RPM Lite package is free.