5

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.

zkytony
  • 1,242
  • 2
  • 18
  • 35

3 Answers3

5

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.

zetetic
  • 47,184
  • 10
  • 111
  • 119
0

Just insert these lines into spec/spec_helper.rb:

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

Reference:

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

Tom Aranda
  • 5,919
  • 11
  • 35
  • 51
0

add these lines to spec/rails_helper.rb

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :rails
  end
end
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 13 '23 at 11:43