[英]Test that rails helper renders a partial
Given the following helper method, how would I properly test this using rspec
? 给定以下辅助方法,如何使用
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
I've tried the following but I get the error: can't convert nil into String
我尝试了以下但我得到错误:
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 Output 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 ./app/helpers/application_helper.rb:26
render 'shared/datatable', { :rows => rows, :headers => headers }
views/shared/_datatable.html.haml 视图/共享/ _datatable.html.haml
= table headers, rows
views/shared/_table.html.haml 视图/共享/ _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
if you just want to test that your helper calls the right partial with the correct parameters you can do the following: 如果您只是想测试您的助手使用正确的参数调用正确的部分,您可以执行以下操作:
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
note that this won't call the actual code in your partial. 请注意,这不会调用部分中的实际代码。
The argument of should_receive
should be a symbol instead of string. should_receive
的参数应该是符号而不是字符串。 At least I have not seen string is used in doc( https://www.relishapp.com/rspec/rspec-mocks/v/2-14/docs/message-expectations ) 至少我没有看到字符串用于doc( https://www.relishapp.com/rspec/rspec-mocks/v/2-14/docs/message-expectations )
So, instead of 所以,而不是
helper.should_receive('render').with(any_args)
Use this 用这个
helper.should_receive(:render).with(any_args)
Not sure if this could solve the problem but at least this is an error which cause your error message probably. 不确定这是否可以解决问题,但至少这是一个错误,可能导致您的错误消息。
Try: 尝试:
describe 'datatable' do
it 'renders the datatable partial' do
rows = []
headers = []
helper.should_receive(:render).with(any_args)
helper.datatable(rows, headers)
end
end
The helper spec documentation explains this: https://www.relishapp.com/rspec/rspec-rails/v/2-0/docs/helper-specs/helper-spec 帮助规范文档解释了这一点: https : //www.relishapp.com/rspec/rspec-rails/v/2-0/docs/helper-specs/helper-spec
The error message is very confusing, and I'm not sure why. 错误信息非常混乱,我不知道为什么。
here you have a problem of convertion 在这里你有转换的问题
can't convert nil into String
无法将nil转换为String
you pass an 2 empty arrays as parameters to the function, but empty array in ruby is not nil, then parameters of render should be a string, not sure but try to convert parameters in your test to string like this : 你将2个空数组作为参数传递给函数,但是ruby中的空数组不是nil,那么render的参数应该是一个字符串,不确定但是尝试将测试中的参数转换为字符串,如下所示:
datatable(rows.to_s, headers.to_s)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.