Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ruby

rspec2

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?

like image 392
Ben Flynn Avatar asked Oct 12 '11 16:10

Ben Flynn


1 Answers

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.

like image 173
rubiii Avatar answered Nov 01 '22 12:11

rubiii