RSpec :: How to have Rails request specs handling errors like production

Edgar Gonzalez
1 min readFeb 9, 2017

--

Recently I have been working on fixing our JWT Rack middleware to sign error responses as well (i.e 404, 500, …).

So, to test my changes I needed Rails to handle errors like production when running my request tests. One approach is to set config.action_dispatch.show_exceptions to true for the test environment:

However this will affect the default behavior for the entire test suite, which is not what I wanted, so I need a way to just change the value of action_dispatch.show_exceptions for specific specs. Not hard right? just set the value inside a before block, for instance (lines 17–19):

That works when I run it as an standalone test, however when I run the entire suite it doesn’t work. The issue is that action_dispatch.show_exceptions gets copied and cached in Rails.application.env_config, and changing Rails.application.env_config in a single test-case it’s having side effects and what I should do is restore the original value after the test finishes.

Following @eliotsykes approach the final code will be:

--

--

Edgar Gonzalez

Current iteration: Director of Software Engineering @StreetEasy/@Zillow | Previously: Director DevOps & Data @BlueApron · @Piictu (@TechStars) · gonzalez.io