混淆 - spec_helper.rb:94:in `<top (required)>': uninitialized constant Shoulda (NameError)

问题描述 投票:0回答:3

我正在使用 Rails 4 和 Mongoid 4 进行一个项目。我正在尝试设置 Shoulda-matchers(版本 2.8.0),遵循 thoughtbot/shoulda-matchers,它指向另一个名为 README for 2.8 的自述文件。 0。我希望使用 mongoid-rspec 进行测试。

然而我不断得到

spec_helper.rb:94:in '<top (required)>': uninitialized constant Shoulda (NameError)

我在

Gemfile
中添加了这个:跟随thoughtbot/shoulda-matchers

group :test do
  gem 'shoulda-matchers'
end

我还添加了

spec_helper.rb
(这是错误的来源)-跟随thoughtbot/shoulda-matchers

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :rails
  end
end

我试过谷歌搜索但是没有直接的解决方案(或者没有直接的问题)。我在 Gemfile 中添加了

require: false
,遵循 2.8.0 的README

group :test do
  gem 'shoulda-matchers', require: false
end

我在

require 'shoulda/matchers'
中添加了
rails_helper.rb
。我的“要求”顺序是这样的:

require 'spec_helper'
require 'rspec/rails'
require 'shoulda/matchers'

require 'rspec/rails' 默认低于 require 'spec_helper'。根据 github 页面上提供的 README,我应该将

shoulda\matcher
放在“rspec/rails”下方。我也曾尝试将
require 'shoulda/matchers'
放在
require 'spec_helper'
之上,但没有成功。

我的版本:

Rails 4.2.1
Ruby 2.2.1
Mongoid ~ 4.0.0
rspec-rails ~ 3.0
mongoid-rspec ~ 2.1.0
shoulda-matchers 2.8.0

我真的很感谢任何帮助。

ruby-on-rails rspec mongoid shoulda
3个回答
5
投票

从您提供的链接:

注意:新的配置语法在公开发行版中尚不可用——有关当前安装说明,请参阅 2.8.0 的自述文件。

该注释来自 master 分支。当前版本 (2.8.0) 有一组不同的文档。令人困惑,我知道。

只需从

spec/spec_helper.rb
中删除该配置部分,一切都应该再次成为彩虹和独角兽。


0
投票

只需将这些行插入

spec/spec_helper.rb

config.include(Shoulda::Matchers::ActiveModel, type: :model)
config.include(Shoulda::Matchers::ActiveRecord, type: :model)

参考:

https://github.com/thoughtbot/shoulda-matchers#availability-of-matchers-in-various-example-groups


0
投票

将这些行添加到 spec/rails_helper.rb

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :rails
  end
end
© www.soinside.com 2019 - 2024. All rights reserved.