This project is archived and is in readonly mode.

#3467 ✓resolved
Chris Hapgood

ActionView::TestCase dies when rendering a layout

Reported by Chris Hapgood | November 6th, 2009 @ 10:18 PM | in 2.3.6

ActionView::TestCase creates a view with a nil output buffer -probably assuming that all rendering simply returns a string. However, rendering a layout does not return a string -instead #_render_with_layout concatenates the result of rendering the layout as a partial onto the view's output buffer. Because the view's output buffer is nil, an exception is raised for invoking append (<<) on nil.

A simple work-around is to provide the view with an output buffer.

This behavior is observed in 2-3-stable.

Which component is breaking the rules? Is is the TestCase, which neglects to provide an output buffer for the view? Or is it ActionView::Base#render_with_layout that doesn't follow convention and simply return a string? Fixing TestCase is easy. Fixing render_with_layout will be (much) harder.

Other (indirect) references to this problem:
* http://groups.google.com/group/haml/browse_thread/thread/c108c393a5...

To be pragmatic, I've created a one-line patch that links the test case's output buffer to the view's output buffer.

It applies cleanly to 2-3-stable.
It includes a failing test.
All action pack tests pass with the patch.

A simple monkey patch to get around the problem in the short term:

module ActionView
  class TestCase
    # When rendering a layout, the ActionView::TestCase creates a view with a nil
    # output buffer -probably assuming that all rendering simply returns a string.  Sadly,  
    # rendering a layout does not return a string -instead #_render_with_layout concat-
    # enates the result of rendering the layout as a partial onto the view's output buffer.  A simple
    # work-around is to provide the view with an output buffer.
    def _view
      view = ActionView::Base.new(ActionController::Base.view_paths, _assigns, @controller)
      view.helpers.include master_helper_module
      view.output_buffer = self.output_buffer
      view
    end
  end
end

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="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Attachments

Referenced by

Pages