← Back to Upcase

Rspec extracting before and after calls


(Alireza ) #1

I have couple of tests inside which I have before and after blocks to save and eject HTTP calls using VCR gem:

  describe "story" do    
    before do
      VCR.insert_cassette 'story', :record => :new_episodes
    end    
    after do
      VCR.eject_cassette
    end
   ......
end

I have repeated this inside other test blocks, each time calling VCR.insert_cassette, with a different parameter (ex. ‘comments’). what is the best way to refactor this code in order to extract the before and after calls ?

you can access the source file on github


(Ben Orenstein) #2

I probably wouldn’t.

I might put the eject_cassette call in a global after block scoped to feature specs (rspec lets you define an after for a certain subset of specs in its config).

As for the insert_cassette, I don’t see what would be gained by an extraction.


(Alireza ) #3

Thanks, the reason was to DRY it, since I’m repeating almost the same before and after block for each external API call I’m testing.