8

I'm adding an rspec hook that will allow me to switch on vcr and use the name of the current example as the cassette name.

it "should have collaborators", :vcr => :once do
  # web interactions
end

config.around(:each, :vcr => :once) do |example|
  VCR.use_cassette(example.name, :record => :once) do
    example.call
  end
end

trouble is I don't know how to get the name of the current example (example.name doesn't work).

Myron Marston
  • 21,452
  • 5
  • 64
  • 63
opsb
  • 29,325
  • 19
  • 89
  • 99

2 Answers2

13

RSpec defines a metadata method that returns a hash with some useful information about the example. You might try:

example.metadata[:full_description]

which should return the group(s) and example name, concatenated into one string.

zetetic
  • 47,184
  • 10
  • 111
  • 119
  • Yes this is a much better way of doing it. As it turns out my solution works better for me (I like having the directory structure for descriptions) but this is the best answer for the original question. – opsb Mar 08 '11 at 07:22
1

This seems a bit fiddly but it does the job

  config.before(:each, :vcr => :once) do
    group_descriptions = self.example.example_group.ancestors.map(&:description)
    spec_name = [*group_descriptions.reverse, self.example.description].join("/")
    VCR.insert_cassette(spec_name, :record => :once)
  end

  config.after(:each, :vcr => :once) do
    VCR.eject_cassette
  end
opsb
  • 29,325
  • 19
  • 89
  • 99