Rails 为测试环境加载不正确的数据库配置?

Rails loading incorrect database config for test env?

有一个奇怪的问题...我的测试保持 运行 对我的开发数据库。我在这里 test_helper.rb 中尝试了 binding.pry

# this is the top of the file
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
binding.pry

并注意到 3 件事:

  1. ENV['RAILS_ENV']/Rails.env 已正确设置为 "test"
  2. Rails 配置似乎加载了正确的值:

    Rails.application.config.database_configuration[Rails.env]
    
    # => {"adapter"=>"postgis",
    #     "encoding"=>"unicode",
    #     "pool"=>5,
    #     "username"=>"user",
    #     "host"=>"localhost",
    #     "database"=>"db_test"} <= CORRECT
    
  3. ...但是 ActiveRecord 加载了一个不正确的值:

    ActiveRecord::Base.configurations[Rails.env]
    
    # => {"adapter"=>"postgis",
    #     "encoding"=>"unicode",
    #     "pool"=>5,
    #     "username"=>"user",
    #     "host"=>"localhost",
    #     "port"=>5432,
    #     "database"=>"db_development"} <= INCORRECT
    

此外,ActiveRecord::Base.configurations[Rails.env] returns port 键而 Rails.application.config.database_configuration[Rails.env] 没有。


为什么这些不同?这是我的配置:

default: &default
  adapter: postgis
  encoding: unicode
  pool: 5
  username: user
  host: localhost

development:
  <<: *default
  database: db_development

test:
  <<: *default
  database: db_test

grep 编辑了我的整个项目文件夹并且 config/database.yml 是我命名我的数据库的唯一地方,所以据我所知没有其他配置覆盖这个。

我是 运行 rails 4.2.5

求助!

尝试从您的环境变量中删除 DATABASE_URL