You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Improvements to assert_enqueued_email_with (rails#45752)
* This is an attempt to make the assert_enqueued_email_with easier to implement.
* Update actionmailer/test/test_helper_test.rb
Fix spelling.
* Documenting additional tests
* Missing a closing "end"
* Renaming tests for consistency
* Updating name
* Naming and documentation
* Leaving original test unchanged
* Fix test name, add new test
* Add assert_enqueued_emails examples to Rails guide
* Add example to test_helper
* Tweaking the Rails guide (#3)
* Updating Rails guide for consistency.
Co-authored-by: Bry <[email protected]>
Co-authored-by: Ron Shinall <[email protected]>
Copy file name to clipboardExpand all lines: guides/source/testing.md
+85-18Lines changed: 85 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1765,14 +1765,9 @@ class UserMailerTest < ActionMailer::TestCase
1765
1765
end
1766
1766
```
1767
1767
1768
-
In the test we create the email and store the returned object in the `email`
1769
-
variable. We then ensure that it was sent (the first assert), then, in the
1770
-
second batch of assertions, we ensure that the email does indeed contain what we
1771
-
expect. The helper `read_fixture` is used to read in the content from this file.
1768
+
In the test we create the email and store the returned object in the `email` variable. We then ensure that it was sent (the first assert), then, in the second batch of assertions, we ensure that the email does indeed contain what we expect. The helper `read_fixture` is used to read in the content from this file.
1772
1769
1773
-
NOTE: `email.body.to_s`is present when there's only one (HTML or text) part present.
1774
-
If the mailer provides both, you can test your fixture against specific parts
1775
-
with `email.text_part.body.to_s` or `email.html_part.body.to_s`.
1770
+
NOTE: `email.body.to_s`is present when there's only one (HTML or text) part present. If the mailer provides both, you can test your fixture against specific parts with `email.text_part.body.to_s` or `email.html_part.body.to_s`.
1776
1771
1777
1772
Here's the content of the `invite` fixture:
1778
1773
@@ -1784,17 +1779,89 @@ You have been invited.
1784
1779
Cheers!
1785
1780
```
1786
1781
1787
-
This is the right time to understand a little more about writing tests for your
1788
-
mailers. The line `ActionMailer::Base.delivery_method = :test` in
1789
-
`config/environments/test.rb`sets the delivery method to test mode so that
1790
-
email will not actually be delivered (useful to avoid spamming your users while
1791
-
testing) but instead it will be appended to an array
1792
-
(`ActionMailer::Base.deliveries`).
1782
+
This is the right time to understand a little more about writing tests for your mailers. The line `ActionMailer::Base.delivery_method = :test` in `config/environments/test.rb` sets the delivery method to test mode so that email will not actually be delivered (useful to avoid spamming your users while testing) but instead it will be appended to an array (`ActionMailer::Base.deliveries`).
1793
1783
1794
-
NOTE: The `ActionMailer::Base.deliveries` array is only reset automatically in
NOTE: The `ActionMailer::Base.deliveries` array is only reset automatically in `ActionMailer::TestCase` and `ActionDispatch::IntegrationTest` tests. If you want to have a clean slate outside these test cases, you can reset it manually with: `ActionMailer::Base.deliveries.clear`
1785
+
1786
+
#### Testing Enqueued Emails
1787
+
1788
+
You can use the `assert_enqueued_email_with` assertion to confirm that the email has been enqueued with all of the expected mailer method arguments and/or parameterized mailer parameters. This allows you to match any email that have been enqueued with the `deliver_later` method.
1789
+
1790
+
As with the basic test case, we create the email and store the returned object in the `email` variable. The following examples include variations of passing arguments and/or parameters.
1791
+
1792
+
This example will assert that the email has been enqueued with the correct arguments:
1793
+
1794
+
```ruby
1795
+
require "test_helper"
1796
+
1797
+
class UserMailerTest < ActionMailer::TestCase
1798
+
test "invite" do
1799
+
# Create the email and store it for further assertions
This example will assert that a parameterized mailer has been enqueued with the correct parameters and arguments. The mailer parameters are passed as `params` and the mailer method arguments as `args`:
1830
+
1831
+
```ruby
1832
+
require "test_helper"
1833
+
1834
+
class UserMailerTest < ActionMailer::TestCase
1835
+
test "invite" do
1836
+
# Create the email and store it for further assertions
# Test that the email got enqueued with the correct mailer parameters
1859
+
assert_enqueued_email_with UserMailer.with(to: "[email protected]"), :create_invite do
1860
+
email.deliver_later
1861
+
end
1862
+
end
1863
+
end
1864
+
```
1798
1865
1799
1866
### Functional and System Testing
1800
1867
@@ -1831,7 +1898,7 @@ class UsersTest < ActionDispatch::SystemTestCase
1831
1898
end
1832
1899
```
1833
1900
1834
-
NOTE: The `assert_emails` method is not tied to a particular deliver method and will work with emails delivered with either the `deliver_now` or `deliver_later` method. If we explicitly want to assert that the email has been enqueued we can use the `assert_enqueued_emails` method. More information can be found in the [documentation here](https://api.rubyonrails.org/classes/ActionMailer/TestHelper.html).
1901
+
NOTE: The `assert_emails` method is not tied to a particular deliver method and will work with emails delivered with either the `deliver_now` or `deliver_later` method. If we explicitly want to assert that the email has been enqueued we can use the `assert_enqueued_email_with` ([examples above](#testing_enqueued_emails)) or `assert_enqueued_emails` methods. More information can be found in the [documentation here](https://api.rubyonrails.org/classes/ActionMailer/TestHelper.html).
0 commit comments