gpt4 book ai didi

ruby-on-rails - 测试 Rails 助手是否呈现部分

转载 作者:数据小太阳 更新时间:2023-10-29 07:23:53 26 4
gpt4 key购买 nike

给定以下辅助方法,我如何使用 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

最佳答案

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

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

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

关于ruby-on-rails - 测试 Rails 助手是否呈现部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17236307/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com