5

Our Rails app has a custom 404 and 500 pages:

  match '/500', :to => 'errors#internal_server_error', :via => :all

And we have some specs to make sure it works.

In test.rb:

Rails.application.configure do
  config.consider_all_requests_local     = false
  config.action_dispatch.show_exceptions = true
end

However, during development exceptions are swallowed making it difficult figure out what is happening.

So, for some tests we need the above config, and other specs different config. But the config is set before the test runs so not able to update.

How to update the config for a single spec in a before block?

Rails 6

EDIT: What I've tried:

Shira mentioned mocking, but that does not appear to do anything.

Rails.application.config.consider_all_requests_local     = false
Rails.application.config.action_dispatch.show_exceptions = true

This does work, but only before the first request. After the first request it does not do anything.

Rails.application.config.consider_all_requests_local     = ->{ ENV['...'] }

This does not work.

It seems that the issue is that these configs are used in the middleware and once the app is configured changes are not reflected.

I tried to find a way to re-initialize the Rails app, but it appears that there is no way to do it.

I guess the only way to do it is to monkey patch the middleware to use a Proc with an ENV...

B Seven
  • 44,484
  • 66
  • 240
  • 385

2 Answers2

2

This does work:

Rails.application.env_config['action_dispatch.show_exceptions']          = true
Rails.application.env_config['action_dispatch.show_detailed_exceptions'] = false

Thanks to Eliot Sykes https://www.eliotsykes.com/realistic-error-responses

B Seven
  • 44,484
  • 66
  • 240
  • 385
1

I am not sure if it is possible to actually change the configuration but you can mock this configuration instead:

allow(Rails.application.config).to receive(:consider_all_requests_local).and_return(true)
Shira Elitzur
  • 433
  • 3
  • 9