Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 219
Joey Robert Avatar asked Sep 28 '10 19:09

Joey Robert


2 Answers

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...

like image 173
Radek Paviensky Avatar answered Sep 18 '22 18:09

Radek Paviensky


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

like image 27
Joey Robert Avatar answered Sep 18 '22 18:09

Joey Robert