Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 117 lines (98 sloc) 5.231 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
Feature: Usage with Cucumber

VCR can be used with cucumber in two basic ways:

- Use `VCR.use_cassette` in a step definition.
- Use a `VCR.cucumber_tags` block to tell VCR to use a
cassette for a tagged scenario.

In a cucumber support file (e.g. features/support/vcr.rb), put code like this:

``` ruby
VCR.cucumber_tags do |t|
t.tag '@tag1'
t.tags '@tag2', '@tag3'

t.tag '@tag3', :cassette => :options
t.tags '@tag4, '@tag5', :cassette => :options
end
```

VCR will use a cassette named "cucumber_tags/<tag_name>" for scenarios
with each of these tags. The configured `default_cassette_options` will
be used, or you can override specific options by passing a hash as the
last argument to `#tag` or `#tags`.

Scenario: Record HTTP interactions in a scenario by tagging it
Given a file named "lib/server.rb" with:
"""ruby
if ENV['WITH_SERVER'] == 'true'
start_sinatra_app(:port => 7777) do
get('/:path') { "Hello #{params[:path]}" }
end
end
"""

Given a file named "features/support/vcr.rb" with:
"""ruby
require "lib/server"
require 'vcr'

VCR.configure do |c|
c.hook_into :fakeweb
c.cassette_library_dir = 'features/cassettes'
end

VCR.cucumber_tags do |t|
t.tag '@localhost_request' # uses default record mode since no options are given
t.tags '@disallowed_1', '@disallowed_2', :record => :none
end
"""
And a file named "features/step_definitions/steps.rb" with:
"""ruby
require 'net/http'

When /^a request is made to "([^"]*)"$/ do |url|
@response = Net::HTTP.get_response(URI.parse(url))
end

When /^(.*) within a cassette named "([^"]*)"$/ do |step, cassette_name|
VCR.use_cassette(cassette_name) { When step }
end

Then /^the response should be "([^"]*)"$/ do |expected_response|
@response.body.should == expected_response
end
"""
And a file named "features/vcr_example.feature" with:
"""
Feature: VCR example

@localhost_request
Scenario: tagged scenario
When a request is made to "http://localhost:7777/localhost_request_1"
Then the response should be "Hello localhost_request_1"
When a request is made to "http://localhost:7777/nested_cassette" within a cassette named "nested_cassette"
Then the response should be "Hello nested_cassette"
When a request is made to "http://localhost:7777/localhost_request_2"
Then the response should be "Hello localhost_request_2"

@disallowed_1
Scenario: tagged scenario
When a request is made to "http://localhost:7777/allowed" within a cassette named "allowed"
Then the response should be "Hello allowed"
When a request is made to "http://localhost:7777/disallowed_1"

@disallowed_2
Scenario: tagged scenario
When a request is made to "http://localhost:7777/disallowed_2"
"""
And the directory "features/cassettes" does not exist
When I run `cucumber WITH_SERVER=true features/vcr_example.feature`
Then it should fail with "3 scenarios (2 failed, 1 passed)"
And the output should contain each of the following:
| An HTTP request has been made that VCR does not know how to handle: |
| GET http://localhost:7777/disallowed_1 |
| An HTTP request has been made that VCR does not know how to handle: |
| GET http://localhost:7777/disallowed_2 |
And the file "features/cassettes/cucumber_tags/localhost_request.yml" should contain "body: Hello localhost_request_1"
And the file "features/cassettes/cucumber_tags/localhost_request.yml" should contain "body: Hello localhost_request_2"
And the file "features/cassettes/nested_cassette.yml" should contain "body: Hello nested_cassette"
And the file "features/cassettes/allowed.yml" should contain "body: Hello allowed"

# Run again without the server; we'll get the same responses because VCR
# will replay the recorded responses.
When I run `cucumber features/vcr_example.feature`
Then it should fail with "3 scenarios (2 failed, 1 passed)"
And the output should contain each of the following:
| An HTTP request has been made that VCR does not know how to handle: |
| GET http://localhost:7777/disallowed_1 |
| An HTTP request has been made that VCR does not know how to handle: |
| GET http://localhost:7777/disallowed_2 |
And the file "features/cassettes/cucumber_tags/localhost_request.yml" should contain "body: Hello localhost_request_1"
And the file "features/cassettes/cucumber_tags/localhost_request.yml" should contain "body: Hello localhost_request_2"
And the file "features/cassettes/nested_cassette.yml" should contain "body: Hello nested_cassette"
And the file "features/cassettes/allowed.yml" should contain "body: Hello allowed"

Something went wrong with that request. Please try again.