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

时间:2016-05-26 19:34:23

标签: activerecord ruby-on-rails-4.2

有一个奇怪的问题......我的测试继续针对我的开发数据库运行。我在binding.pry点试了test_helper.rb

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

注意到三件事:

  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
    
  4. 此外,ActiveRecord::Base.configurations[Rails.env]会返回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

    帮助!

1 个答案:

答案 0 :(得分:0)

尝试从环境变量中删除DATABASE_URL