如何在spec_helper.rb中指定自定义格式化程序?

发布于 2024-12-08 21:29:43 字数 1137 浏览 0 评论 0原文

我正在开发一个 Rails 项目,其中的 Rspec 测试需要很长时间才能运行。为了弄清楚哪些花费了这么多时间,我想我应该为 RSpec 制作一个自定义格式化程序,并让它打印出每个示例的持续时间:

require 'rspec/core/formatters/base_formatter'

class TimestampFormatter < RSpec::Core::Formatters::BaseFormatter

  def initialize(output)
    super(output)
    @last_start = 0
  end

  def example_started(example)
    super(example)
    output.print "Example started: " << example.description
    @last_start = Time.new
  end

  def example_passed(example)
    super(example)
    output.print "Example finished"
    now = Time.new
    time_diff = now - @last_start

    hours,minutes,seconds,frac = Date.day_fraction_to_time(time_diff)
    output.print "Time elapsed: #{hours} hours, #{minutes} minutes and #{seconds} seconds"    
  end
end

在我的 spec_helper.rb 中,我尝试了以下操作:

RSpec.configure do |config|      
  config.formatter = :timestamp
end

但我最终得到了运行 rspec 时出现以下错误:

configuration.rb:217:in `formatter=': Formatter 'timestamp' unknown - maybe you meant 'documentation' or 'progress'?. (ArgumentError)

如何使自定义格式化程序可用作符号?

I'm working on a Rails project with Rspec tests that take very long to run. In order to figure out which ones are taking so much time I figured I'd make a custom formatter for RSpec and have it print out the duration of each example:

require 'rspec/core/formatters/base_formatter'

class TimestampFormatter < RSpec::Core::Formatters::BaseFormatter

  def initialize(output)
    super(output)
    @last_start = 0
  end

  def example_started(example)
    super(example)
    output.print "Example started: " << example.description
    @last_start = Time.new
  end

  def example_passed(example)
    super(example)
    output.print "Example finished"
    now = Time.new
    time_diff = now - @last_start

    hours,minutes,seconds,frac = Date.day_fraction_to_time(time_diff)
    output.print "Time elapsed: #{hours} hours, #{minutes} minutes and #{seconds} seconds"    
  end
end

And in my spec_helper.rb I tried the following:

RSpec.configure do |config|      
  config.formatter = :timestamp
end

But I end up getting the following error when running rspec:

configuration.rb:217:in `formatter=': Formatter 'timestamp' unknown - maybe you meant 'documentation' or 'progress'?. (ArgumentError)

How do I make my custom formatter available as a symbol?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

静谧 2024-12-15 21:29:43
config.formatter = :timestamp

这是错误的。对于自定义格式化程序,您需要根据您的情况指定完整的类名

# if you load it manually
config.formatter = TimestampFormatter
# or if you do not want to autoload it by rspec means, but it should be in
# search path
config.formatter = 'TimestampFormatter'
config.formatter = :timestamp

This is wrong. For custom formatters you need to specify full class name, in your case

# if you load it manually
config.formatter = TimestampFormatter
# or if you do not want to autoload it by rspec means, but it should be in
# search path
config.formatter = 'TimestampFormatter'
勿挽旧人 2024-12-15 21:29:43

这并不完全是答案,但是您知道可以使用 --profile 标志运行 RSpec 来做到这一点,对吗? :)

This is not exactly the answer but, you know you can run RSpec with the --profile flag to do just that, right ? :)

野心澎湃 2024-12-15 21:29:43

您可以将格式化程序复制到您的spec目录并运行rspec命令,如下所示:

rspec spec/ -f TimestampFormatter

You can copy the formatter to your spec directory and run the rspec command as following:

rspec spec/ -f TimestampFormatter
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文