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. 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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With