【问题标题】:Test that rails helper renders a partial测试 rails helper 渲染部分
【发布时间】:2013-06-18 15:32:54
【问题描述】:

鉴于以下辅助方法,我将如何使用 rspec 正确测试它?

  def datatable(rows = [], headers = [])
    render 'shared/datatable', { :rows => rows, :headers => headers }
  end

  def table(headers = [], data = [])
    render 'shared/table', headers: headers, data: data
  end

我尝试了以下方法,但出现错误:can't convert nil into String

describe 'datatable' do
  it 'renders the datatable partial' do
    rows = []
    headers = []
    helper.should_receive('render').with(any_args)
    datatable(rows, headers)
  end
end

Rspec 输出

Failures:

  1) ApplicationHelper datatable renders the datatable partial
     Failure/Error: datatable(rows, headers)
     TypeError:
       can't convert nil into String
     # ./app/helpers/application_helper.rb:26:in `datatable'
     # ./spec/helpers/application_helper_spec.rb:45:in `block (3 levels) in <top (required)>'

./app/helpers/application_helper.rb:26

render 'shared/datatable', { :rows => rows, :headers => headers }

views/shared/_datatable.html.haml

= table headers, rows

views/shared/_table.html.haml

%table.table.dataTable
  %thead
    %tr
      - headers.each do |header|
        %th= header
  %tbody
    - data.each do |columns|
      %tr
        - columns.each do |column|
          %td= column

【问题讨论】:

  • 哪一行产生了这个错误?
  • 我已经用一些额外的细节更新了这个问题

标签: ruby-on-rails ruby rspec


【解决方案1】:

如果您只是想测试您的助手是否使用正确的参数调用正确的部分,您可以执行以下操作:

describe ApplicationHelper do

  let(:helpers) { ApplicationController.helpers }

  it 'renders the datatable partial' do
    rows    = double('rows')
    headers = double('headers')

    helper.should_receive(:render).with('shared/datatable', headers: headers, rows: rows)

    helper.datatable(rows, headers)
  end

end

请注意,这不会调用您部分中的实际代码。

【讨论】:

    【解决方案2】:

    should_receive 的参数应该是一个符号而不是字符串。至少我没有看到文档中使用了字符串(https://www.relishapp.com/rspec/rspec-mocks/v/2-14/docs/message-expectations

    所以,而不是

    helper.should_receive('render').with(any_args)
    

    使用这个

    helper.should_receive(:render).with(any_args)
    

    不确定这是否可以解决问题,但至少这是一个错误,可能会导致您的错误消息。

    【讨论】:

    • 我尝试使用符号,但仍然收到错误消息。很高兴知道我应该只使用符号!
    【解决方案3】:

    试试:

    describe 'datatable' do
      it 'renders the datatable partial' do
        rows = []
        headers = []
        helper.should_receive(:render).with(any_args)
        helper.datatable(rows, headers)
      end
    end
    

    帮助规范文档解释了这一点: https://www.relishapp.com/rspec/rspec-rails/v/2-0/docs/helper-specs/helper-spec

    错误信息非常混乱,我不知道为什么。

    【讨论】:

      【解决方案4】:

      这里有转换问题

      无法将 nil 转换为字符串

      您将 2 个空数组作为参数传递给函数,但 ruby​​ 中的空数组不是 nil,那么 render 的参数应该是字符串,不确定但尝试将测试中的参数转换为字符串,如下所示:

      datatable(rows.to_s, headers.to_s)
      

      【讨论】:

      • 但是这些参数需要是数组的,因为我在部分内部循环它们。
      猜你喜欢
      • 2016-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-17
      • 1970-01-01
      • 2011-05-24
      • 1970-01-01
      • 2017-05-07
      相关资源
      最近更新 更多