Skip to content

Commit

Permalink
add VCR.turned_on (#835)
Browse files Browse the repository at this point in the history
Co-authored-by: Kenton Hirowatari <kenton@hirowatari.com>
  • Loading branch information
hirowatari and hirowatari committed Oct 2, 2020
1 parent 60aaecc commit 570e1d9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
11 changes: 10 additions & 1 deletion features/cassettes/no_cassette.feature
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Feature: Error for HTTP request made when no cassette is in use
| c.hook_into :excon | excon |
| c.hook_into :faraday | faraday (w/ net_http) |

Scenario: Temporarily turn VCR off to allow HTTP requests to procede as normal
Scenario: Temporarily turn VCR off to allow HTTP requests to proceed as normal
Given a file named "turn_off_vcr.rb" with:
"""ruby
$server = start_sinatra_app do
Expand Down Expand Up @@ -82,6 +82,10 @@ Feature: Error for HTTP request made when no cassette is in use
VCR.turn_off!
make_request "After calling VCR.turn_off!"
VCR.turned_on do
make_request "In VCR.turned_on block"
end
VCR.turn_on!
make_request "After calling VCR.turn_on!"
"""
Expand All @@ -101,6 +105,11 @@ Feature: Error for HTTP request made when no cassette is in use
After calling VCR.turn_off!
Hello
"""
And the output should contain:
"""
In VCR.turned_on block
Error: VCR::Errors::UnhandledHTTPRequestError
"""
And the output should contain:
"""
After calling VCR.turn_on!
Expand Down
18 changes: 18 additions & 0 deletions lib/vcr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ def cucumber_tags(&block)
# @see #turn_off!
# @see #turn_on!
# @see #turned_on?
# @see #turned_on
def turned_off(options = {})
turn_off!(options)

Expand Down Expand Up @@ -301,11 +302,28 @@ def turn_off!(options = {})
set_context_value(:turned_off, true)
end

# Turns on VCR, for the duration of a block.
# @param (see #turn_off!)
# @return [void]
# @see #turn_off!
# @see #turned_off
# @see #turned_on?
def turned_on(options = {})
turn_on!

begin
yield
ensure
turn_off!(options)
end
end

# Turns on VCR, if it has previously been turned off.
# @return [void]
# @see #turn_off!
# @see #turned_off
# @see #turned_on?
# @see #turned_on
def turn_on!
set_context_value(:turned_off, false)
end
Expand Down
22 changes: 22 additions & 0 deletions spec/lib/vcr_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,28 @@ def insert_cassette(name = :cassette_test)
end
end

describe '.turned_on' do
before { VCR.turn_off! }

it 'yields with VCR turned on' do
expect(VCR).not_to be_turned_on
yielded = false

VCR.turned_on do
yielded = true
expect(VCR).to be_turned_on
end

expect(yielded).to eq(true)
expect(VCR).not_to be_turned_on
end

it 'passes options through to .turn_off!' do
expect(VCR).to receive(:turn_off!).with(:ignore_cassettes => true)
VCR.turned_on(:ignore_cassettes => true) { }
end
end

describe '.turned_on?' do
it 'is on by default' do
expect(VCR).to be_turned_on
Expand Down

0 comments on commit 570e1d9

Please sign in to comment.