Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rspec "it" string

Tags:

ruby

rspec

Is it possible for an rspec method to get the value of the parameter passed to it() in the local method? For example, if I want:

describe Me do
  it "should figure this out"
    puts "I " + SPEC_NAME
  end
end

to print this:

I should figure this out

... what would I put for SPEC_NAME in the code sample?

Even better, how would a relatively new rubologist like me figure this out on his own?

like image 990
lambinator Avatar asked Apr 15 '10 04:04

lambinator


2 Answers

The description method should do what you want. e.g.

describe Me do
  it "should figure this out" do
    puts "I " + description # or "I #{description}" using string interpolation
  end
end

In terms of how to figure this out, I found it by looking in the RDocs for RSpec, starting by looking at the source of the it method to see how it works. You will then find that RSpec refers to the "it" string as the "description" so you can look for available methods that include description in their name.

I know it's not much use to you right now, but as you learn more Ruby you'll find it easier to read the code in libraries like RSpec and will develop an instinct for how they are likely to be implemented, which will help you look in the right places for things.

like image 146
mikej Avatar answered Sep 27 '22 23:09

mikej


With more recent versions of RSpec (I'm using 2.14), you can refer to it as example.description.

it "prints itself" do
  puts "My description string is '#{example.description}'."
end
like image 21
declan Avatar answered Sep 27 '22 23:09

declan