Jirapong
Jirapong

Reputation: 24256

How to get current context name of RSpec?

if I have rspec like this

describe 'Foo' do
 # init go here
 describe 'Sub-Foo' do
    it "should Bar" do
       # test go here
       # puts ... <-- need "Foo.Sub-Foo should Bar" here 
    end
 end
end

How can I get "Foo.Sub-Foo should Bar" inside the test context at // test go here?

It is similar to format with specdocs, but how to get it inside itself?

Upvotes: 20

Views: 5491

Answers (1)

mtyaka
mtyaka

Reputation: 8848

RSpec.describe 'Foo' do
  describe 'Sub-Foo' do
    # NOTE: `self.` context is also available within subject { } block

    it 'should Bar' do |example|
      expect(self.class.description).to eq('Sub-Foo')
      expect(example.description).to eq('should Bar')

      expect(self.class.top_level_description).to eq('Foo')
    end
  end
end

Upvotes: 27

Related Questions