Skip to content

Commit

Permalink
Use RSpec metadata value as cassette name if value is String
Browse files Browse the repository at this point in the history
  • Loading branch information
evansagge authored and Evan Sagge committed Oct 27, 2019
1 parent cfa2924 commit e468239
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/vcr/test_frameworks/rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,19 @@ def configure!
config.before(:each, when_tagged_with_vcr) do |ex|
example = ex.respond_to?(:metadata) ? ex : ex.example

cassette_name = nil
options = example.metadata[:vcr]
options = options.is_a?(Hash) ? options.dup : {} # in case it's just :vcr => true

cassette_name = options.delete(:cassette_name) ||
options = case options
when Hash #=> vcr: { cassette_name: 'foo' }
options.dup
when String #=> vcr: 'bar'
cassette_name = options.dup
{}
else #=> :vcr or vcr: true
{}
end

cassette_name ||= options.delete(:cassette_name) ||
vcr_cassette_name_for[example.metadata]
VCR.insert_cassette(cassette_name, options)
end
Expand Down
12 changes: 12 additions & 0 deletions spec/lib/vcr/test_frameworks/rspec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@
end
end

context 'when the metadata value is a string', vcr: 'foo' do
it 'uses that string as the cassette name' do
expect(VCR.current_cassette.name).to eq('foo')
end

context 'when defined on the example level' do
it 'uses that string as the cassette name', vcr: 'bar' do
expect(VCR.current_cassette.name).to eq('bar')
end
end
end

it 'allows the cassette name to be overriden', :vcr => { :cassette_name => 'foo' } do
expect(VCR.current_cassette.name).to eq('foo')
end
Expand Down

0 comments on commit e468239

Please sign in to comment.