-
Notifications
You must be signed in to change notification settings - Fork 21.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add assert_enqueued_email_with to ActionMailer::TestHelper #30695
Add assert_enqueued_email_with to ActionMailer::TestHelper #30695
Conversation
Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @javan (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review. Please see the contribution instructions for more information. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Haven't had a need for this myself. So I'll defer merge decision to someone else. 😊
@georgeclaghorn you added the assert_enqueued_emails
way back when. How does this strike you?
args = args && [mailer.to_s, method.to_s, "deliver_now", *args] | ||
|
||
assert_enqueued_with( | ||
job: ActionMailer::DeliveryJob, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mailers can also be enqueued with ActionMailer::Parameterized::DeliveryJob
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, @kaspth! Suggestions on how to handle both are very welcome.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could add a separate helper: assert_enqueued_parameterized_email_with
.
Alternatively, this helper could assert that an ActionMailer::Parameterized::DeliveryJob
is enqueued if args
is Hash-like or responds to to_hash
.
I took your second suggestion @georgeclaghorn – thanks for your feedback! |
# to be enqueued. | ||
# | ||
# def test_emails_again | ||
# assert_enqueued_email_with ContactMailer, :welcome |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert_enqueued_email_with ContactMailer, :welcome do
you forgot do
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🙄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please squash your commits into one?
@@ -93,6 +93,43 @@ def assert_enqueued_emails(number, &block) | |||
assert_enqueued_jobs number, only: [ ActionMailer::DeliveryJob, ActionMailer::Parameterized::DeliveryJob ], &block | |||
end | |||
|
|||
# Asserts that a specific email has been enqueued, optionally | |||
# also matching arguments. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✂️ also
# def test_emails | ||
# ContactMailer.welcome.deliver_later | ||
# assert_enqueued_email_with ContactMailer, :welcome | ||
# end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that the description above alludes to passing args
, we should include an example of that.
# end | ||
# end | ||
# | ||
# If a hash is passed as arguments the email is presumed to be Paramererized. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If `args` is provided as a Hash, a parameterized email is matched.
@georgeclaghorn Done and done. Thanks! |
assert_enqueued_email_with TestHelperMailer, :test_parameter_args, args: { all: "good" } do | ||
silence_stream($stdout) do | ||
TestHelperMailer.with(all: "good").test_parameter_args.deliver_later | ||
puts enqueued_jobs.inspect |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this puts
needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh god forgot that in there. I need to concentrate.
Last thing: this needs a changelog entry. Mind adding one? |
Of course! Like so? |
The entry itself looks good, but new entries should be added to the top of the file. |
Does it show that this is my first contribution? |
Thanks! |
@mikker looks like http://guides.rubyonrails.org/contributing_to_ruby_on_rails.html#running-tests could mention |
Summary
A proposal to add
assert_enqueued_email_with
to ActionMailer::TestHelper. We already haveassert_enqueued_email(amount)
and although that certainly is sufficient for most cases it's sometimes nice to be a little bit more specific in what you're expecting.I couldn't get the test suite to run without an exception that I couldn't diagnose so I hope this passes CI.Got the suite to run withbundle exec
🙄@kaspth eventually told me about
bin/test
. Is this documented anywhere? 😄Should probably add a test case for something with arguments as well.Added a test with arguments. Added support for parameterized emails.