From f852fe3ef89a75bc79c02f98acd3427719557dfc Mon Sep 17 00:00:00 2001 From: tomo Date: Thu, 1 Jun 2023 21:47:01 +0900 Subject: [PATCH] =?UTF-8?q?args=E3=81=A7=E3=81=AE=E5=91=BC=E3=81=B3?= =?UTF-8?q?=E5=87=BA=E3=81=97=E3=81=AB=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/mailers/activity_mailer.rb | 5 +++-- test/deliveries/activity_delivery_test.rb | 8 ++++++++ test/mailers/activity_mailer_test.rb | 20 +++++++++++++++++++- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/app/mailers/activity_mailer.rb b/app/mailers/activity_mailer.rb index ccb0a849a85..353d765dfbb 100644 --- a/app/mailers/activity_mailer.rb +++ b/app/mailers/activity_mailer.rb @@ -346,9 +346,10 @@ def signed_up(args = {}) end # required params: answer, receiver - def chose_correct_answer + def chose_correct_answer(args = {}) + @receiver ||= args[:receiver] @user = @receiver - @answer = params[:answer] + @answer = params&.key?(:answer) ? params[:answer] : args[:answer] @link_url = notification_redirector_url( link: question_path(@answer.question, anchor: "answer_#{@answer.id}"), diff --git a/test/deliveries/activity_delivery_test.rb b/test/deliveries/activity_delivery_test.rb index f366a8c260d..605c4d4ce6b 100644 --- a/test/deliveries/activity_delivery_test.rb +++ b/test/deliveries/activity_delivery_test.rb @@ -352,6 +352,14 @@ class ActivityDeliveryTest < ActiveSupport::TestCase receiver: answer.user } + assert_difference -> { AbstractNotifier::Testing::Driver.deliveries.count }, 1 do + ActivityDelivery.notify!(:chose_correct_answer, **params) + end + + assert_difference -> { AbstractNotifier::Testing::Driver.enqueued_deliveries.count }, 1 do + ActivityDelivery.notify(:chose_correct_answer, **params) + end + assert_difference -> { AbstractNotifier::Testing::Driver.deliveries.count }, 1 do ActivityDelivery.with(**params).notify!(:chose_correct_answer) end diff --git a/test/mailers/activity_mailer_test.rb b/test/mailers/activity_mailer_test.rb index 23d27f98a68..e058d7789d5 100644 --- a/test/mailers/activity_mailer_test.rb +++ b/test/mailers/activity_mailer_test.rb @@ -916,7 +916,25 @@ class ActivityMailerTest < ActionMailer::TestCase assert_match(/入会/, email.body.to_s) end - test 'chose_correct_answer' do + test 'chose_correct_answer with using synchronous mailer' do + answer = correct_answers(:correct_answer1) + receiver = answer.user + + ActivityMailer.chose_correct_answer( + answer: answer, + receiver: receiver + ).deliver_now + + assert_not ActionMailer::Base.deliveries.empty? + email = ActionMailer::Base.deliveries.last + query = CGI.escapeHTML({ kind: Notification.kinds[:chose_correct_answer], link: "/questions/#{answer.question.id}#answer_#{answer.id}" }.to_param) + assert_equal ['noreply@bootcamp.fjord.jp'], email.from + assert_equal [receiver.email], email.to + assert_equal "[FBC] #{answer.receiver.login_name}さんの質問【 #{answer.question.title} 】で#{answer.sender.login_name}さんの回答がベストアンサーに選ばれました。", email.subject + assert_match(%r{回答へ}, email.body.to_s) + end + + test 'chose_correct_answer with params using asynchronous mailer' do answer = correct_answers(:correct_answer1) receiver = answer.user