diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2018-03-13 15:17:43 -0400 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2018-03-13 15:18:49 -0400 |
commit | f36e298ca7512fd159686d9feec635cdfb9cd1fa (patch) | |
tree | 352e8d09772933b255642059917652191806f435 /actionmailer | |
parent | bebce8c1a07c1b0628d3568d56ccc1e7bb1943a7 (diff) | |
parent | 96218b45680e210fcda647401cf4feeb60bfe41b (diff) | |
download | rails-f36e298ca7512fd159686d9feec635cdfb9cd1fa.tar.gz rails-f36e298ca7512fd159686d9feec635cdfb9cd1fa.tar.bz2 rails-f36e298ca7512fd159686d9feec635cdfb9cd1fa.zip |
Merge pull request #32231 from gmcgibbon/perform_email_jobs_in_assert_emails
Perform email jobs in #assert_emails
Diffstat (limited to 'actionmailer')
-rw-r--r-- | actionmailer/CHANGELOG.md | 4 | ||||
-rw-r--r-- | actionmailer/lib/action_mailer/test_helper.rb | 12 | ||||
-rw-r--r-- | actionmailer/test/test_helper_test.rb | 22 |
3 files changed, 32 insertions, 6 deletions
diff --git a/actionmailer/CHANGELOG.md b/actionmailer/CHANGELOG.md index c5dbb0f1d4..9fb2e44210 100644 --- a/actionmailer/CHANGELOG.md +++ b/actionmailer/CHANGELOG.md @@ -1,4 +1,6 @@ -## Rails 6.0.0.alpha (Unreleased) ## +* Perform email jobs in `assert_emails`. + + *Gannon McGibbon* * Rails 6 requires Ruby 2.4.1 or newer. 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 |