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.

Rendering Text with a Proc fails in testing

#2423

I found an untested corner of ActionController and a bug in TestProcess when I dug in.

Imagine you need to stream some XML (try not to think about all the downsides). See my simple example below and pretend it generates valid output.


def render_text_with_proc
  render :text => Proc.new{ |resp, out|
    1.upto(10){ |i| out.write "<element>#{i}</element>" }
  }
end

After writing something like you see above, you then try to write a test that probably looks something like this:


def test_render_text_with_proc
  get :render_text_with_proc
  assert_response :success
  assert_select "element", 10
end

That test will fail with the following trace:

1) Error: test_render_text_with_proc(RenderTest): TypeError: can't convert Proc into String

./test/../lib/action_controller/vendor/html-scanner/html/tokenizer.rb:26:in `initialize'
./test/../lib/action_controller/vendor/html-scanner/html/tokenizer.rb:26:in `new'
./test/../lib/action_controller/vendor/html-scanner/html/tokenizer.rb:26:in `initialize'
./test/../lib/action_controller/vendor/html-scanner/html/document.rb:16:in `new'
./test/../lib/action_controller/vendor/html-scanner/html/document.rb:16:in `initialize'
./test/../lib/action_controller/test_process.rb:498:in `new'
./test/../lib/action_controller/test_process.rb:498:in `html_document'
./test/../lib/action_controller/assertions/selector_assertions.rb:614:in `response_from_page_or_rjs'
./test/../lib/action_controller/assertions/selector_assertions.rb:213:in `assert_select'
./test/controller/render_test.rb:952:in `test_render_text_with_proc'
./test/../../activesupport/lib/active_support/testing/setup_and_teardown.rb:62:in `__send__'
./test/../../activesupport/lib/active_support/testing/setup_and_teardown.rb:62:in `run'

I have attached a patch that updates TestProcess and lets this test succeed. This has been built against tags/v2.3.2.1.

Reported by Andrew Bloom · April 5th, 2009 @ 07:12 PM

State: new
Milestone: 3.x
Assigned to: nobody
Importance: none

Activity

  1. Andrew Bloom
    Andrew Bloom
    • Tag changed from 2.3.2 to 2.3.2, actionpack, action_controller, test_process

    April 5th, 2009 @ 07:13 PM

  2. Andrew Bloom
    Andrew Bloom

    I wrote up a slightly more thorough explanation here: http://www.somethingunimportant....

    April 6th, 2009 @ 05:31 AM

  3. Steve Madsen
    Steve Madsen
    • Tag changed from 2.3.2, actionpack, action_controller, test_process to 2.3.4, actionpack, action_controller, test_process

    I can confirm that this problem still exists in 2.3.4, although this patch doesn't fix it for me because it only triggers the call to the proc due to assert_select. If you evaluate @response.body in the test or have set Mocha expectations on methods that should be called within the proc, those tests still fail.

    I've reworked the patch slightly and existing tests pass, as well as the new test added by Andrew.

    October 7th, 2009 @ 08:51 PM

  4. Steve Madsen
    Steve Madsen

    Minor change to my previous patch. That one caused problems for Mocha expectations because it allowed the proc to be called more than once.

    October 7th, 2009 @ 08:57 PM

  5. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  6. Rohit Arondekar
    Rohit Arondekar
    • Importance changed from to

    Any updates here?

    Could somebody try and reproduce this on latest 2.x/3.x releases or branches? The ticket may be marked as stale if it's not updated in 2 months so if it's still an issue please leave a comment, preferably with a failing test for Rails.

    October 14th, 2010 @ 02:46 PM

  7. Ryan Bigg
    Ryan Bigg
    • Tag cleared.

    Automatic cleanup of spam.

    October 19th, 2010 @ 08:31 AM

  8. Jeff Kreeftmeijer
  9. bingbing