打印运行规范名称

Mac*_*rio 3 rspec ruby-on-rails

我在使用 rake 运行 Rails 应用程序的规范时遇到问题,它在某个规范上冻结。我想看看正在运行什么规范。

Rad*_*sky 5

我有一个很有趣的答案给你。

我看到的问题是规范的名称是成功或失败之后写的。在你的情况下,它永远不会发生。

所以你可以带一个自定义格式化程序!是的,它会起作用。问题是基本格式化程序定义了很多点,例如组何时开始,示例何时开始,但可用格式化程序(http://github.com/rspec/rspec-core/tree/master/lib/rspec/core/formatters/ ) 不要使用example_started方法,这正是您需要的方法。

创建自定义格式化程序很简单 - 只需将此文件放入 spec/support/formatters/anxious_formatter.rb

class AnxiousFormatter < RSpec::Core::Formatters::DocumentationFormatter
  def example_started(example)
    message = "- #{example.description}"
    output.puts message
    output.flush
  end
end
Run Code Online (Sandbox Code Playgroud)

然后你可以运行它:

spec -r spec/support/formatters/anxious_formatter.rb -f AnxiousFormatter spec/models/...
Run Code Online (Sandbox Code Playgroud)

上面的示例适用于 Rails 3 和 RSpec 2.0 - 对于以前的版本,它会略有不同。更多关于自定义格式化程序(用于 Rails 2 和 RSpec 1.x)可以在项目 wiki 上找到:https : //github.com/dchelimsky/rspec/wiki/custom-formatters

很有趣,不是吗?