Skip to content

Commit

Permalink
add error message when cassete file does not exist (#1028)
Browse files Browse the repository at this point in the history
* add error message when cassete file does not exist

* add tests for all none error messages

---------

Co-authored-by: Paulo Vilarinho <paulotarsobranco@gmail.com >
  • Loading branch information
PauloVilarinho and Paulo Vilarinho authored Aug 4, 2024
1 parent aff1309 commit 9d82a03
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
18 changes: 17 additions & 1 deletion lib/vcr/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@ def format_foot_note(url, index)
"https://benoittgt.github.io/vcr/?v=%s#/record_modes/none"
],

:none_without_file => [
["The current record mode (:none) does not allow requests to be recorded.",
"One or more cassette names registered was not found. Use ",
":new_episodes or :once record modes to record a new cassette"],
"https://benoittgt.github.io/vcr/?v=%s#/record_modes/none"
],


:use_a_cassette => [
["If you want VCR to record this request and play it back during future test",
"runs, you should wrap your test (or this portion of your test) in a",
Expand Down Expand Up @@ -284,14 +292,22 @@ def record_mode_suggestion
record_modes = current_cassettes.map(&:record_mode)

if record_modes.all?{|r| r == :none }
[:deal_with_none]
none_suggestion
elsif record_modes.all?{|r| r == :once }
[:delete_cassette_for_once]
else
[]
end
end

def none_suggestion
if current_cassettes.any? {|c| !File.exist?(c.file) }
[:none_without_file]
else
[:deal_with_none]
end
end

def has_used_interaction_matching?
current_cassettes.any?{|c| c.http_interactions.has_used_interaction_matching?(request) }
end
Expand Down
15 changes: 15 additions & 0 deletions spec/lib/vcr/errors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,21 @@ def request_with(options)
end
end

it 'mentions that one of the files does not exist when using :none and file does not exist' do
VCR.use_cassette('example', :record => :none) do
expect(message).to include('One or more cassette names registered was not found.')
end
end

it 'mentions legacy message when file exists' do
VCR.configure do |c|
c.cassette_library_dir = File.join(VCR::SPEC_ROOT, 'fixtures')
end
VCR.use_cassette('fake_example_responses', :record => :none) do
expect(message).to include('can temporarily change the record mode to :once, delete the cassette file')
end
end

it 'does not mention the :once or :none record modes if using the :new_episodes record mode' do
VCR.use_cassette('example', :record => :new_episodes) do
expect(message).not_to include(':once', ':none')
Expand Down

0 comments on commit 9d82a03

Please sign in to comment.