In short, in order to use assert_template
, or expect(view).to render_template
, to test the rendering of a partial with an :object
you'll have to write it out longhand in the template.
# Use this in the template
<%= render partial: 'people', locals: { people: @people } %>
# Rather than
<%= render partial: 'people', object: @people %>
# And test it with one of the following
assert_template partial: '_people', locals: { people: people }
expect(view).to render_template(partial: '_people', locals: { people: people })
# In the template
<%= render partial: 'people', object: @people %>
# In the view spec
expect(view).to render_template(partial: 'people/_people', object: people)
# or
assert_template(partial: 'people/_people', object: people)
# Error
Unknown key: object
# In the template
<%= render partial: 'people', object: @people %>
# In the view spec
expect(view).to render_template(partial: 'people/_people', locals: { people: people })
# Error
expected people/people to be rendered but it was not..
Expected ["people"] to include "people/people".
Failure: Using :object
in the template, :locals
in the spec and not specifying the path to the partial in the spec
# In the template
<%= render partial: 'people', object: @people %>
# In the view spec
expect(view).to render_template(partial: '_people', locals: { people: people })
# Error
undefined method `[]' for nil:NilClass
# In the template
<%= render partial: 'people', locals: {people: @people} %>
# In the view spec
expect(view).to render_template(partial: 'people/_people', locals: {people: people})
# Error
expected people/people to be rendered but it was not..
Expected ["people"] to include "people/people".
Success: Using :locals
in the template, :locals
in the spec and not specifying the path to the partial in the spec
# In the template
<%= render partial: 'people', locals: {people: @people} %>
# In the view spec
# *NOTE* We no longer specify the path to the partial!
expect(view).to render_template(partial: '_people', locals: { people: people })
In short, in order to use assert_template
, or expect(view).to render_template
, to test the rendering of a partial with a :collection
you'll have to write it out longhand in the template.
# Use this in the template
<% @people.each do |person| %>
<%= render partial: 'person', locals: { person: person } %>
<% end %>
# Rather than
<%= render partial: 'person', collection: @people %>
# And test it with one of the following
assert_template partial: '_person', locals: { person: person }
expect(view).to render_template(partial: '_person', locals: { person: person })
# In the template
<%= render partial: 'person', collection: @people %>
# In the view spec
expect(view).to render_template(partial: 'people/_person', collection: people)
# Error
Unknown key: collection
# In the template
<%= render partial: 'person', collection: @people %>
# In the view spec
expect(view).to render_template(partial: 'people/_person', locals: {person: person})
# Error
expected people/person to be rendered but it was not..
Expected ["person"] to include "people/person".
Failure: Using :collection
in the template, :locals
in the spec and not specifying the path to the partial in the spec
# In the template
<%= render partial: 'person', collection: @people %>
# In the view spec
expect(view).to render_template(partial: '_person', locals: {person: person})
# Error
undefined method `[]' for nil:NilClass
# In the template
<% @people.each do |person| %>
<%= render partial: 'person', locals: {person: person} %>
<% end %>
# In the view spec
expect(view).to render_template(partial: 'people/_person', locals: {person: person})
# Error
expected people/person to be rendered but it was not..
Expected ["person"] to include "people/person".
Success: Using :locals
in the template, :locals
in the spec and not specifying the path to the partial in the spec
# In the template
<% @people.each do |person| %>
<%= render partial: 'person', locals: {person: person} %>
<% end %>
# In the view spec
expect(view).to render_template(partial: '_person', locals: {person: person})