From 4d41177fb9474d93af475d2d89748540fb0cbf8f Mon Sep 17 00:00:00 2001 From: rsmokeUM Date: Wed, 26 Mar 2025 10:33:36 -0400 Subject: [PATCH] Refactor email delivery in ContestInstancesController to improve readability - Update the email delivery method to store the mail object in a variable before calling deliver_later, enhancing code clarity. - Add tests to mock the mailer and verify that emails are sent correctly for each entry, ensuring robust email functionality. --- .../contest_instances_controller.rb | 3 ++- .../contest_instances_controller_spec.rb | 19 +++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/app/controllers/contest_instances_controller.rb b/app/controllers/contest_instances_controller.rb index 4233ba06..6b46946e 100644 --- a/app/controllers/contest_instances_controller.rb +++ b/app/controllers/contest_instances_controller.rb @@ -131,7 +131,8 @@ def send_round_results # Send an email for each entry entries.each do |entry| - ResultsMailer.entry_evaluation_notification(entry, judging_round).deliver_later + mail = ResultsMailer.entry_evaluation_notification(entry, judging_round) + mail.deliver_later email_count += 1 end diff --git a/spec/controllers/contest_instances_controller_spec.rb b/spec/controllers/contest_instances_controller_spec.rb index afaf97f6..8b19b123 100644 --- a/spec/controllers/contest_instances_controller_spec.rb +++ b/spec/controllers/contest_instances_controller_spec.rb @@ -95,11 +95,26 @@ # Configure ActiveJob to use inline adapter for testing ActiveJob::Base.queue_adapter = :inline + + # Mock the mailer to return a proper mail object + allow(ResultsMailer).to receive(:entry_evaluation_notification).and_wrap_original do |original_method, *args| + mail = original_method.call(*args) + allow(mail).to receive(:deliver_now) do + ActionMailer::Base.deliveries << mail + true + end + allow(mail).to receive(:deliver_later) do + ActionMailer::Base.deliveries << mail + true + end + mail + end end it 'sends emails for each entry' do - expect(ResultsMailer).to receive(:entry_evaluation_notification).with(entry1, judging_round).and_call_original - expect(ResultsMailer).to receive(:entry_evaluation_notification).with(entry2, judging_round).and_call_original + # Verify the mailer was called with the correct arguments + expect(ResultsMailer).to receive(:entry_evaluation_notification).with(entry1, judging_round) + expect(ResultsMailer).to receive(:entry_evaluation_notification).with(entry2, judging_round) expect { post :send_round_results, params: {