I am using VCR to record http interactions with an external API. This is my (working) code:
it 'does xyz....' do
hook_payload = {ruby_hash: 'here'}
VCR.use_cassette('my_folder/create_project') do
VCR.use_cassette('my_folder/get_project') do
update = Update.new(hook_payload)
expect { update.call }.to change { Projects.count }.by(1)
end
end
end
The code above works, but the organization isn't good, as I prefer to have the expect{}
call outside the block. So, I tried this, but the following code does not work:
context 'my context', vcr: { group: 'my_folder', cassettes: %w[create_project get_project] } do
it 'does xyz....' do
hook_payload = {ruby_hash: 'here'}
update = Update.new(hook_payload)
expect { update.call }.to change { Projects.count }.by(1)
end
However this code doesn't work and I get the following error:
VCR is currently using the following cassette: - /Users/me/this_project/spec/fixtures/vcr/my_folder/create_project.yml.
Under the current configuration VCR can not find a suitable HTTP interaction to replay and is prevented from recording new requests.
I am 100% sure that my_folder/get_project.yml
is valid, and it works in other tests in the project.
I even put the cassettes (%w[create_project get_project]
) in the same order that they are used in my code. What am I doing incorrectly here?
It's recommended to use the block but there are options if you don't want to do that.
https://www.rubydoc.info/github/vcr/vcr/VCR:insert_cassette
Try using VCR.insert_cassette
which does not require a block. You'll need to call eject_cassette
in your tear downs. I tried it and it worked. The ordering doesn't matter.
it 'does xyz....' do
hook_payload = {ruby_hash: 'here'}
VCR.insert_cassette('my_folder/create_project')
VCR.insert_cassette('my_folder/get_project')
update = Update.new(hook_payload)
expect { update.call }.to change { Projects.count }.by(1)
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With