ruby-on-rails - 在 Rails 中测试辅助方法

标签 ruby-on-rails testing mocking helper

我正在测试我的 Rails 辅助方法,如下所示:

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

最佳答案

我认为:

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

可能有帮助吗?

关于ruby-on-rails - 在 Rails 中测试辅助方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/564837/

相关文章:

ruby-on-rails - Azure Ubuntu 虚拟机上的 Ruby On Rails

ruby-on-rails - 每当 Rails 中的 cron 作业因 'production' 数据库未配置错误而失败时

python - 如何模拟具有设定大小的文件对象?

android - 在 Android Instrumentation 上下文中写入/创建文件

java - 模拟删除方法

ruby-on-rails - 使用 ActiveRecord 连接动态创建 Ruby 类

html - 我希望我的帖子彼此相邻显示 Ruby on Rails

node.js - 我将如何测试在 express 中使用 mocha/chai/superagent 发送路由?

reactjs - 来自 Storybook 的模拟 api 调用

javascript - fetch-mock 调用实际的 API,而不是模拟请求