diff --git a/app/controllers/commentaires_controller.rb b/app/controllers/commentaires_controller.rb index f711a4ab5..4a0b75662 100644 --- a/app/controllers/commentaires_controller.rb +++ b/app/controllers/commentaires_controller.rb @@ -22,10 +22,11 @@ class CommentairesController < ApplicationController end @commentaire.body = params['texte_commentaire'] - @commentaire.save unless flash.alert + saved = false + saved = @commentaire.save unless flash.alert if is_gestionnaire? - NotificationMailer.new_answer(@commentaire.dossier).deliver_now! + NotificationMailer.new_answer(@commentaire.dossier).deliver_now! if saved redirect_to url_for(controller: 'backoffice/dossiers', action: :show, id: params['dossier_id']) elsif current_user.email != @commentaire.dossier.user.email invite = Invite.where(dossier: @commentaire.dossier, user: current_user).first diff --git a/spec/controllers/backoffice/commentaires_controller_spec.rb b/spec/controllers/backoffice/commentaires_controller_spec.rb index a66229f20..a85f0cc99 100644 --- a/spec/controllers/backoffice/commentaires_controller_spec.rb +++ b/spec/controllers/backoffice/commentaires_controller_spec.rb @@ -91,5 +91,17 @@ describe Backoffice::CommentairesController, type: :controller do end end end + + describe 'comment cannot be saved' do + before do + allow_any_instance_of(Commentaire).to receive(:save).and_return(false) + end + it 'Notification email is not sent' do + expect(NotificationMailer).not_to receive(:new_answer) + expect(NotificationMailer).not_to receive(:deliver_now!) + + post :create, dossier_id: dossier_id, texte_commentaire: texte_commentaire + end + end end end