2013-11-21 38 views
0

我有一些規格,我希望在Rails視圖(v3.2.15)中使用。如何使用HtmlFormatter運行RSpec?獲得「私人方法」放'所謂nil:NilClass「

我使用的是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 

謝謝

回答

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和:文檔:

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) 

希望有所幫助。

+0

謝謝,但我想存儲在一個attr_accessor的HTML,所以我可以打印它在一個視圖,發送它在電子郵件等等等等更新問題,以顯示完整的代碼。感謝您的回覆 – rwb

+0

我會編輯我的答案,爲您的案例展示解決方案。 – DiegoSalazar

+0

這看起來很棒。現在將有一個遊戲,並回到你身邊。非常感謝您的幫助 – rwb

相關問題