“未初始化常量”当包含测试辅助模块时
当我尝试将辅助模块包含到测试中时,出现未初始化的常量错误。
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将其添加到
test_helper.rb
中:旁注:不确定 test:unit,但 rspec 有一个
spec/support
目录,用于自动加载文件。Try adding this to
test_helper.rb
:Side note: Not sure about test:unit, but rspec has a
spec/support
directory for files to get auto-loaded.对我来说有效的(通过跟踪和错误发现)是:
For me what worked (found via trail and error) was: