rspec“它”字符串

时间:2010-04-15 04:44:13

标签: ruby rspec

rspec方法是否有可能在本地方法中获取传递给it()的参数的值?例如,如果我想:

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

打印这个:

I should figure this out

...我会在代码示例中为SPEC_NAME提供什么?

更好的是,像我这样的新生态学家怎么会自己解决这个问题呢?

3 个答案:

答案 0 :(得分:7)

description方法应该做你想要的。 e.g。

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

就如何解决这个问题而言,我通过查看RDocs for RSpec找到了它,首先查看it方法的来源,看看它是如何工作的。然后,您将发现RSpec将“it”字符串称为“描述”,因此您可以查找包含其名称中描述的可用方法。

我知道它现在对你没什么用处,但随着你学到更多Ruby,你会发现更容易阅读像RSpec这样的库中的代码,并且会发展出一种关于如何实现它们的本能,这将是帮助你找到合适的地方。

答案 1 :(得分:1)

对于更新版本的RSpec(我使用的是2.14),您可以将其称为example.description

it "prints itself" do
  puts "My description string is '#{example.description}'."
end

答案 2 :(得分:0)

这并不能完全回答您的问题,但如果您尝试在运行时打印出每个测试,请将其放入spec.opts文件中:
--format specdoc

以下是我运行测试时的示例:

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