.env没有加载到Rails的rspec的测试环境中 [英] .env not being loaded in test environment in Rails with rspec

查看:132
本文介绍了.env没有加载到Rails的rspec的测试环境中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 gem'dotenv-rails','〜> 0.9.0'将环境变量加载到Rails 4.0.5应用程序中。我有一个.env文件以及.env.test。所有这些都在开发中运行良好,但是当我使用rspec进行测试时,它无法设置环境变量。

I am using gem 'dotenv-rails', '~> 0.9.0' to load environment variables into a Rails 4.0.5 app. I have a .env file along with a .env.test. All is working well in development, however when it comes to testing, which I do with rspec, it is failing to set the environment variables.

我已经进入了Rails控制台在测试环境中,我可以看到它们设置为零。

I have gone into Rails console in testing environment and I can see they are set to nil.

有关如何让dotenv加载测试的任何提示?

Any tips on how to get dotenv to load in testing?

推荐答案

Dotenv.load(File.expand_path("../../.env.#{Rails.env}", __FILE__))

在研究Paritosh的答案时,我发现该文件需要指定每个环境。

upon researching Paritosh's answer I found that the file needed to specified per environment.

这篇关于.env没有加载到Rails的rspec的测试环境中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆