Panasonic Youth rob sanheim writes about software, business, ruby, music, stuff and things



Posted
11 October 2006 @ 2pm

Tagged
Rails, Ruby, TDD

Discuss

How to Stub ActionController#render for Testing

Sometimes you just want to ensure that render will get called with the correct parameters in a test. Say, for instance, if you have your own special site_render method that examines subdomains or other info about the current request, and then does the real render based on that. This little test helper will override the render method to just return the options hash it was called with in a block, and then it will restore the original method to allow other functional tests to work correctly:

# stub out render to just return the options hash it was called with, to make it testable
  def stub_render_method
    @controller.class.class_eval do 
      def render(options = nil)
        options
      end
    end
    yield
  ensure
    @controller.class.send(:remove_method, :render)
  end

Call it like so, with a made up "special_render" that takes some params and then calls render based on that:

stub_render_method do
      assert_equal({:partial => "foo/baz/about"}, @controller.special_render(:baz => true, :partial => "about"))
      assert_equal({:partial => "foo/doop/about"}, @controller.special_render(:doop => true, :partial => "about"))
      # bad parameter, render gets called with default params and probably an error message
       assert_equal "",    @controller.special_render(:partial => "unknown_bad_parameter")
    end

Easy.


1 Comment

Posted by
19892 Blog Verification
16 October 2006 @ 3am

19892 Blog Verification

19892


Leave a Comment

Introducing BrainBuster - Logic Captcha for Rails New Version of BrainBuster - 0.6