“未初始化常量”当包含测试辅助模块时

发布于 2024-10-07 18:21:35 字数 875 浏览 1 评论 0原文

当我尝试将辅助模块包含到测试中时,出现未初始化的常量错误。

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

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

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

/.../.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)

我尝试过使用命名空间,但根本无法混合模块!有什么想法为什么我会收到此错误吗?或者这是否是提取常见测试函数的正确方法?我对 Rails 很陌生,所以任何建议将不胜感激:)

干杯!

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

I have the following files in my rails test directory

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

The class/modules bodies are as follows:

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': uninitialized constant ControllerHelper (NameError)

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 :)

Cheers!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傲影 2024-10-14 18:21:35

尝试将其添加到 test_helper.rb 中:

require "test/functional/controller_helper"

旁注:不确定 test:unit,但 rspec 有一个 spec/support 目录,用于自动加载文件。

Try adding this to test_helper.rb:

require "test/functional/controller_helper"

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

蓝天白云 2024-10-14 18:21:35

对我来说有效的(通过跟踪和错误发现)是:

require "./app/helpers/currencies_helper.rb"

For me what worked (found via trail and error) was:

require "./app/helpers/currencies_helper.rb"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文