[Fix #2277] Do not crash when creating a service with invalid data

This commit is contained in:
gregoirenovel 2018-07-19 15:29:06 +02:00 committed by Mathieu Magnin
parent fa4248967e
commit abbd4e3c69
2 changed files with 6 additions and 3 deletions

View file

@ -17,6 +17,7 @@ module NewAdministrateur
redirect_to services_path(procedure_id: params[:procedure_id]),
notice: "#{new_service.nom} créé"
else
@procedure = procedure
flash[:alert] = new_service.errors.full_messages
render :new
end
@ -34,6 +35,7 @@ module NewAdministrateur
redirect_to services_path(procedure_id: params[:procedure_id]),
notice: "#{@service.nom} modifié"
else
@procedure = procedure
flash[:alert] = @service.errors.full_messages
render :edit
end

View file

@ -1,5 +1,6 @@
describe NewAdministrateur::ServicesController, type: :controller do
let(:admin) { create(:administrateur) }
let(:procedure) { create(:procedure, administrateur: admin) }
describe '#create' do
before do
@ -38,7 +39,7 @@ describe NewAdministrateur::ServicesController, type: :controller do
end
context 'when submitting an invalid service' do
let(:params) { { service: { nom: 'super service' } } }
let(:params) { { service: { nom: 'super service' }, procedure_id: procedure.id } }
it { expect(flash.alert).not_to be_nil }
it { expect(response).to render_template(:new) }
@ -54,7 +55,7 @@ describe NewAdministrateur::ServicesController, type: :controller do
params = {
id: service.id,
service: service_params,
procedure_id: 12
procedure_id: procedure.id
}
patch :update, params: params
end
@ -64,7 +65,7 @@ describe NewAdministrateur::ServicesController, type: :controller do
it { expect(flash.notice).to eq('nom modifié') }
it { expect(Service.last.nom).to eq('nom') }
it { expect(Service.last.type_organisme).to eq('region') }
it { expect(response).to redirect_to(services_path(procedure_id: 12)) }
it { expect(response).to redirect_to(services_path(procedure_id: procedure.id)) }
end
context 'when updating a service with invalid data' do