在Rails中测试帮助器方法

时间:2009-02-19 11:17:21

标签: ruby-on-rails testing mocking helpers

我正在测试我的rails helper方法:

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

这很有用,除非帮助者调用需要发生请求的方法,例如 url_for 。由于我不希望通过请求进程运行整个堆栈来测试这些(例如通过调用get:index),需要初始化或删除什么才能使其工作?

我正在寻找一个更通用的模拟请求的解决方案,但具体来说,我得到的错误是:

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

1 个答案:

答案 0 :(得分:4)

我认为:

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

可能会有帮助吗?