简体   繁体   中英

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?

The description method should do what you want. eg

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM