【问题标题】:Confused - spec_helper.rb:94:in `<top (required)>': uninitialized constant Shoulda (NameError)困惑 - spec_helper.rb:94:in `<top (required)>': uninitialized constant Shoulda (NameError)
【发布时间】:2015-07-08 05:00:14
【问题描述】:

我正在使用 Rails 4 和 Mongoid 4 开发一个项目。我正在尝试在 thoughtbot/shoulda-matchers 之后设置 Shoulda-matchers(版本 2.8.0),它指向另一个名为 README for 2.8.0 的 README。我希望使用mongoid-rspec 进行测试。

但是我不断收到spec_helper.rb:94:in '&lt;top (required)&gt;': 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,紧随README for 2.8.0

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

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

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


    【解决方案1】:

    从您提供的链接:

    注意:新的配置语法尚未在公开版本中提供——请参阅 2.8.0 的 README 以了解当前的安装说明。

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

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

    【讨论】:

    • 删除什么??
    【解决方案2】:

    只需将这些行插入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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多