2

我正在测试我的 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
4

1 回答 1

4

我觉得:

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

可能有帮助?

于 2009-02-21T01:22:07.107 回答