Skip to content
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

Merged
merged 1 commit into from
Sep 27, 2017
Merged

Add assert_enqueued_email_with to ActionMailer::TestHelper #30695

merged 1 commit into from
Sep 27, 2017

Conversation

mikker
Copy link
Contributor

@mikker mikker commented Sep 24, 2017

Summary

A proposal to add assert_enqueued_email_with to ActionMailer::TestHelper. We already have assert_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 with bundle 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.

@rails-bot
Copy link

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.

Copy link
Contributor

@kaspth kaspth left a 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,
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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.

@javan
Copy link
Contributor

javan commented Sep 26, 2017

r? @georgeclaghorn

@rails-bot rails-bot assigned georgeclaghorn and unassigned javan Sep 26, 2017
@mikker
Copy link
Contributor Author

mikker commented Sep 27, 2017

I took your second suggestion @georgeclaghorn – thanks for your feedback!

# to be enqueued.
#
# def test_emails_again
# assert_enqueued_email_with ContactMailer, :welcome
Copy link

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙄

Copy link
Contributor

@georgeclaghorn georgeclaghorn left a 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.
Copy link
Contributor

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
Copy link
Contributor

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.
Copy link
Contributor

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.

@mikker
Copy link
Contributor Author

mikker commented Sep 27, 2017

@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
Copy link
Contributor

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?

Copy link
Contributor Author

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.

@georgeclaghorn
Copy link
Contributor

Last thing: this needs a changelog entry. Mind adding one?

@mikker
Copy link
Contributor Author

mikker commented Sep 27, 2017

Of course! Like so?

@georgeclaghorn
Copy link
Contributor

The entry itself looks good, but new entries should be added to the top of the file.

@mikker
Copy link
Contributor Author

mikker commented Sep 27, 2017

Does it show that this is my first contribution?

@georgeclaghorn georgeclaghorn merged commit 212cf23 into rails:master Sep 27, 2017
@georgeclaghorn
Copy link
Contributor

Thanks!

@kaspth
Copy link
Contributor

kaspth commented Sep 28, 2017

@mikker looks like http://guides.rubyonrails.org/contributing_to_ruby_on_rails.html#running-tests could mention bin/test instead of the rake test task 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants