[英]Is it possible to set ENV variables for rails development environment in my code?
[英]ENV variables are not set for test environment
我在database.yml
有一個這樣的測試數據庫集:
test:
adapter: postgresql
encoding: unicode
database: database_test
host: localhost
pool: 5
username: <%= ENV['PG_USERNAME']%>
password: <%= ENV['PG_PASSWORD']%>
我使用spring
gem來運行測試(盡管這並不重要,因為問題發生時也沒有使用它)
問題是每次我運行bundle exec rspec spec/
我得到一個錯誤no password supplied (PG::ConnectionBad)
:
/.bundle/ruby/2.3.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `initialize': fe_sendauth: no password supplied (PG::ConnectionBad)
我需要手動將其更改為:
test:
adapter: postgresql
encoding: unicode
database: database_test
host: localhost
pool: 5
username: postgres
password: postgres
為了它的工作。
我在~/.bashrc
設置了變量:
export PG_USERNAME="postgres"
export PG_PASSWORD="postgres"
奇怪的是,在development
模式下,當我使用與test
相同的結構時,它都能正常工作 :
development:
adapter: postgresql
encoding: unicode
database: database_dev
host: localhost
pool: 5
username: <%= ENV['PG_USERNAME']%>
password: <%= ENV['PG_PASSWORD']%>
我的test
環境有什么問題以及如何修復它以便在開發模式下自動使用ENV['PG_USERNAME']
?
我在評論中提出了一個quickfix,但我建議根據您的配置使用優秀的dotenv gem進行配置加載。 它會偶然解決您當前的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.