angelica-thd / SaaS

A RestAPI implementation of a to-do list with Ruby on Rails using httpie for testing.
0 stars 0 forks source link

unitialized constant Shoulda (NameError) #1

Closed angelica-thd closed 3 years ago

angelica-thd commented 3 years ago

While working on these API requests I encountered this error: uninitialized constant Shoulda (NameError) in the spec\rails_helper.rb file My Gemfile already had the shoulda-matchers gem and the spec\rails_helper.rb looked something like this


# 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
require 'database_cleaner'

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

RSpec.configure do |config|

  config.include FactoryBot::Syntax::Methods

  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
    DatabaseCleaner.strategy = :transaction
  end

  config.around(:each) do |example|
    DatabaseCleaner.cleaning do
      example.run
    end
  end

end
angelica-thd commented 3 years ago

Update on that: I solved the issue by changing the spec\rails_helper.rb The problem lied on the fact that there was no requirement for the rspec\rails that I used later on and also there was no RAILS_ENV aka no rails environment for the test to run. Basically, don't erase any of the code already written when the rails_helper.rb is made or else you lose the environment variables. updated code to:

ENV['RAILS_ENV'] ||= 'test'

require File.expand_path('../config/environment', __dir__)

# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
require 'database_cleaner'

 Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }

# Checks for pending migrations and applies them before tests are run.
# If you are not using ActiveRecord, you can remove these lines.
begin
  ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError => e
  puts e.to_s.strip
  exit 1
end

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

RSpec.configure do |config|
  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.include RequestSpecHelper, type: :request
  #
  config.use_transactional_fixtures = true
  # add `FactoryBot` methods
    config.include FactoryBot::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

  config.infer_spec_type_from_file_location!

  config.filter_rails_from_backtrace!

end