Repository: rails/rails
File: railties/test/application/loading_test.rb (Ruby)

Code before:
    require "#{rails_root}/config/environment"
    setup_ar!

    assert_nothing_raised do
      User
    end
  end

  test "load config/environments/environment before Bootstrap initializers" do
    app_file "config/environments/development.rb", <<-RUBY
      Rails.application.configure do
        config.development_environment_loaded = true
      end
    RUBY

    add_to_config <<-RUBY
      config.before_initialize do
        config.loaded = config.development_environment_loaded
      end
    RUBY

    require "#{app_path}/config/environment"
    assert ::Rails.application.config.loaded
  end

  test "Raises RuntimeError when config/environments/ENV.rb file does not exist" do
    assert_raise(RuntimeError, /Rails environment has been set to non_existent_environment but config\/environments\/non_existent_environment.rb does not exist./) { boot_app "non_existent_environment" }
  end

  test "descendants loaded after framework initialization are cleaned on each request if reloading is enabled" do
    add_to_config <<-RUBY
      config.enable_reloading = true
      config.reload_classes_only_on_change = false
    RUBY

    app_file "app/models/post.rb", <<-MODEL
      class Post < ApplicationRecord
      end
    MODEL

    app_file "config/routes.rb", <<-RUBY
      Rails.application.routes.draw do
        get '/load',   to: lambda { |env| Post.all.to_a; [200, {}, [ActiveRecord::Base.descendants.collect(&:to_s).sort.uniq.to_json]] }
        get '/unload', to: lambda { |env| [200, {}, [ActiveRecord::Base.descendants.collect(&:to_s).sort.uniq.to_json]] }
      end
    RUBY

    require "rack/test"
    extend Rack::Test::Methods

    require "#{rails_root}/config/environment"

Review comment (suggestion):
```suggestion
  test "raises RuntimeError when config/environments/ENV.rb file does not exist" do
```

Code after:
    require "#{rails_root}/config/environment"
    setup_ar!

    assert_nothing_raised do
      User
    end
  end

  test "load config/environments/environment before Bootstrap initializers" do
    app_file "config/environments/development.rb", <<-RUBY
      Rails.application.configure do
        config.development_environment_loaded = true
      end
    RUBY

    add_to_config <<-RUBY
      config.before_initialize do
        config.loaded = config.development_environment_loaded
      end
    RUBY

    require "#{app_path}/config/environment"
    assert ::Rails.application.config.loaded
  end

  test "raises RuntimeError when config/environments/ENV.rb file does not exist" do
    msg = <<~MSG.squish
      Rails environment has been set to non_existent_environment but
      config/environments/non_existent_environment.rb does not exist.
    MSG

    assert_raise(RuntimeError, match: msg) { boot_app "non_existent_environment" }
  end

  test "descendants loaded after framework initialization are cleaned on each request if reloading is enabled" do
    add_to_config <<-RUBY
      config.enable_reloading = true
      config.reload_classes_only_on_change = false
    RUBY

    app_file "app/models/post.rb", <<-MODEL
      class Post < ApplicationRecord
      end
    MODEL

    app_file "config/routes.rb", <<-RUBY
      Rails.application.routes.draw do
        get '/load',   to: lambda { |env| Post.all.to_a; [200, {}, [ActiveRecord::Base.descendants.collect(&:to_s).sort.uniq.to_json]] }
        get '/unload', to: lambda { |env| [200, {}, [ActiveRecord::Base.descendants.collect(&:to_s).sort.uniq.to_json]] }
      end
    RUBY