This project is archived and is in readonly mode.
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:
- 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.
- 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
-
josh December 3rd, 2008 @ 04:01 PM
- State changed from new to resolved
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="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>