views:

50

answers:

2

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?

+1  A: 

In RSpec 2.0 you can use (I'm not sure if it is a best way but it works)

x.example.metadata[:example_group][:full_description]

As for RSpec 1.X I don't know. And that's probably what you are asking for...

pawien
+1  A: 

I found the answer. Turns out there used to be a method called full_description on x that would do exactly what I want, however it was deprecated. The following produces the string I want:

"#{x.class.description} #{x.description}"

Reference

Joey Robert