Andrew Vit
Andrew Vit

Reputation: 19239

Testing helper methods in Rails

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

Upvotes: 2

Views: 3336

Answers (1)

nitecoder
nitecoder

Reputation: 5486

I think that:

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

might help?

Upvotes: 4

Related Questions