Shoulda-matchers: Getting NameError: uninitialized constant ActiveSupport::TestCase

Created on 21 Jul 2017  路  6Comments  路  Source: thoughtbot/shoulda-matchers

Hi

I am new newbie to ROR, I creating a Rails 5 API application and stuck with this problem.
This is my git repository; I am following this tutorial for same
At my latest stage whenever I run bundle exec rspec, I get loading errors; all are NameError. Once I fix one it moves to another but same type of error.

Can someone please guide me, what am I doing wrong.
screenshot-vikrant emedinexus-69 -ror-api_applications-todo-api
screenshot-vikrant emedinexus-69 -ror-api_applications-todo-api-1

Most helpful comment

Check the link to the code (repo) at the top of the tutorial (pink button stating Code)
Here is a link to the full rails_helper.rb file on github
You'll notice there is a lot more in the spec/rails_helper.rb

The tutorial only omits this content. So, yes, you need to specify the rails env at the top of the helper

ENV['RAILS_ENV'] ||= 'test'
# and there is a bunch of other stuff...

All 6 comments

Hey @voidviking. Two things here:

  1. The first error indicates that you may not have added shoulda-matchers to your Gemfile. Have you done this?
  2. The second error shouldn't be happening -- that indicates that you're using Minitest in addition to RSpec. Is it possible for you to share your rails_helper.rb so I can get a better look at what you're doing?

@voidviking Were you able to get this to work?

@mcmire, I am having this issue.
I'm using Ruby 2.3.4p301 (2017-03-30 revision 58214) [x86_64-darwin16] and Rails 5.1.3
shoulda-matchers are included in my gemfile like this:

group :development, :test do
  gem 'shoulda-matchers', '~> 3.1'

(I did have shoulda-matchers line in group :test do instead earlier, with the same error.

Here are the errors:

鈾モ櫏鈾モ櫏鈾モ櫏鈾モ櫏鈾モ櫏鈾モ櫏 bundle exec rspec

An error occurred while loading ./spec/models/meal_spec.rb.
Failure/Error:
  Shoulda::Matchers.configure do |config|
    config.integrate do |with|
      with.test_framework :rspec
      with.library :rails
    end
  end

NameError:
  uninitialized constant Shoulda
# ./spec/rails_helper.rb:6:in `<top (required)>'
# ./spec/models/meal_spec.rb:1:in `require'
# ./spec/models/meal_spec.rb:1:in `<top (required)>'

An error occurred while loading ./spec/models/user_spec.rb.
Failure/Error:
  Shoulda::Matchers.configure do |config|
    config.integrate do |with|
      with.test_framework :rspec
      with.library :rails
    end
  end

NameError:
  uninitialized constant Shoulda
# ./spec/rails_helper.rb:6:in `<top (required)>'
# ./spec/models/user_spec.rb:1:in `require'
# ./spec/models/user_spec.rb:1:in `<top (required)>'
No examples found.


Finished in 0.00019 seconds (files took 0.11059 seconds to load)
0 examples, 0 failures, 2 errors occurred outside of examples

Here is my rails_helper:

# require database cleaner at the top level
require 'database_cleaner'

# [...]
# configure shoulda matchers to use rspec as the test framework and full matcher libraries for rails
Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :rails
  end
end

# [...]
RSpec.configuration do |config|
  # [...]
  # add `FactoryGirl` methods
  config.include FactoryGirl::Syntax::Methods

  # start by truncating all the tables but then use the faster transaction strategy the rest of the time.
  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
    DatabaseCleaner.strategy = :transaction
  end

  # start the transaction strategy as examples are run
  config.around(:each) do |example|
    DatabaseCleaner.cleaning do
      example.run
    end
  end
  # [...]
end

@HeidiHansen Hmm. That's very odd. Are you loading your Rails environment file at the top of rails_helper?

Check the link to the code (repo) at the top of the tutorial (pink button stating Code)
Here is a link to the full rails_helper.rb file on github
You'll notice there is a lot more in the spec/rails_helper.rb

The tutorial only omits this content. So, yes, you need to specify the rails env at the top of the helper

ENV['RAILS_ENV'] ||= 'test'
# and there is a bunch of other stuff...

Cool. It sounds like you were able to spot the discrepancy here, so I'm going to close this :)

Was this page helpful?
0 / 5 - 0 ratings