Search code examples
rspecfilterhookrspec2vcr

Name of current example/group in rspec hook


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).


Solution

  • 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.