demarches-normaliennes/spec/controllers/errors_controller_spec.rb
2024-04-11 18:19:31 +02:00

52 lines
1.3 KiB
Ruby

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
end
end