2

当我尝试运行我的规范时,我得到一个未初始化的常量错误。我的规格如下所示:

describe Facility do 
  it { should have_many(:units) }
  it { should have_many(:facilities_users) }
  it { should have_many(:administrators) }
  it { should have_many(:facility_employees) }
end

错误是: facility_spec.rb:1:in `<top (required)>': uninitialized constant Facility (NameError)

我当然有一个 Facility 模型,所以我不确定为什么会发生这种情况。

4

4 回答 4

2

您应该尝试运行rake spec而不是rspec spec. 但两者都可能有效。

如果不工作尝试尝试bundle exec rspec specbundle exec rake spec

资料来源:尝试运行 rspec 时,我得到 uninitialized constant

于 2012-10-18T17:01:48.697 回答
0

在文件顶部添加以下内容:

require 'spec_helper'
于 2012-10-18T16:17:47.667 回答
0

如果您使用的是“rspec-rails”gem,请运行

rails g rspec:install

这将创建spec/spec_helper.rb文件(如果您不使用 ActiveRecord,则应对其进行编辑,以便它正确运行您的规范设置)。

之后,确保您在规范文件的顶部需要帮助程序:

require 'spec_helper'

如果这对您不起作用,则可能还有更多问题,例如:

  1. You're trying to test a file under the lib/ directory. In this case, make sure this file is loaded with the environment (config/application.rb -> autoload_paths) or require it explicitly.
  2. The constant actually doesn't exist. It could be inside a namespace or just a typo.
于 2012-10-18T17:34:46.287 回答
0

In the spec file, require the file where Facility class is defined.

于 2012-10-19T04:41:06.677 回答