views:

791

answers:

1

I'm testing my rails helper methods like this:

require File.dirname(__FILE__) + '/../../test_helper'
require 'action_view/test_case'
require 'action_view/helpers'

class ListingsHelperTest < ActionView::TestCase

  def setup
    @controller = ListingsController.new
    @request  = ActionController::TestRequest.new
    @response = ActionController::TestResponse.new
  end
end

This works great, except where the helpers call on methods that require a request to have happened, such as url_for. Since I don't want to run the whole stack through a request process to test these (e.g. by calling get :index), what would need to be initialized or stubbed out for this to work?

I'm looking for a more general solution to mock a request, but specifically, the error I'm getting is this:

NoMethodError: You have a nil object when you didn't expect it!
The error occurred while evaluating nil.rewrite
rails/actionpack/lib/action_controller/base.rb:625
rails/actionpack/lib/action_view/helpers/url_helper.rb:85
rails/actionpack/lib/action_view/helpers/url_helper.rb:85
+3  A: 

I think that:

include ActionView::Helpers::UrlHelper
include ActionController::UrlWriter

might help?

railsninja
That's the ticket, thanks! I also just realized this technique would probably be visible in rails' own helper tests, right?
Andrew Vit