Getting the full RSpec test name from within a before(:each) block

Viewed 10919

RSpec allows you to get the current running test method name in a before(:each) block, by doing the following:

Spec::Runner.configure do |config|
  config.before :each do |x|
    x.method_name # returns 'should be cool'
  end
end

This is for a test like:

require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe 'Hello world' do
  it 'should be cool' do
    # test code
  end 
end

Would it be possible to get the whole test name with what it's describing, (a.k.a. 'Hello World should be cool') in the before block?

8 Answers

This works in rspec 3.5

example.metadata[:full_description]

Fuller example, of how to access it:

  subject(:example_description) do |example|
    example.metadata[:full_description]
  end
Related