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.