如何使用HtmlFormatter运行RSpec?获取“私有方法`puts”调用nil:NilClass“

时间:2013-11-21 15:02:28

标签: ruby-on-rails rspec rspec2 rspec-rails

我有很多规范,我希望在Rails视图(v3.2.15)中使用。

我使用rspec-rails gem v2.14.0,我运行的代码如下。

此代码用于处理gem的v2.11.4,我可以看到HtmlFormatter现在已将其部分代码拆分为HtmlPrinter,我想这与错误有关。你打算如何使用HtmlFormatter?我无法找到源代码之外的任何文档......我在下面做错了什么?

  class RSpecRunner
    attr_accessor :summary, :html, :documentation
    SpecPath = "system_checks/**/*_spec.rb"
    DataChecks = "data_checks/**/*_spec.rb"

    def initialize(path)
      @html = RSpec::Core::Formatters::HtmlFormatter.new(nil)
      @documentation = RSpec::Core::Formatters::DocumentationFormatter.new(nil)
    end

    def run!
      RSpec::world.reset
      Dir[@spec_path].each { |f| load f }

      @html = RSpec::Core::Formatters::HtmlFormatter.new(nil)
      reporter = RSpec::Core::Reporter.new(@html)

      RSpec::world.example_groups.each do |example_group|
        example_group.run(reporter)
      end
    end
  end

控制器

@sys_check = RSpecRunner.new(RSpecRunner::SystemChecksPath)
@sys_check.run!

查看

@sys_check.html.output.string

错误

NoMethodError: private method `puts' called for nil:NilClass
    from /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/formatters/html_printer.rb:23:in `print_example_group_start'
    from /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/formatters/html_formatter.rb:50:in `example_group_started'
    from /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/reporter.rb:127:in `block in notify'
    from /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/reporter.rb:126:in `each'
    from /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/reporter.rb:126:in `notify'
    from /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/reporter.rb:74:in `example_group_started'
    from /usr/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.7/lib/rspec/core/example_group.rb:367:in `run'
    from (irb):11:in `block in irb_binding'
    from (irb):10:in `each'
    from (irb):10

由于

1 个答案:

答案 0 :(得分:2)

HtmlFormatter需要一种输出内容的方法。给它STDOUT:

@html = RSpec::Core::Formatters::HtmlFormatter.new(STDOUT)

如果您希望输出转到文件,例如HTML文件,请为其指定文件句柄:

fh = File.open('path/to/html_output/rspec-html-output.html', 'w')
@html = RSpec::Core::Formatters::HtmlFormatter.new(fh)

现在你将有一个很好的rspec格式化输出的小html文件。

编辑: 无论您传递给RSpec::Core::Formatters::HtmlFormatter.new的是什么,都需要回复putsflush

由于您希望将输出保存到访问器,您可以在自己的自定义类上定义这些方法,将输出保存到适当的访问器:html和:documentation:

class HTMLOutput
  def initialize(rspec_runner)
    @rspec_runner = rspec_runner
  end

  def puts(html)
    @rspec_runner.html ||= ""
    @rspec_runner.html << html
  end

  def flush; end
end

class DocOutput
  def initialize(rspec_runner)
    @rspec_runner = rspec_runner
  end

  def puts(html)
    @rspec_runner.documentation ||= ""
    @rspec_runner.documentation << html
  end

  def flush; end
end

class RSpecRunner
  def initialize(path)
    # I assumed you wanted to save the output of the formatter to the 
    # accessors :html and :documentation but you were already assigning
    # the formatters to these accessors via @html etc. Make new instance vars
    # for these and use the accessors just for the output.
    # By instantiating these output classes and passing in self they will be able
    # to save the output to the :html, :documentation accessors.
    @html_formatter = RSpec::Core::Formatters::HtmlFormatter.new(HTMLOutput.new(self))
    @documentation_formatter = RSpec::Core::Formatters::DocumentationFormatter.new(DocOutput.new(self))
  end

  ... rest of your code ...
end

最后,在你的跑步者的run!方法中,你不需要再次实例化这个人:

@html = RSpec::Core::Formatters::HtmlFormatter.new(nil)

希望有所帮助。