Rspec reports Rails environment is running in production mode!

Asked 2 years ago, Updated 2 years ago, 112 views

We are currently creating a portfolio (Rails-api, React) and the deployment to production heroku has been completed.
After running the previous Rspec and confirming that there was no problem, I was going to deploy the CI/CD, but I got an error that Rspec didn't appear before, so I couldn't solve it even if I googled it, so I'm posting it.

An error appears after executing the following command:

 docker-compose run api bundle exec rspec

error messages:

Creating cookielog_api_run...done

Error occurred while loading./spec/models/favorite_spec.rb.
Failure/Error: abort("The Rails environment is running in production mode!") if Rails.env.production?

NameError:
  uninitialized constant Rails
# ./spec/rails_helper.rb:7:in`<top(required)>'
# ./spec/models/favorite_spec.rb:1:in `require'
# ./spec/models/favorite_spec.rb:1:in`<top(required)>'

All the tests that require helper below will be shown the same way.

No examples found.

Finished in 0.00015 seconds (files book 0.42682 seconds to load)
0 examples, 0 failures, 8 errors occurred outside of examples

The following description in line 7 of rails_helper.rb below appears to be an error.

 abort("The Rails environment is running in production mode!") if Rails.env.production?

Have you run the test as a production environment?

Tried

Check the rails console for your current environment

[1]pry(main)>Rails.env
=>"development"

When I ran bundle exec rspec in a local environment without using Docker, the error did not change.

I'm confused because I don't know the cause.
I'd appreciate it if you could give me some advice.
Thank you for reading to the end.

rails_helper.rb

#This file is copied to spec/when you run 'rails generate rspec:install'
require "spec_helper"
ENV["RAILS_ENV"]||="test"
# require File.expand_path('../config/environment',__FILE__)
# Comment out due to error
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require "rspec/rails"
# Add additional requirements below this line.Rails is not loaded until this point!

require "capybara/rspec"

Dir [Rails.root.join("spec/support/**/*.rb")] .each {|f|require f}
# Requirements supporting ruby files with custom matches and macros, etc., in
# spec/support/and its subdirectories.Files matching `spec/**/*_spec.rb`are
# run as spec files by default.This means that files in spec / support that end
# in_spec.rb will both be required and run as specifics, causing the specific to be
# runtime.It is recommended that you do not name files matching this globe to
# end with_spec.rb.You can configure this pattern with the -- pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
#
# The following line is provided for convention purpos. It is the downside
# of increasing the boot-up time by auto-requiring all files in the support
# directory.Alternatively, in the individual`*_spec.rb`files, manually
# require only the support files necessary.
#
Dir [Rails.root.join("spec", "support", "**", "*.rb")] .sort.each {|f|require f}

# Checks for pending migrations and apps 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
  exit1
end
RSpec.configure do | config |
  # Remove this line if you're not using ActiveRecord or ActiveRecord fixes
  config.fixure_path="#{:Rails.root}/spec/fixes"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples with a transaction, remove the following line or assign false
  # installed of true.
  config.use_transactional_fixes=true

  # You can comment this line to turn off ActiveRecord support entry.
  # config.use_active_record=false

  # RSpec Rails can automatically mix in different behaviors to your tests
  # based on their file location, for example enabling you to call `get` and
  # `post`inspecs under`spec/controllers`.
  #
  # You can disable this behavior by removing the line below, and install
  # explicitly tag your specifications with their type, e.g.:
  #
  #     RSpec.describe UsersController, type::controller do
  #       # ...
  #     end
  #
  # The different available types are documented in the features, such as in
  # # https://relishapp.com/rspec/rspec-rails/docs
  config.infer_spec_type_from_file_location!

  # Filter lines from Rails gems in backtraces.
  config.filter_rails_from_backtrace!
  # arbitrary gems may be filtered via:
  # config.filter_gems_from_backtrace("gem name")
  config.include FactoryBot::Syntax::Methods
  config.includeDevice::Test::IntegrationHelpers, type::request
  # config.includeDevice::Test::IntegrationHelpers, type::system
  config.include LoginHelpers
  config.include JsonApiHelpers
  # Test Case Common Pre-processing
  config.before(:each)do

    # The value specified in let(:rspec_session) is the initial value of the session.
    session=defined?(rspec_session)?rspec_session:{}

    # Run the destroy method without errors (if necessary).
    session.class_eval {def destroy;nil;end}

    # Allow post-execution sessions to be retrieved
    config.add_setting(:session,:default=>session)

    # Overrides the change session method with RSpec.configuration.session
    allow_any_instance_of (ActionDispatch::Request).to receive(:session).and_return(RSspec.configuration.session)
  end
end

ruby-on-rails rspec

2022-09-30 19:22

1 Answers

docker-composer run api rails generate rspec:install and
The test is now running.

However, the configuration to enable json on rspec still behaves strangely.
It responds as a different error.
Thank you for browsing.


2022-09-30 19:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.