Module: VCR::RSpec::Metadata

Extended by:
Metadata
Included in:
Metadata
Defined in:
lib/vcr/test_frameworks/rspec.rb

Instance Method Summary collapse

Instance Method Details

#configure!Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/vcr/test_frameworks/rspec.rb', line 8

def configure!
  ::RSpec.configure do |config|
    vcr_cassette_name_for = lambda do ||
      description = [:description]
      example_group = if .key?(:example_group)
                        [:example_group]
                      else
                        [:parent_example_group]
                      end

      if example_group
        [vcr_cassette_name_for[example_group], description].join('/')
      else
        description
      end
    end

    when_tagged_with_vcr = { :vcr => lambda { |v| !!v } }

    config.before(:each, when_tagged_with_vcr) do |ex|
      example = ex.respond_to?(:metadata) ? ex : ex.example

      options = example.[:vcr]
      options = options.is_a?(Hash) ? options.dup : {} # in case it's just :vcr => true

      cassette_name = options.delete(:cassette_name) ||
                      vcr_cassette_name_for[example.]
      VCR.insert_cassette(cassette_name, options)
    end

    config.after(:each, when_tagged_with_vcr) do |ex|
      example = ex.respond_to?(:metadata) ? ex : ex.example
      VCR.eject_cassette(:skip_no_unused_interactions_assertion => !!example.exception)
    end
  end
end