困惑 - spec_helper.rb:94:在`<top(required)=“”>':未初始化的常量Shoulda(NameError)

时间:2015-07-08 05:00:15

标签: ruby-on-rails rspec mongoid shoulda

我正在开发一个带有Rails 4&amp; amp;的项目。 Mongoid 4.我试图在thoughtbot/shoulda-matchers之后设置Shoulda-matcher(版本2.8.0),它指向另一个名为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

我试过谷歌搜索,但没有直接的解决方案(或者没有直接的问题)。我在README for 2.8.0

后面的Gemfile中添加了require: false
group :test do
  gem 'shoulda-matchers', require: false
end

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

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

要求'rspec / rails'默认情况下需要'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

我真的很感激任何帮助。

2 个答案:

答案 0 :(得分:3)

从您提供的链接:

注意:新配置语法尚未在公开发布中提供 - 有关当前安装说明,请参阅自述文件2.8.0。

该说明来自主分支。当前版本(2.8.0)有一组不同的文档。令我感到困惑,我知道。

只需从spec/spec_helper.rb删除该配置部分,所有部分都应该再次成为彩虹和独角兽。

答案 1 :(得分:0)

只需将这些行插入[[0,4,8],[1,5,9],[2,6,10],[3,7,11]]

即可
spec/spec_helper.rb

参考:

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