demarches-normaliennes/spec/controllers/errors_controller_spec.rb

60 lines
1.5 KiB
Ruby
Raw Normal View History

2024-04-08 23:36:18 +02:00
RSpec.describe ErrorsController, type: :controller do
render_views
describe 'GET #show' do
# rspec can't easily manage the exceptions_app for a real route,
# just verify the action renders correctly
let(:status_code) { 426 }
let(:status_message) { 'Upgrade Required' }
context 'HTML format' do
subject do
get :show, params: { status: status_code }, format: :html
end
it 'correctly handles and responds with an HTML response' do
subject
expect(response).to have_http_status(status_code)
expect(response.body).to include(status_message)
end
end
context 'JSON format' do
subject do
get :show, params: { status: status_code }, format: :json
end
it 'correctly handles and responds with a JSON response' do
subject
expect(response).to have_http_status(status_code)
json_response = response.parsed_body
expect(json_response['status']).to eq(status_code)
expect(json_response['name']).to eq(status_message)
end
end
end
shared_examples 'specific action' do
subject { get action_name }
it do
is_expected.to have_http_status(status_code)
end
context "404" do
let(:status_code) { 404 }
let(:action_name) { :not_found }
it_behaves_like 'specific action'
end
2024-04-09 00:00:31 +02:00
context "422" do
let(:status_code) { 422 }
let(:action_name) { :unprocessable_entity }
it_behaves_like 'specific action'
end
2024-04-08 23:36:18 +02:00
end
end