From 336d30a5274d4d90650c390971ee4777f6a691d5 Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Thu, 25 Sep 2025 19:16:56 +0530 Subject: [PATCH] style: use verified doubles and remove receive_message_chain usage --- .../email/send_on_email_service_spec.rb | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/spec/services/email/send_on_email_service_spec.rb b/spec/services/email/send_on_email_service_spec.rb index 348df43a2..81e892e5e 100644 --- a/spec/services/email/send_on_email_service_spec.rb +++ b/spec/services/email/send_on_email_service_spec.rb @@ -9,15 +9,25 @@ describe Email::SendOnEmailService do let(:service) { described_class.new(message: message) } describe '#perform' do + let(:mailer_context) { instance_double(ConversationReplyMailer) } + let(:delivery) { instance_double(ActionMailer::MessageDelivery) } + + before do + allow(ConversationReplyMailer).to receive(:with).with(account: message.account).and_return(mailer_context) + end + context 'when message is email notifiable' do before do - allow(ConversationReplyMailer).to receive_message_chain(:with, :email_reply, :deliver_now) + allow(mailer_context).to receive(:email_reply).with(message).and_return(delivery) + allow(delivery).to receive(:deliver_now) end it 'sends email via ConversationReplyMailer' do service.perform expect(ConversationReplyMailer).to have_received(:with).with(account: message.account) + expect(mailer_context).to have_received(:email_reply).with(message) + expect(delivery).to have_received(:deliver_now) end end @@ -25,24 +35,28 @@ describe Email::SendOnEmailService do let(:message) { create(:message, conversation: conversation, message_type: 'incoming') } before do - allow(ConversationReplyMailer).to receive_message_chain(:with, :email_reply, :deliver_now) + allow(mailer_context).to receive(:email_reply) end it 'does not send email' do service.perform expect(ConversationReplyMailer).not_to have_received(:with) + expect(mailer_context).not_to have_received(:email_reply) end end context 'when an error occurs' do let(:error_message) { 'SMTP connection failed' } let(:error) { StandardError.new(error_message) } + let(:exception_tracker) { instance_double(ChatwootExceptionTracker, capture_exception: true) } + let(:status_service) { instance_double(Messages::StatusUpdateService, perform: true) } before do - allow(ConversationReplyMailer).to receive_message_chain(:with, :email_reply, :deliver_now).and_raise(error) - allow(ChatwootExceptionTracker).to receive(:new).and_return(double(capture_exception: true)) - allow(Messages::StatusUpdateService).to receive(:new).and_return(double(perform: true)) + allow(mailer_context).to receive(:email_reply).with(message).and_return(delivery) + allow(delivery).to receive(:deliver_now).and_raise(error) + allow(ChatwootExceptionTracker).to receive(:new).and_return(exception_tracker) + allow(Messages::StatusUpdateService).to receive(:new).and_return(status_service) end it 'captures the exception' do