Search code examples
ruby-on-railsrspecmongoidshoulda

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


I am working on a project with Rails 4 & Mongoid 4. I am trying to set up Shoulda-matchers (version 2.8.0), following thoughtbot/shoulda-matchers, which points to another README called README for 2.8.0. And I am hoping to use the mongoid-rspec for testing.

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

I added this in Gemfile: following thoughtbot/shoulda-matchers

group :test do
  gem 'shoulda-matchers'
end

I also added in spec_helper.rb (Which is where the error comes from) - following thoughtbot/shoulda-matchers

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

I have tried googling but there is no direct solution to this (Or there is no direct problem to this). I added require: false in the Gemfile, following README for 2.8.0

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

I added require 'shoulda/matchers' in the rails_helper.rb. My order of 'requires' is this:

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

require 'rspec/rails' is below require 'spec_helper' by default. And by the README provided on the github page, I should place shoulda\matcher below 'rspec/rails'. I have also tried to place require 'shoulda/matchers' on top of require 'spec_helper' but it didn't work.

My versions:

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

I really appreciate any help.


Solution

  • From the link you provided:

    NOTE: The new configuration syntax isn't available in a public release just yet -- please refer to the README for 2.8.0 for the current installation instructions.

    That note is from the master branch. The current release (2.8.0) has a different set of documentation. Confusing, I know.

    Just remove that configuration section from spec/spec_helper.rb and all should be rainbows and unicorns once again.