Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisroos/7153418 to your computer and use it in GitHub Desktop.
Save chrisroos/7153418 to your computer and use it in GitHub Desktop.
An investigation in testing `render :partial` with `assert_template`

Testing render :partial with assert_template

Testing the use of render :partial with the :object option

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 })

Tests

Failure: Using :object in the template and :object in the spec

# 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

Failure: Using :object in the template and :locals in the spec

# 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

Failure: Using :locals in the template and :locals in the spec

# 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 })

Testing the use of render :partial with the :collection option

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 })

Tests

Failure: Using :collection in the template and :collection in the spec

# 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

Failure: Using :collection in the template and :locals in the spec

# 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

Failure: Using :locals in the template and :locals 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: '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})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment