ruby-on-rails – 测试rails helper渲染部分
发布时间:2020-12-17 03:55:13 所属栏目:百科 来源:网络整理
导读:给定以下辅助方法,如何使用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 我尝试
给定以下辅助方法,如何使用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 我尝试了以下但我得到错误:无法将nil转换为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',:headers => headers } 视图/共享/ _datatable.html.haml = table headers,rows 视图/共享/ _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',rows: rows) helper.datatable(rows,headers) end end 请注意,这不会调用部分中的实际代码. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |