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

3

18

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?

Maclay answered 12/10, 2011 at 16:26 Comment(1)
github.com/rspec/rspec-core/issues/6 might suggest that the functionality does not exist? Or perhaps just setting metadata in before(:all) was infeasible?Maclay
E
18

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.

Ebby answered 11/8, 2012 at 11:32 Comment(4)
Cool. I'm not using rspec on my current project, so if someone else can confirm this works for them then I'll accept the answer.Maclay
Or ... ;) you could simply save the example, run gem install rspec and execute the spec via rspec example_spec.rb.Ebby
Hah! I'll try. I'll need to grab a computer with Ruby installed on it, but I think I have one. If I end up futzing with my environment I may give up though. =)Maclay
The method example has been deprecated and then removed in current rspec versions. It's now RSpec.current_example.Lubra
A
3

This is not exactly answering the original question, but it is related and this was the first post related to my Google search, so I'd like to share what I just figured out.

In my case, I was looking for a way to run some commands in a before(:suite)/before(:all), but only if the tests being run included some system tests (aka examples with certain metadata). Here's what I came up with:

RSpec.configure do |config|
  config.before(:suite) do
    examples = RSpec.world.filtered_examples.values.flatten
    has_system_tests = examples.any? { |example| example.metadata[:type] == :system }

    if has_system_tests
      ...
    end
  end
end

Aftergrowth answered 3/4, 2019 at 20:11 Comment(0)
D
2

With rspec-core 3.7 you can simply access metadata the second argument of before

RSpec.configure do |config|
  config.before(:all, type: :job) do 
    # do what you want to do
  end
end
Dreiser answered 14/1, 2020 at 19:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.