ruby-on-rails – 如何在spec_helper.rb中指定自定义格式化程序
发布时间:2020-12-17 01:28:02 所属栏目:百科 来源:网络整理
导读:我正在使用Rspec测试开发一个Rails项目,需要很长时间才能运行.为了弄清楚哪些花了这么多时间,我想我会为RSpec制作一个自定义格式化程序并打印出每个例子的持续时间: require 'rspec/core/formatters/base_formatter'class TimestampFormatter 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时我最终得到以下错误: 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' (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |