[英]Testing helper methods in Rails
I'm testing my rails helper methods like this: 我正在测试我的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
This works great, except where the helpers call on methods that require a request to have happened, such as url_for . 这很好用,除非助手调用要求发生请求的方法,例如url_for 。 Since I don't want to run the whole stack through a request process to test these (eg by calling get :index), what would need to be initialized or stubbed out for this to work? 由于我不想通过整个请求过程来测试整个堆栈(例如,通过调用get:index),因此什么需要初始化或存根才能使其正常工作?
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
I think that: 我觉得:
include ActionView::Helpers::UrlHelper
include ActionController::UrlWriter
might help? 可能有帮助?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.