且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

rspec 测试抛出未初始化的常量 Rails (NameError)

更新时间:2023-02-12 17:01:09

在你的 spec_helper.rb 中,你有以下一行两次:

In your spec_helper.rb, you have the following line twice:

Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

删除第一个实例(第 2 行的实例).这就是导致错误的原因.在 require 'rspec/rails' 之前有这行会导致问题,因为我们不知道 Rails 是什么,所以我们不能调用 root> 方法.第二个实例(第 13 行)很好,因为它在 require 'rspec/rails' 之后.

Delete the first instance (the one on line 2). This is what is causing the error. Having this line before require 'rspec/rails' will cause problems because we don't know what Rails is, and so we cannot call the root method. The second instance (on line 13) is fine because this is after require 'rspec/rails'.