Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using multiple cassettes with VCR in one test

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?

like image 721
jjjjjjjj Avatar asked Jun 21 '18 04:06

jjjjjjjj


1 Answers

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


like image 164
Rimian Avatar answered Oct 04 '22 20:10

Rimian