Skip to content

Instantly share code, notes, and snippets.

@edgar
Last active April 3, 2019 12:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edgar/8d4e232b061d5a0c8472882a7b73d08e to your computer and use it in GitHub Desktop.
Save edgar/8d4e232b061d5a0c8472882a7b73d08e to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
module ErrorResponses
def rails_respond_without_detailed_exceptions
env_config = Rails.application.env_config
original_show_exceptions = env_config['action_dispatch.show_exceptions']
original_show_detailed_exceptions = env_config['action_dispatch.show_detailed_exceptions']
env_config['action_dispatch.show_exceptions'] = true
env_config['action_dispatch.show_detailed_exceptions'] = false
yield
ensure
env_config['action_dispatch.show_exceptions'] = original_show_exceptions
env_config['action_dispatch.show_detailed_exceptions'] = original_show_detailed_exceptions
end
end
RSpec.configure do |config|
config.include ErrorResponses
end
require 'rails_helper'
RSpec.describe 'My API' do
let(:request) do
lambda do
get '/my_api/value-cannot-be-routed-due-to-route-constraints'
end
end
context 'when action_dispatch.show_exceptions is false (default behavior on test env)' do
it 'raise a routing error' do
expect { request.call }.to raise_error(ActionController::RoutingError)
end
end
context 'when action_dispatch.show_exceptions is true' do
it 'return a 404' do
rails_respond_without_detailed_exceptions do
request.call
end
expect(response.status).to eq(404)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment