Last active
April 3, 2019 12:08
-
-
Save edgar/8d4e232b061d5a0c8472882a7b73d08e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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