[英]How is spec/rails_helper.rb different from spec/spec_helper.rb? Do I need it?
[英]How do I specify a custom formatter in spec_helper.rb?
我正在使用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时我最终得到以下错误:
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'
这不是答案,但是,你知道你可以使用--profile标志运行RSpec来做到这一点,对吗? :)
您可以将格式化程序复制到spec目录并运行rspec命令,如下所示:
rspec spec/ -f TimestampFormatter
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.