Dynamic ERB Cassettes
VCR is designed to help you remove all HTTP dependencies from your test suite. To assist with this, VCR will cause an exception to be raised when an HTTP request is made while there is no cassette in use. The error is helpful to pinpoint where HTTP requests are made so you can use a VCR cassette at that point in your code.
If you want insight about how VCR attempted to handle the request, you can use the debug_logger configuration option to log more details.
If you want to allow an HTTP request to proceed as normal, you can set the allow_http_connections_when_no_cassette configuration option or you can temporarily turn VCR off:
VCR.turn_off!
=> turn VCR off so HTTP requests are allowed.Cassette insertions will trigger an error.
VCR.turn_off!(:ignore_cassettes => true)
=> turnVCR off and ignore cassette insertions (so that no error is raised).
VCR.turn_on!
=> turn VCR back onVCR.turned_off { ... }
=> turn VCR off for the duration of theprovided block.
Error for request when no cassette is in use
Given a file named "no_cassette_error.rb" with:
When I run ruby no_cassette_error.rb
Then it should fail with "An HTTP request has been made that VCR does not know how to handle".
Examples
configuration | http_lib |
c.hook_into :webmock | net/http |
c.hook_into :webmock | httpclient |
c.hook_into :webmock | curb |
c.hook_into :webmock | patron |
c.hook_into :webmock | em-http-request |
c.hook_into :webmock | typhoeus |
c.hook_into :typhoeus | typhoeus |
c.hook_into :excon | excon |
c.hook_into :faraday | faraday (w/ net_http) |
Temporarily turn VCR off to allow HTTP requests to procede as normal
Given a file named "turn_off_vcr.rb" with:
When I run ruby turn_off_vcr.rb
Then the output should contain:
And the output should contain:
And the output should contain:
And the output should contain:
Turning VCR off prevents cassettes from being inserted
Given a file named "turn_off_vcr_and_insert_cassette.rb" with:
When I run ruby turn_off_vcr_and_insert_cassette.rb
Then it should fail with "VCR is turned off. You must turn it on before you can insert a cassette.".
Turning VCR off with :ignore_cassettes => true
ignores cassettes
:ignore_cassettes => true
ignores cassettesGiven a file named "turn_off_vcr_and_insert_cassette.rb" with:
When I run ruby turn_off_vcr_and_insert_cassette.rb
Then it should pass with "Response: Hello"
And the file "cassettes/example.yml" should not exist.
Last updated