diff options
author | Gannon McGibbon <gannon.mcgibbon@gmail.com> | 2018-03-12 14:30:25 -0400 |
---|---|---|
committer | Gannon McGibbon <gannon.mcgibbon@shopify.com> | 2018-03-12 15:35:18 -0400 |
commit | 96218b45680e210fcda647401cf4feeb60bfe41b (patch) | |
tree | 7936e43b9fc3cdf2a9d915ac265d115982632397 /actionmailer | |
parent | 309bb6c4d068b0d480681cf4ef1b90158527dfe5 (diff) | |
download | rails-96218b45680e210fcda647401cf4feeb60bfe41b.tar.gz rails-96218b45680e210fcda647401cf4feeb60bfe41b.tar.bz2 rails-96218b45680e210fcda647401cf4feeb60bfe41b.zip |
Perform email jobs in #assert_emails
Perform enqueued delivery jobs in #assert_emails and #assert_no_emails.
Diffstat (limited to 'actionmailer')
-rw-r--r-- | actionmailer/lib/action_mailer/test_helper.rb | 12 | ||||
-rw-r--r-- | actionmailer/test/test_helper_test.rb | 22 |
2 files changed, 29 insertions, 5 deletions
diff --git a/actionmailer/lib/action_mailer/test_helper.rb b/actionmailer/lib/action_mailer/test_helper.rb index 8ee4d06915..ec748236b4 100644 --- a/actionmailer/lib/action_mailer/test_helper.rb +++ b/actionmailer/lib/action_mailer/test_helper.rb @@ -14,7 +14,7 @@ module ActionMailer # assert_emails 0 # ContactMailer.welcome.deliver_now # assert_emails 1 - # ContactMailer.welcome.deliver_now + # ContactMailer.welcome.deliver_later # assert_emails 2 # end # @@ -28,17 +28,19 @@ module ActionMailer # # assert_emails 2 do # ContactMailer.welcome.deliver_now - # ContactMailer.welcome.deliver_now + # ContactMailer.welcome.deliver_later # end # end - def assert_emails(number) + def assert_emails(number, &block) if block_given? original_count = ActionMailer::Base.deliveries.size - yield + perform_enqueued_jobs(only: [ActionMailer::DeliveryJob, ActionMailer::Parameterized::DeliveryJob], &block) new_count = ActionMailer::Base.deliveries.size assert_equal number, new_count - original_count, "#{number} emails expected, but #{new_count - original_count} were sent" else - assert_equal number, ActionMailer::Base.deliveries.size + perform_enqueued_jobs(only: [ActionMailer::DeliveryJob, ActionMailer::Parameterized::DeliveryJob]) do + assert_equal number, ActionMailer::Base.deliveries.size + end end end diff --git a/actionmailer/test/test_helper_test.rb b/actionmailer/test/test_helper_test.rb index 3866097389..8fdc687a8b 100644 --- a/actionmailer/test/test_helper_test.rb +++ b/actionmailer/test/test_helper_test.rb @@ -69,6 +69,16 @@ class TestHelperMailerTest < ActionMailer::TestCase end end + def test_assert_emails_with_enqueued_emails + assert_nothing_raised do + assert_emails 1 do + silence_stream($stdout) do + TestHelperMailer.test.deliver_later + end + end + end + end + def test_repeated_assert_emails_calls assert_nothing_raised do assert_emails 1 do @@ -105,6 +115,18 @@ class TestHelperMailerTest < ActionMailer::TestCase end end + def test_assert_no_emails_with_enqueued_emails + error = assert_raise ActiveSupport::TestCase::Assertion do + assert_no_emails do + silence_stream($stdout) do + TestHelperMailer.test.deliver_later + end + end + end + + assert_match(/0 .* but 1/, error.message) + end + def test_assert_emails_too_few_sent error = assert_raise ActiveSupport::TestCase::Assertion do assert_emails 2 do |