forked from vcr/vcr
/
no_cassette.feature
152 lines (125 loc) · 4.67 KB
/
no_cassette.feature
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
Feature: Error for HTTP request made when no cassette is in use
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 to allow an HTTP request to proceed as normal, you can
set the `allow_http_connections_when_no_cassette` configuration option
(see configuration/allow_http_connections_when_no_cassette.feature) 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)` => turn
VCR off and ignore cassette insertions (so that no error is raised).
- `VCR.turn_on!` => turn VCR back on
- `VCR.turned_off { ... }` => turn VCR off for the duration of the
provided block.
Scenario Outline: Error for request when no cassette is in use
Given a file named "no_cassette_error.rb" with:
"""ruby
require 'vcr_cucumber_helpers'
include_http_adapter_for("<http_lib>")
require 'vcr'
VCR.configure do |c|
c.stub_with <stub_with>
c.cassette_library_dir = 'cassettes'
end
response_body_for(:get, 'http://example.com/')
"""
When I run `ruby no_cassette_error.rb`
Then it should fail with "Real HTTP connections are disabled"
And the output should contain each of the following:
| You can use VCR to automatically record this request and replay it later. |
| no_cassette_error.rb:11 |
Examples:
| stub_with | http_lib |
| :fakeweb | net/http |
| :webmock | net/http |
| :webmock | httpclient |
| :webmock | curb |
| :webmock | patron |
| :webmock | em-http-request |
| :webmock | typhoeus |
| :typhoeus | typhoeus |
| :excon | excon |
Scenario: Temporarily turn VCR off to allow HTTP requests to procede as normal
Given a file named "turn_off_vcr.rb" with:
"""ruby
require 'vcr_cucumber_helpers'
start_sinatra_app(:port => 7777) do
get('/') { 'Hello' }
end
require 'vcr'
VCR.configure do |c|
c.stub_with :fakeweb
end
def make_request(context)
puts context
puts Net::HTTP.get_response('localhost', '/', 7777).body
rescue => e
puts "Error: #{e.message}"
end
VCR.turned_off do
make_request "In VCR.turned_off block"
end
make_request "Outside of VCR.turned_off block"
VCR.turn_off!
make_request "After calling VCR.turn_off!"
VCR.turn_on!
make_request "After calling VCR.turn_on!"
"""
When I run `ruby turn_off_vcr.rb`
Then the output should contain:
"""
In VCR.turned_off block
Hello
"""
And the output should contain:
"""
Outside of VCR.turned_off block
Error: Real HTTP connections are disabled.
"""
And the output should contain:
"""
After calling VCR.turn_off!
Hello
"""
And the output should contain:
"""
After calling VCR.turn_on!
Error: Real HTTP connections are disabled.
"""
Scenario: Turning VCR off prevents cassettes from being inserted
Given a file named "turn_off_vcr_and_insert_cassette.rb" with:
"""ruby
require 'vcr'
VCR.configure do |c|
c.stub_with :fakeweb
end
VCR.turn_off!
VCR.insert_cassette('example')
"""
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."
Scenario: Turning VCR off with `:ignore_cassettes => true` ignores cassettes
Given a file named "turn_off_vcr_and_insert_cassette.rb" with:
"""ruby
require 'vcr_cucumber_helpers'
start_sinatra_app(:port => 7777) do
get('/') { 'Hello' }
end
require 'vcr'
VCR.configure do |c|
c.cassette_library_dir = 'cassettes'
c.stub_with :fakeweb
end
VCR.turn_off!(:ignore_cassettes => true)
VCR.use_cassette('example') do
response = Net::HTTP.get_response('localhost', '/', 7777).body
puts "Response: #{response}"
end
"""
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