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

Implement send_email matcher #2670

Closed
wants to merge 1 commit into from
Closed

Conversation

ka8725
Copy link
Contributor

@ka8725 ka8725 commented Apr 19, 2023

Currently, it already has a have_enqueued_mail matcher, but it's too low-level, requiring attention to details such as job name, mailer name, queue, and so on. While all of that information can be useful, from a business perspective, it's rarely necessary. This suggested matcher aims to improve the situation so that when you use it, you can focus on what matters, such as 'an email with a specific subject and specific text within the body being sent'.

Examples:

@example Positive expectation
  expect { action }.to send_email

@example Negative expectations
  expect { action }.not_to send_email

@example More precise expectation with attributes to match
  expect { action }.to send_email(to: 'test@example.com', subject: 'Confirm email')

Originally that was a gist: https://gist.github.com/ka8725/6053d55be74f15b53cabfc3ac4dc99df

A related LinkedIn post - https://www.linkedin.com/feed/update/urn:li:activity:6990304469477429248?utm_source=share&utm_medium=member_desktop

TODO

  • find a way to surface better the multiple email failure case especially as if you don't match say, subject, you could easily trigger it

Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

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

Nice!

lib/rspec/rails/matchers/send_email.rb Show resolved Hide resolved
lib/rspec/rails/matchers/send_email.rb Outdated Show resolved Hide resolved
lib/rspec/rails/matchers/send_email.rb Outdated Show resolved Hide resolved
spec/rspec/rails/matchers/send_email_spec.rb Show resolved Hide resolved
lib/rspec/rails/matchers/send_email.rb Outdated Show resolved Hide resolved
lib/rspec/rails/matchers/send_email.rb Outdated Show resolved Hide resolved
lib/rspec/rails/matchers/send_email.rb Outdated Show resolved Hide resolved
lib/rspec/rails/matchers/send_email.rb Outdated Show resolved Hide resolved
lib/rspec/rails/matchers/send_email.rb Outdated Show resolved Hide resolved
lib/rspec/rails/matchers/send_email.rb Outdated Show resolved Hide resolved
@pirj
Copy link
Member

pirj commented Apr 19, 2023

A feature spec would be helpful to make this more discoverable.

@ka8725 ka8725 requested a review from pirj April 19, 2023 20:23
features/matchers/send_email_matcher.feature Outdated Show resolved Hide resolved
features/matchers/send_email_matcher.feature Outdated Show resolved Hide resolved
features/matchers/send_email_matcher.feature Outdated Show resolved Hide resolved
lib/rspec/rails/matchers/send_email.rb Outdated Show resolved Hide resolved
spec/support/mailers.rb Outdated Show resolved Hide resolved
lib/rspec/rails/matchers/send_email.rb Outdated Show resolved Hide resolved
lib/rspec/rails/matchers/send_email.rb Outdated Show resolved Hide resolved
@JonRowe
Copy link
Member

JonRowe commented Apr 21, 2023

Thanks for this, it mostly looks great just some readability tweaks, I'm also wondering if theres a way to surface better the multiple email failure case especially as if you don't match say, subject, you could easily trigger it

lib/rspec/rails/matchers/send_email.rb Outdated Show resolved Hide resolved
@@ -0,0 +1,9 @@
RSpec.shared_context "test mailer" do
class TestMailer < ActionMailer::Base
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't like this idea actually, Rubocop is right - https://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Lint/ConstantDefinitionInBlock

Defining classes with let also complicates things.

Classes should be defined within modules. Moving it out to a shared module.

@ka8725
Copy link
Contributor Author

ka8725 commented Apr 21, 2023

@JonRowe to address this point:

find a way to surface better the multiple email failure case especially as if you don't match say, subject, you could easily trigger it

I have an idea to inspect the sent emails. 0e25b66 That should give a clue for devs where the failure comes from. Ideally, that would be great to inspect the body as well, but the output can be very huge. Therefore, I would better not do that. Basically, having just the subject inspected would suffice, but the other important attributes like from/to not hurt.

@ka8725 ka8725 requested a review from JonRowe April 21, 2023 11:15
Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

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

Looks great!
A few nitpicks below.

spec/rspec/rails/matchers/send_email_spec.rb Outdated Show resolved Hide resolved
features/matchers/send_email_matcher.feature Outdated Show resolved Hide resolved
features/matchers/send_email_matcher.feature Outdated Show resolved Hide resolved
lib/rspec/rails/matchers/send_email.rb Outdated Show resolved Hide resolved
spec/rspec/rails/matchers/send_email_spec.rb Outdated Show resolved Hide resolved
lib/rspec/rails/matchers/send_email.rb Outdated Show resolved Hide resolved
Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

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

Thank you so much for the idea and for implementing it!

@ka8725
Copy link
Contributor Author

ka8725 commented Apr 25, 2023

@pirj can the failed CI job be restarted? looks like there was a network issue - https://github.com/rspec/rspec-rails/actions/runs/4790425603/jobs/8522781666?pr=2670

@pirj
Copy link
Member

pirj commented May 1, 2023

Green. It slipped my mind, do we squash commits in meaningful ones before merging?

@ka8725
Copy link
Contributor Author

ka8725 commented May 1, 2023

@pirj I've squashed them.

@ka8725
Copy link
Contributor Author

ka8725 commented May 10, 2023

@JonRowe all things are polished in my opinion, is there something else I can improve in this PR?

Copy link
Member

@JonRowe JonRowe left a comment

Choose a reason for hiding this comment

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

Just a couple of wording tweaks to the feature, and this is good to go, sorry for the delayed review.

JonRowe added a commit that referenced this pull request Jun 26, 2023
@JonRowe
Copy link
Member

JonRowe commented Jun 26, 2023

Merged in 69c09bc thanks for the hard work on this.

@JonRowe JonRowe closed this Jun 26, 2023
@ka8725 ka8725 deleted the send-email-matcher branch June 26, 2023 12:36
@JonRowe
Copy link
Member

JonRowe commented Nov 21, 2023

Released in 6.1.0 apologies for the delay

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

4 participants