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.

render_other_test fails when wrapped in respond_to

#6059

I am having trouble adding a new renderer to Rails 3.0.3 and when looking at the tests I find that render_simon_says in render_other_test.rb works as. However when the same test is wrapped in a respond_to the tests fail.

1) Failure:
test_using_custom_render_option(RenderOtherTest) [test/controller/render_other_test.rb:272]:
<"Simon says: foo"> expected but was
<" ">.

ActionController.add_renderer :simon do |says, options|
  self.content_type  = Mime::TEXT
  self.response_body = "Simon says: #{says}"
end   

def render_simon_says  
  respond_to do |format|
    format.simon { render :simon => "foo" }
  end
end

def test_using_custom_render_option     
  Mime::Type.register 'application/simon', :simon 
  get :render_simon_says
  assert_equal "Simon says: foo", @response.body
end

Reported by amkirwan · November 24th, 2010 @ 06:05 PM

State: resolved
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Neeraj Singh
    Neeraj Singh
    • Importance changed from to Low

    Renderers are procs which tell for which key what proc to use to render the result.

    When you wrap your code in respond_to then formats need to be matched. Since there is no format called :simon your test fails. In order to make the test pass you need to use something like Mime::Type.register "text/simon", :simon . I have not tested my code but you need to register format :simon in order for your code to work .

    Hope that helps.

    November 24th, 2010 @ 08:45 PM

  2. amkirwan
    amkirwan

    I could be missing something about the way this should work in Rails 3 but in my test_using_custom_reneder_option in the code above I have a Mime::Type.register but the code does not work. I mistakenly put Mime::Type.register 'application/simon', :simon in the code above but it also fails when using Mime::Type.register 'text/simon', :simon. If the MIME type isn't registered then the test will error with:

      1) Error:
    test_using_custom_render_option(RenderOtherTest):
    NameError: uninitialized constant Mime::SIMON
    

    but if the MIME type :simon is registered like in my code in the original post then the test fails:

      1) Failure:
    test_using_custom_render_option(RenderOtherTest) [test/controller/render_other_test.rb:273]:
    <"Simon says: foo"> expected but was
    <" ">.
    

    It seems as though for a custom MIME type when put within a respond_to then the block associated with the custom render is never called.

    ActionController.add_renderer :simon do |says, options|
      self.content_type  = Mime::TEXT
      self.response_body = "Simon says: #{says}"
    end
    

    I've been stepping through the render code to see if I can find where the problem is. I've gotten to the point where I see that calling format.simon will create a new instance method simon in the class ActionController::MimeResponds::Collector using method_missing of the AbstractController::Collector module. It seems as though the &block of this method is never called which is why the custom render doesn't work in respond_to.

    November 25th, 2010 @ 03:02 AM

  3. Neeraj Singh
    Neeraj Singh
    • State changed from new to resolved

    In order to make the test pass this is what you need to do

      def test_using_custom_render_option
         Mime::Type.register 'text/simon', :simon
        get :render_simon_says, :format => :simon
        assert_equal "Simon says: foo", @response.body
      end
    
        def render_simon_says
          respond_to do |format|
           format.simon { render :simon => "foo" }
          end
        end
    

    Hope that helps.

    December 1st, 2010 @ 05:35 AM