Check that the assistant rails

Given the following helper method, how would I correctly check this with 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 tried the following but get an error: 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

 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 } 

view / general / _datatable.html.haml

 = table headers, rows 

view / general / _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 
+4
source share
4 answers

if you just want to verify that your helper calls the correct 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 will not call the actual code in partial.

+8
source

The should_receive argument must be a character instead of a string. At least I have not seen the string used in the doc ( https://www.relishapp.com/rspec/rspec-mocks/v/2-14/docs/message-expectations )

So instead

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

Use this

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

Not sure if this can solve the problem, but at least it is an error that can cause an error message.

+1
source

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 supporting specification documentation explains this: 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.

+1
source

here you have a conversion problem

cannot convert nil to string

you pass 2 empty arrays as parameters for the function, but the empty array in ruby ​​is not nil, then the render parameters should be a string, not sure, but try to convert the parameters in your test to a string as follows:

 datatable(rows.to_s, headers.to_s) 
0
source

Source: https://habr.com/ru/post/1487474/


All Articles