Trying to change environment variable within RSpec model spec
Try it
module TopDogCore::Concerns::UserValidations
extend ActiveSupport::Concern
included do
validates :username,
presence: true,
uniqueness: true, if: -> { ENV['APP_FOR'] == 'app-1' }
validates :email,
presence: true,
uniqueness: true, if: -> { ENV['APP_FOR'] == 'app-2' }
end
end
RSpec - Best Way to Set Env Variable
Env vars aren't really supposed to be changed at runtime. So, calling ENV['IS_OFF'] = "NO"
in your tests isn't really a good idea. The problem is, env vars are a global configuration, so if you change it from one test, the value will still be changed when you run the other test.
The easiest way to do this is probably stub_const, which will temporarily change the value of the constant, e.g. stub_const "Cryption::Crypt", "OFF_ENCRYPT", "false"
. This is the best option, imo.
But for the sake of completeness, I can show you a way to actually change the environment variable from a specific test. First you would need to change the constant to a method, e.g. def off_encrypt; ENV["OFF_ENCRYPT"]; end
. Either that or change the encrypt/decrypt
methods to just read the env var directly, e.g. not from a constant.
Then, in your tests, you could do something like this:
around(:each) do |example|
orig_value = ENV["OFF_ENCRYPT"]
ENV["OFF_ENCRYPT"] = "true" # set the new value
example.run
ensure
ENV["OFF_ENCRYPT"] = orig_value
end
Again, I wouldn't really recommend this, but it is possible.
How to load environment variables to ruby app spec using Rspec and .env
In your test helper file, you can add:
require 'dotenv'
Dotenv.load('.env.test.local')
This is on the dotenv readme
How do I get an ENV variable set for rspec?
You can use the dotenv gem --- it'll work the same as foreman and load from a .env file. (and a .env.test file for your test environments)
https://github.com/bkeepers/dotenv
What is the best way to write specs for code that depends on environment variables?
That would work.
Another way would be to put a layer of indirection between your code and the environment variables, like some sort of configuration object that's easy to mock.
How to set different environment variables for test and development when using Foreman
The .env
file will be used by default, but you can make a second file like .env2
and use it with foreman start --env .env2
.
If you are using the dotenv-rails gem to load environment variables, .env.test.local
will be automatically loaded for just the test environment.
Related Topics
Monitor Ruby Processes with Monit
Catching Command-Line Errors Using %X
Should Gemfile.Lock Be Committed to Source Control on Windows
How to Prevent Rails Controller Generator to Modify Config/Routes.Rb
Using Fork in Windows with Ruby
How Is Ruby Tcpsocket Timeout Defined
How to Render a Layout Directly from Routes.Rb, Without a Controller
Rake Test Very Slow in Windows
Unexpected Keyword_End, Expecting $End (Syntaxerror)
How to Change Environment Variables When Running Rspec for Ruby
Ruby Minitest Assert_Output Syntax
How to Define a Method in Ruby Using Splat and an Optional Hash at the Same Time
How to Make Rake Db:Migrate Generate Schema.Rb When Using :SQL Schema Format
Ruby: Automatically Set Instance Variable as Method Argument
Svg Files in Raphael, Can They Be Used
Does It Matter If a Conditional Statement Comes Before or After the Expression