before_playback hook

The before_playback hook is called before a cassette sets up its stubs for playback.

Your block should accept up to 2 arguments. The first argument will be the HTTP interaction that is about to be used for play back. The second argument will be the current cassette.

You can also call #ignore! on the HTTP interaction to prevent VCR from playing it back.

You can use tags to specify a cassette, otherwise your hook will apply to all cassettes. Consider this code:

  VCR.configure do |c|
    c.before_playback(:twitter) { ... } # modify the interactions somehow
  end

  VCR.use_cassette('cassette_1', :tag => :twitter) { ... }
  VCR.use_cassette('cassette_2') { ... }

In this example, the hook would apply to the first cassette but not the second cassette.

Background ()

Given a previously recorded cassette file "cassettes/example.yml" with:

---
http_interactions:
- request:
    method: get
    uri: http://localhost:7777/
    body:
      encoding: UTF-8
      string: ""
    headers: {}
  response:
    status:
      code: 200
      message: OK
    headers:
      Content-Type:
      - text/html;charset=utf-8
      Content-Length:
      - "20"
    body:
      encoding: UTF-8
      string: previously recorded response
    http_version: "1.1"
  recorded_at: Tue, 01 Nov 2011 04:58:44 GMT
recorded_with: VCR 2.0.0

Modify played back response

Given a file named "before_playback_example.rb" with:

When I run ruby before_playback_example.rb

Then it should pass with "Response: response from before_playback".

Modify played back response based on the cassette

Given a file named "before_playback_example.rb" with:

When I run ruby before_playback_example.rb

Then it should pass with "Response: response for example cassette".

Prevent playback by ignoring interaction in before_playback hook

Given a file named "before_playback_ignore.rb" with:

When I run ruby before_playback_ignore.rb

Then it should pass with "Response: sinatra response".

Multiple hooks are run in order

Given a file named "multiple_hooks.rb" with:

When I run ruby multiple_hooks.rb

Then it should pass with:

Use tagging to apply hooks to only certain cassettes

Given a file named "tagged_hooks.rb" with:

When I run ruby tagged_hooks.rb

Then it should pass with:

Last updated

Was this helpful?