【问题标题】:How to run RSpec using HtmlFormatter? Getting "private method `puts' called for nil:NilClass"如何使用 HtmlFormatter 运行 RSpec?获取“调用 nil:NilClass 的私有方法‘puts’”
【发布时间】:2013-11-21 15:02:28
【问题描述】:

我有许多规格,我希望在 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

谢谢

【问题讨论】:

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


    【解决方案1】:

    HtmlFormatter 想要一种输出内容的方法。给它标准输出:

    @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 和:文档:

    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)
    

    希望对您有所帮助。

    【讨论】:

    • 谢谢,但我想将 HTML 存储在 attr_accessor 中,这样我就可以在视图中打印它,通过电子邮件等方式发送它等等。我会更新问题以显示完整代码。感谢您的回复
    • 这看起来很棒。现在会有一场戏,然后回复你。非常感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-08
    相关资源
    最近更新 更多