如何在spec_helper.rb中指定自定义格式化程序,有哪些详细步骤?

2026-04-11 18:011阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计309个文字,预计阅读时间需要2分钟。

如何在spec_helper.rb中指定自定义格式化程序,有哪些详细步骤?

我正在使用Rspec测试一个Rails项目,运行需要很长时间。为了更清晰地了解耗时,我想自定义Rspec,打印出每个例子的持续时长:+ require 'rspec/core'

我正在使用Rspec测试开发一个Rails项目,需要很长时间才能运行.为了弄清楚哪些花了这么多时间,我想我会为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时我最终得到以下错误:

如何在spec_helper.rb中指定自定义格式化程序,有哪些详细步骤?

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

如何将自定义格式化程序作为符号提供?

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'

本文共计309个文字,预计阅读时间需要2分钟。

如何在spec_helper.rb中指定自定义格式化程序,有哪些详细步骤?

我正在使用Rspec测试一个Rails项目,运行需要很长时间。为了更清晰地了解耗时,我想自定义Rspec,打印出每个例子的持续时长:+ require 'rspec/core'

我正在使用Rspec测试开发一个Rails项目,需要很长时间才能运行.为了弄清楚哪些花了这么多时间,我想我会为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时我最终得到以下错误:

如何在spec_helper.rb中指定自定义格式化程序,有哪些详细步骤?

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

如何将自定义格式化程序作为符号提供?

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'