Usage With Cucumber
VCR can be used with cucumber in two basic ways:
Use
VCR.use_cassettein a step definition.Use a
VCR.cucumber_tagsblock 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:
VCR.cucumber_tags do |t| t.tag '@tag1' t.tags '@tag2', '@tag3' t.tag '@tag3', :cassette => :options t.tags '@tag4', '@tag5', :cassette => :options t.tag '@vcr', :use_scenario_name => true endVCR will use a cassette named
cucumber_tags/<tag_name>for scenarios with each of these tags (Unless the:use_scenario_nameoption is provided. See below). The configureddefault_cassette_optionswill be used, or you can override specific options by passing a hash as the last argument to#tagor#tags.You can also have VCR name your cassettes automatically according to the feature and scenario name by providing
:use_scenario_name => trueto#tagor#tags. In this case, the cassette will be named<feature_name>/<scenario_name>. For scenario outlines, VCR will record one cassette per row, and the cassettes will be named<feature_name>/<scenario_name>/<row_name>.
Record HTTP interactions in a scenario by tagging it
Given a file named "lib/server.rb" with:
if ENV['WITH_SERVER'] == 'true'
$server = start_sinatra_app do
get('/:path') { "Hello #{params[:path]}" }
end
endGiven a file named "features/support/vcr.rb" with:
And a file named "features/step_definitions/steps.rb" with:
And a file named "features/vcr_example.feature" with:
And the directory "features/cassettes" does not exist
When I run cucumber WITH_SERVER=true features/vcr_example.feature
Then it should fail with "5 scenarios (2 failed, 3 passed)"
And the file "features/cassettes/cucumber_tags/localhost_request.yml" should contain "Hello localhost_request_1"
And the file "features/cassettes/cucumber_tags/localhost_request.yml" should contain "Hello localhost_request_2"
And the file "features/cassettes/nested_cassette.yml" should contain "Hello nested_cassette"
And the file "features/cassettes/allowed.yml" should contain "Hello allowed"
And the file "features/cassettes/VCR_example/tagged_scenario.yml" should contain "Hello localhost_request_1"
And the file "features/cassettes/VCRexample/tagged_scenario_outline/_foo_bar.yml" should contain "Hello localhost_request_1"
When I run cucumber features/vcr_example.feature
Then it should fail with "5 scenarios (2 failed, 3 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 "Hello localhost_request_1"
And the file "features/cassettes/cucumber_tags/localhost_request.yml" should contain "Hello localhost_request_2"
And the file "features/cassettes/nested_cassette.yml" should contain "Hello nested_cassette"
And the file "features/cassettes/allowed.yml" should contain "Hello allowed"
And the file "features/cassettes/VCR_example/tagged_scenario.yml" should contain "Hello localhost_request_1"
And the file "features/cassettes/VCRexample/tagged_scenario_outline/_foo_bar.yml" should contain "Hello localhost_request_1".
:allow_unused_http_interactions => false does not raise if the scenario already failed
:allow_unused_http_interactions => false does not raise if the scenario already failedGiven a previously recorded cassette file "features/cassettes/cucumber_tags/example.yml" with:
And a file named "features/support/vcr.rb" with:
And a file named "features/step_definitions/steps.rb" with:
And a file named "features/vcr_example.feature" with:
When I run cucumber features/vcr_example.feature
Then it should fail with "1 scenario (1 failed)"
And the output should contain "boom"
And the output should not contain "There are unused HTTP interactions".
Last updated
Was this helpful?