本文介绍了“未初始化的常数"指的是“未初始化的常数".包括测试助手模块时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将帮助程序模块包含在测试中时,出现未初始化的常量错误.

I am getting an uninitialized constant error when trying to include a helper module into a test.

我的rails测试目录中有以下文件

I have the following files in my rails test directory

functional> admin> school_controller_test.rb
functional> controller_helper.rb

类/模块主体如下:

module ControllerHelper
  def check_sort_order (items, column, direction)
    ...
  end
end

class Admin::SchoolsControllerTest < ActionController::TestCase
  include ::ControllerHelper 

  test "should sort by columns" do
    check_sort_order(assigns(:schools), 'schools.name', 'asc')
    check_sort_order(assigns(:schools), 'schools.name', 'desc')
  end
end

运行此命令时,测试输出为:

When I run this, the test output is:

/.../.rvm/gems/ruby​​-1.9.2-p0/gems/rspec-core-2.3.0/lib/rspec/core/backward_compatibility.rb:20:in'const_missing':未初始化的常量ControllerHelper(NameError)

/.../.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.3.0/lib/rspec/core/backward_compatibility.rb:20:in `const_missing': uninitialized constant ControllerHelper (NameError)

我尝试使用名称空间,但是根本无法将模块混入其中!有任何想法为什么我会收到此错误吗?还是这是提取通用测试功能的正确方法?我是Rails的新手,所以任何建议都将不胜感激:)

I've tried playing with the namespaces, but can't get the module mixed in at all! Any ideas why I'm getting this error? Or is this even the correct way to extract common test functions? I'm very new to Rails, so any advice would be appreciated :)

干杯!

推荐答案

尝试将其添加到test_helper.rb:

require "test/functional/controller_helper"

侧面说明:不确定test:unit,但是rspec具有spec/support目录,用于自动加载文件.

Side note: Not sure about test:unit, but rspec has a spec/support directory for files to get auto-loaded.

这篇关于“未初始化的常数"指的是“未初始化的常数".包括测试助手模块时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 06:31