Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 310
Jirapong Avatar asked Oct 05 '09 04:10

Jirapong


1 Answers

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
like image 77
mtyaka Avatar answered Oct 21 '22 19:10

mtyaka