diff --git a/app/mailers/activity_mailer.rb b/app/mailers/activity_mailer.rb index b21a6a6ff47..cd277e45ef5 100644 --- a/app/mailers/activity_mailer.rb +++ b/app/mailers/activity_mailer.rb @@ -306,9 +306,10 @@ def first_report(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/mailers/activity_mailer_test.rb b/test/mailers/activity_mailer_test.rb index bb4ca47c9a7..45d10a3d0af 100644 --- a/test/mailers/activity_mailer_test.rb +++ b/test/mailers/activity_mailer_test.rb @@ -823,7 +823,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