From 9d63111499164884635462bb09f5de979e7bffb9 Mon Sep 17 00:00:00 2001 From: arktisklada Date: Fri, 22 Apr 2016 07:51:39 -0500 Subject: [PATCH] Clear ActionMailer deliveries on setup and teardown Signed-off-by: Jeremy Daer --- actionmailer/lib/action_mailer/test_case.rb | 5 ++- actionmailer/test/test_case_test.rb | 38 +++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/actionmailer/lib/action_mailer/test_case.rb b/actionmailer/lib/action_mailer/test_case.rb index 65e7347ae4..b045e883ad 100644 --- a/actionmailer/lib/action_mailer/test_case.rb +++ b/actionmailer/lib/action_mailer/test_case.rb @@ -15,10 +15,12 @@ module ActionMailer extend ActiveSupport::Concern included do + setup :clear_test_deliveries teardown :clear_test_deliveries end private + def clear_test_deliveries if ActionMailer::Base.delivery_method == :test ActionMailer::Base.deliveries.clear @@ -76,6 +78,7 @@ module ActionMailer set_delivery_method :test @old_perform_deliveries = ActionMailer::Base.perform_deliveries ActionMailer::Base.perform_deliveries = true + ActionMailer::Base.deliveries.clear end def restore_test_deliveries # :nodoc: @@ -89,6 +92,7 @@ module ActionMailer end def restore_delivery_method # :nodoc: + ActionMailer::Base.deliveries.clear ActionMailer::Base.delivery_method = @old_delivery_method end @@ -114,6 +118,5 @@ module ActionMailer end include Behavior - include ClearTestDeliveries end end diff --git a/actionmailer/test/test_case_test.rb b/actionmailer/test/test_case_test.rb index 86fd37bea6..5d8d3c3b36 100644 --- a/actionmailer/test/test_case_test.rb +++ b/actionmailer/test/test_case_test.rb @@ -3,6 +3,44 @@ require 'abstract_unit' class TestTestMailer < ActionMailer::Base end +class ClearTestDeliveriesMixinTest < ActiveSupport::TestCase + include ActionMailer::TestCase::ClearTestDeliveries + + def before_setup + ActionMailer::Base.delivery_method, @original_delivery_method = :test, ActionMailer::Base.delivery_method + ActionMailer::Base.deliveries << 'better clear me, setup' + super + end + + def after_teardown + super + assert_equal [], ActionMailer::Base.deliveries + ActionMailer::Base.delivery_method = @original_delivery_method + end + + def test_deliveries_are_cleared_on_setup_and_teardown + assert_equal [], ActionMailer::Base.deliveries + ActionMailer::Base.deliveries << 'better clear me, teardown' + end +end + +class MailerDeliveriesClearingTest < ActionMailer::TestCase + def before_setup + ActionMailer::Base.deliveries << 'better clear me, setup' + super + end + + def after_teardown + super + assert_equal [], ActionMailer::Base.deliveries + end + + def test_deliveries_are_cleared_on_setup_and_teardown + assert_equal [], ActionMailer::Base.deliveries + ActionMailer::Base.deliveries << 'better clear me, teardown' + end +end + class CrazyNameMailerTest < ActionMailer::TestCase tests TestTestMailer