0

ENV variable values are set in config/application.yml in a rails app. is it possible to change only for specific test case in ActionDispatch::IntegrationTest? Something like this:

class DummyTest < ActionDispatch::IntegrationTest
  context '...' do
    it '...' do
      ENV['API_URL'] = ...
    end
  end
end

I have tried this but it doesn't seem to change anything. Thanks

nanakondor
  • 615
  • 11
  • 25

1 Answers1

1

I think you've missed creating-rails-environments of the configuration on the Rails documentation.

This should solve your issue without monkey patching anything on your specs.

I would also suggest to use dotenv gem which help you with environment variables. In your case you'll have at least those files:

  • .env.development
  • .env.test

If you really want to update a value for a specific test, I would suggest to stub it like suggested here

brcebn
  • 1,571
  • 1
  • 23
  • 46
  • oh yes, I know it is possible to have multiple environments in the configuration. but in this case, I have an environment variable value that I only want to be applied on this specific testcase instead of the whole test environment. not sure if its the best approach though – nanakondor Apr 21 '22 at 12:26
  • Oh ok. Let me update my answer. – brcebn Apr 21 '22 at 12:27