Search code examples
rubycucumbervcr

Recycle recorded vcr cassettes in similar scenario OR tell vcr to use specific cassette for a scenario


i have to extend an exsiting cucumber feature with some new scenarios covering some new behaviour. At the moment this whole cucumber/vcr thingy is set up as such that it will look for cassettes with matching scenario name:

feature bar

  @vcr
  scenario foo

will look for a recorded cassette in ./cassettes/bar/foo.yml. But there are further scenarios which could recycle the before recorded cassette. (The ones to add from my side too). So there is a folder bloated with the same cassette for each scenario but only renamed:

feature bar

  @vcr
  scenario foo

  @vcr
  scenario foo1

  ...

  @vcr
  scenario fooX

So were is ./cassettes/bar/foo.yml, ./cassettes/bar/foo1.yml ... ./cassettes/bar/fooX.yml with exact same content. What is the proper way to get this dry?

Is there a possibility to specify the vcr cassette to be used explicitly, something like?

  @vcr(:cassette => foo)
  scenario fooX

many thanks ;)


Solution

  • I solved this issue by defining a new tag for VCR:

    #./test_helper/vcr.rb
    VCR.cucumber_tags do |t|
      t.tag '@vcr', use_scenario_name: true
      t.tag '@foo'
    end
    

    And then i added a before and after hook, which loads my wanted cassette:

    # ./features/support/env.rb
    Before('@foo') do
      VCR.insert_cassette('cassettes/bar/foo')
    end
    
    After('@foo') do
      VCR.eject_cassette
    end
    

    So now i simply needed to annotate my new defined tag for the similar scenarios:

    feature bar
    
      @foo
      scenario foo
    
      @foo
      scenario foo1
    
      ...
    
      @foo
      scenario fooX
    

    So now i only have one recorded cassette file left.