Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

assert_template fails when multiple templates are rendered

#1121

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.

Reported by Matt Brown · September 26th, 2008 @ 06:46 PM

State: resolved
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Nick Eskelinen
  2. Nick Eskelinen
  3. josh
    josh
    • State changed from new to resolved

    December 3rd, 2008 @ 04:01 PM