This project is archived and is in readonly mode.

#1121 ✓resolved
Matt Brown

assert_template fails when multiple templates are rendered

Reported by Matt Brown | September 26th, 2008 @ 06:46 PM | in 2.x

assert_template is provided by rails to check if the appropriate view template was rendered by a controller, the example given by the docs is

 # assert that the "new" view template was rendered
 assert_template "new"

This will only work if no other template is rendered in the controller as the implementation of assert_template simply checks that the name of the first template rendered during the processing matches the supplied value.

There are many scenarios where the first template rendered during the controller is not actually the template being checked for, the two that I have encountered are:

  1. Using render_to_string to process a partial and store into an instance variable in the controller. assert_template ends up with the name of the partial in this case.
  2. Rendering a view inside of a layout, because the layout template is rendered before the 'inner' view template, assert_template ends up with the name of the layout template in this case.

Both of these situations cause the example usage of assert_template to fail and make assert_template essentially useless for testing many controllers.

I have attached a patch which fixes the first case, by resetting first_render in the erase_render_results method which is called after render_to_string completes. I'm not sure how to address the 2nd case or the more design flaw however.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>

People watching this ticket