Skip to content

Commit

Permalink
Updated README.
Browse files Browse the repository at this point in the history
  • Loading branch information
myronmarston committed Mar 3, 2010
1 parent 1679d4f commit 20ec034
Showing 1 changed file with 61 additions and 23 deletions.
84 changes: 61 additions & 23 deletions README.rdoc
@@ -1,7 +1,7 @@
= VCR

VCR provides helpers to record HTTP requests for URIs that are not registered with fakeweb, and replay them later.
It provides built-in support for cucumber, but works with any ruby testing framework.
It works with any ruby testing framework and provides built-in support for cucumber.

== Installation

Expand All @@ -12,7 +12,10 @@ It provides built-in support for cucumber, but works with any ruby testing frame
This README assumes you are familiar with FakeWeb; if not, please checkout the {README}[http://github.com/chrisk/fakeweb/blob/master/README.rdoc].

VCR was inspired by {NetRecorder}[http://github.com/chrisyoung/netrecorder], but was designed from the ground up to support
localized recording and replaying, rather than the global recording and replaying of NetRecorder.
localized recording and replaying, rather than the global recording and replaying of NetRecorder. In general, I believe that
tests should not rely upon anything global. You're coupling your test to something outside of it that may later change and
break the test. It's far better to localize things to each test, as much as possible. VCR, in combination with FakeWeb,
makes it easy to do this with the recording and replying of HTTP responses.

== Cassettes

Expand All @@ -24,7 +27,7 @@ the following:
* It register these responses with fakeweb (depending on the cassette's :record option--see below)
* It sets the <tt>FakeWeb.allow_net_connect</tt> setting based on the cassette's :record option.

While a cassette is active, any HTTP requests to a URL of a previously recorded responses will use get
While a cassette is active, any HTTP requests to a URL of a previously recorded response will use
the recorded response. New HTTP requests (i.e. HTTP requests that have not been previously recorded)
will be recorded to the same cache file, depending on your :record option. When you destroy a cassette,
it does the following:
Expand All @@ -35,27 +38,29 @@ it does the following:

== Record modes

VCR supports 3 record modes, which configures when it records new responses. You can set a default
VCR supports 3 record modes, which configure when it records new responses. You can set a default
record mode in your configuration (see below) and a per-cassette record mode when creating a cassette. The record
modes are:

* :all - This will cause VCR to re-record all HTTP requests that occur while the cassette is the current one.
* <tt>:all</tt> - This will cause VCR to re-record all HTTP requests that occur.
When the cassette is created, it will not register any of the cached responses with fakeweb.
<tt>FakeWeb.allow_net_connect</tt> will be set to true, so it can record the requests.
* :none - This will prevent VCR from recording, or even allowing, any new HTTP requests while the cassette is the current one.
<tt>FakeWeb.allow_net_connect</tt> will be set to true, so it can record the requests. Use this
when you want to re-record all of the HTTP requests for a cassette. Alternately, you can simply
delete the corresponding cache file and use the :unregistered record mode, described below.
* <tt>:none</tt> - This will prevent VCR from recording, or even allowing, any new HTTP requests.
The previously recorded responses will be registered with fakeweb. <tt>FakeWeb.allow_net_connect</tt> will be set to
false, so that no new HTTP connections are allowed.
* :unregistered - This will use the previously recorded responses, and record any new requests that are not registered with
false, so that no new HTTP connections are allowed. Use this when you want to guarantee that no external
HTTP requests will be made while the given cassette is active. Fakeweb will raise an error in this case.
* <tt>:unregistered</tt> - This will use the previously recorded responses, and record any new requests that are not registered with
fakeweb. The previously recorded responses will be registered with fakeweb. <tt>FakeWeb.allow_net_connect</tt> will be
set to true, so that VCR will record any new HTTP requests within the cassette.

Note that :none and :unregistered will usually at the same. The difference is when your code changes and
it makes a new HTTP request that wasn't made when the cassette was first recorded. With :none, you would
get an error from FakeWeb (since allow_net_connect is set to false). With :unregistered, the new response
would get saved in the cassette's yaml file, and automatically get used in the future.
set to true, so that VCR will record any new HTTP requests within the cassette. Use this when it's ok for external HTTP
requests to be made without you explicitly allowing it. New requests will get saved to the cassettes yml file, and
automatically get used in the future.

== Configuration

require 'vcr'

# Set the default allow_net_connect option--usually you'll want this off.
# You don't usually want your test suite to make HTTP connections, do you?
FakeWeb.allow_net_connect = false
Expand All @@ -68,11 +73,12 @@ would get saved in the cassette's yaml file, and automatically get used in the f
c.default_cassette_record_mode = :none
end

This can go pretty much wherever, as long as this code is run before your tests, specs or scenarios.
This can go pretty much wherever, as long as this code is run before your tests, specs or scenarios. I tend
to put it in spec/support/vcr.rb, test/support/vcr.rb or features/support/vcr.rb.

== Usage with your favorite test/spec framework

VCR can easily be used with any ruby test/spec framework. Usually, you'll want to use <tt>VCR.with_cassette</tt>:
VCR can easily be used with any ruby test or spec framework. Usually, you'll want to use <tt>VCR.with_cassette</tt>:

VCR.with_cassette('geocoding/Seattle, WA', :record => :unregistered) do
# do something that causes an HTTP request.
Expand Down Expand Up @@ -100,25 +106,57 @@ cache dir. The :record setting is optional--if you leave it blank, your configu
VCR provides special support for cucumber. You can of course use <tt>VCR.with_cassette</tt> within a step definition,
and that's the recommended way for any of your step definitions. But many times I find myself using generic step definitions
provided by another library (such as the webrat/capybara web steps generated by cucumber-rails), and I don't want to modify
these. VCR provides cucumber tagging support to help in these cases:
these. VCR provides cucumber tagging support to help in these cases.

First, tag your scenario with something descriptive:

# in a cucumber feature file...
@facebook_http_request
Scenario: Sign up with facebook connect

# in features/support/vcr.rb (or some similar support file)
Then let VCR know about this tag, in features/support/vcr.rb (or some similar support file):

VCR.cucumber_tags do |t|
t.tags '@facebook_http_request', '@twitter_status_update', :record => :none
t.tags '@another_scenario_tag' # the default record mode will be used for this tag.
end

# Note: you'd probably also want to put your VCR config in this file (see above).

For each of the tags you specify in your cucumber_tags block, VCR will set up the appropriate
{Before and After hooks}[http://wiki.github.com/aslakhellesoy/cucumber/hooks] to use a cassette
for the entire scenario. The tag (minus the '@') will be used as the cassette name, and it'll
go in the cucumber_tags subdirectory of the configured cache dir.

== Suggested Workflow

First, configure VCR and FakeWeb as I have above. I like setting <tt>FakeWeb.allow_net_connect</tt> to <tt>false</tt>
and VCR's <tt>default_cassette_record_mode</tt> to <tt>:none</tt> so that no new HTTP requests are made without me
explicitly allowing it.

When an HTTP request is made, you'll get an {error from FakeWeb}[http://github.com/chrisk/fakeweb/blob/fakeweb-1.2.8/lib/fake_web/ext/net_http.rb#L62-63],
such as:

FakeWeb::NetConnectNotAllowedError: Real HTTP connections are disabled. Unregistered request: get http://example.com

Find the place that is making the HTTP request (the backtrace should help here). If you've already recorded this HTTP
request to a cassette from a different test, you can simply re-use the cassette. Use <tt>VCR.with_cassette</tt>, as
shown above. You may also want to refactor this into a helper method that sets up the VCR cassette and does whatever
makes the HTTP request:

def set_user_address(user, address, city, state)
VCR.with_cassette("geocoding/#{address}, #{city}, #{state}", :record => :unregistered) do
user.address.update_attributes!(:address => address, :city => city, :state => state)
end
end

In this case, I've used a dynamic cassette name based on the address being geocoded. That way, each separate address
gets a different cassette, and tests that set the same user address will reuse the same cassette. I've also set
the record mode to <tt>:unregistered</tt> so that VCR will automatically record geocoding requests for a new address
to a new cassette, without me having to do anything.

If the HTTP request that triggered the error is new, you'll have to record it for the first time. Simply use <tt>VCR.with_cassette</tt>
with the record mode set to <tt>:unregistered</tt> or <tt>:all</tt>. Run the test again, and VCR will record the HTTP response. I usually
remove the record mode at this point so that it uses the default of <tt>:none</tt> in the future. Future test runs will use the
recorded response, and if your code changes so that it is making a new HTTP request, you'll get the same FakeWeb error as shown above.

== Ruby Version Compatibility

specs.should pass if RUBY_VERSION =~ /^1.(8.6|8.7|9.1)$/
Expand All @@ -130,7 +168,7 @@ go in the cucumber_tags subdirectory of the configured cache dir.
before the cassette reads or writes to the file.
* You can use a directory separator (i.e. '/') in your cassette names to cause it to use a subdirectory
of the cache_dir. The cucumber tagging support uses this.
* VCR maintains a simple stack of cassette. This allows you to nest them as deeply as you want.
* VCR maintains a simple stack of cassettes. This allows you to nest them as deeply as you want.
This is particularly useful when you have a cucumber step definition that uses a cassette, and
you also want to use a cassette for the entire scenario using the tagging support.

Expand Down

0 comments on commit 20ec034

Please sign in to comment.