Rails应用程序在测试运行时是否应加载其.env文件?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Rails应用程序在测试运行时是否应加载其.env文件?相关的知识,希望对你有一定的参考价值。

我的应用程序在运行其测试套件时应该调用Dotenv.load吗?

在运行我的测试套件时不调用Dotenv.load,rspec失败并显示错过环境密钥的错误。我已经粘贴了错误及其堆栈跟踪。

$ rspec

An error occurred while loading ./spec/requests/users/create_spec.rb.
Failure/Error: require File.expand_path('../../config/environment', __FILE__)

KeyError:
  key not found: "REDIS_URL"
# (erb):11:in `fetch'
# (erb):11:in `<main>'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/application.rb:233:in `config_for'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/actioncable-5.1.4/lib/action_cable/engine.rb:31:in `block (2 levels) in <class:Engine>'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/lazy_load_hooks.rb:69:in `instance_eval'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/lazy_load_hooks.rb:69:in `block in execute_hook'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/lazy_load_hooks.rb:60:in `with_execution_control'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/lazy_load_hooks.rb:65:in `execute_hook'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/lazy_load_hooks.rb:50:in `block in run_load_hooks'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/lazy_load_hooks.rb:49:in `each'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/lazy_load_hooks.rb:49:in `run_load_hooks'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/actioncable-5.1.4/lib/action_cable/server/base.rb:85:in `<module:Server>'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/actioncable-5.1.4/lib/action_cable/server/base.rb:4:in `<module:ActionCable>'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/actioncable-5.1.4/lib/action_cable/server/base.rb:3:in `<top (required)>'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `require'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `block in require'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:258:in `load_dependency'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `require'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/actioncable-5.1.4/lib/action_cable.rb:44:in `server'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/actioncable-5.1.4/lib/action_cable/engine.rb:46:in `block (3 levels) in <class:Engine>'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/actionpack-5.1.4/lib/action_dispatch/routing/route_set.rb:426:in `instance_exec'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/actionpack-5.1.4/lib/action_dispatch/routing/route_set.rb:426:in `eval_block'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/actionpack-5.1.4/lib/action_dispatch/routing/route_set.rb:443:in `block in clear!'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/actionpack-5.1.4/lib/action_dispatch/routing/route_set.rb:443:in `each'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/actionpack-5.1.4/lib/action_dispatch/routing/route_set.rb:443:in `clear!'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/application/routes_reloader.rb:50:in `block in clear!'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/application/routes_reloader.rb:48:in `each'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/application/routes_reloader.rb:48:in `clear!'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/application/routes_reloader.rb:17:in `reload!'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/application/routes_reloader.rb:41:in `block in updater'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/file_update_checker.rb:81:in `execute'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/application/routes_reloader.rb:42:in `updater'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/application/routes_reloader.rb:31:in `execute_if_updated'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/application/finisher.rb:128:in `block in <module:Finisher>'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/initializable.rb:30:in `instance_exec'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/initializable.rb:30:in `run'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/initializable.rb:59:in `block in run_initializers'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/initializable.rb:58:in `run_initializers'
# /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/application.rb:353:in `initialize!'
# ./config/environment.rb:5:in `<top (required)>'
# ./spec/rails_helper.rb:4:in `require'
# ./spec/rails_helper.rb:4:in `<top (required)>'
# ./spec/requests/users/create_spec.rb:1:in `require'
# ./spec/requests/users/create_spec.rb:1:in `<top (required)>'
答案

在Rails应用程序之外,您需要执行以下操作:

require 'dotenv/tasks'

task start: [:dotenv] do
  # this Rake tasks depends on the dotenv  loading task
  puts 'hi'
end

在Rails应用程序内:

因为你实际上在Rails应用程序中,我会确保你在你的Gemfile中使用dotenv-rails,而不仅仅是dotenv。前者将自动运行dotenv内容作为Rails启动例程的一部分

以上是关于Rails应用程序在测试运行时是否应加载其.env文件?的主要内容,如果未能解决你的问题,请参考以下文章

在 Rails 中将页面加载到内存中

Rails 4在“测试”环境中发送电子邮件

测试时按特定顺序加载 Rails 夹具

Rails不会替换database.yml中ENV的值

Bitnami Redmine插件记录

没有路线匹配 [GET] /assets