.env not loading into test environment in Rails with rspec

I use gem 'dotenv-rails', '~> 0.9.0'to load environment variables into a Rails 4.0.5 application. I have a .env file along with .env.test. Everything works well in development, however, when it comes to testing that I do with rspec, it cannot set environment variables.

I went to the Rails console in a test environment, and I see that they are set to zero.

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

+4
source share
3 answers
Dotenv.load(File.expand_path("../../.env.#{Rails.env}", __FILE__))

while researching Paritosh's answer, I found that a file must be specified for each environment.

-1
source

, , , spec_helper.rb

require 'dotenv'
Dotenv.load('.env')

, Dotenv.load , :

Dotenv.load(
  '.env.local',
  '.env.test',
  '.env'
)
+10

put this line in Dotenv::Railtie.loadthe rspec settings or on any page where you want to download the dot env file.

0
source

Source: https://habr.com/ru/post/1584252/


All Articles