tags:

views:

44

answers:

2
+1  Q: 

rspec "it" string

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?

+2  A: 

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.

mikej
That's perfect, thanks!
geosteve
A: 

This doesn't answer your question exactly, but if you're trying to print out each test as it runs, put this in your spec.opts file:
--format specdoc

Here's an example of what my tests look like when I run them:

A new release when parsing the artist containing a full join word
- should remove it
- should not remove it when it is inter-word

A new release when parsing the artist containing an abbreviated joining word
- should remove it when there is a period after it
- should remove it when when there is not a period after it
- should not remove it when it is inter-word with a period after it
- should not remove it when it is inter-word without a period after it

A new release when parsing the artist
- should be invalid if the year is not current
- should be valid if it is the current year
ryeguy
Not quite what I was looking for, but great tip nonetheless!
geosteve