Search code examples
rubyrspec2

How can I access metadata in rspec before(:all)?


I would like to be able to display a test group name (and ancestry) during the before(:all) method:

describe "My awesome app" do
  before(:all) do
    puts running_example_group.metadata[:full_description] # <- what I'm imagining
    ...
  done
  ...
  describe "awesome widget" do
    before (:all) do
      puts running_example_group.metadata[:full_description] # <- what I'm imagining
      ...
    done
    ...
  done
done

The idea is that would produce the output:

My awesome app
My awesome app awesome widget

This data is available inside "it" clauses, but I can't figure it out for before(:all). Is it not available? Am I just making a dumb mistake?


Solution

  • Inside a before(:all) block, there is no "running example", but you can still access the metadata through the RSpec::Core::ExampleGroup. Here's an example of how you can access the metadata from various scopes:

    describe "My app", js: true do
    
      context "with js set to #{metadata[:js]}" do
        before :all do
          puts "in before block: js is set to #{self.class.metadata[:js]}"
        end
    
        it "works" do
          puts "in example: js is set to #{example.metadata[:js]}"
        end
      end
    
    end
    

    For more information, please take a look at this comment in rspec/rspec-core#42.