Fix nil.rewrite errors in your Helper Tests

In testing my helpers, I discovered a nil.rewrite exception due to ActionController::Base.initialize_current_url not being called. This occurs when using url_for with a hash as arguments.

ActionView::TestCase does not initialize current url so you won’t be able to use *_url and *_path helpers generated from your routes. Put the following lines at the end of your test_helper.rb file to resolve the issue.

class ActionView::TestCase < ActiveSupport::TestCase
  class TestController < ActionController::Base
    attr_accessor :request, :response, :params

    def initialize
      @request = ActionController::TestRequest.new
      @response = ActionController::TestResponse.new
      
      #TestCase doesn't have context of a current url so cheat a bit
      @params = {}
      send(:initialize_current_url)
    end
  end
end

I've submitted a patch to core regarding this. Please +1 for the good of helper_tests!

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Mixx
  • Google Bookmarks
  • DZone
  • blogmarks
  • Bumpzee

Tags: , , ,


You can skip to the end and leave a response. Pinging is currently not allowed.

  • Now I now what had been my error, well never thought it would be simple like this
blog comments powered by Disqus