refactor(tests): replace mailer mocks with email delivery assertions#2617
Open
mroderick wants to merge 10 commits into
Open
refactor(tests): replace mailer mocks with email delivery assertions#2617mroderick wants to merge 10 commits into
mroderick wants to merge 10 commits into
Conversation
…d of mocks Replaces mock expectations on mailer classes with assertions about ActionMailer::Base.deliveries. This improves test isolation for parallel execution. Changes: - 'creates an invitation for each student' now verifies emails sent via delivery count - 'creates an invitation for each coach' now verifies emails sent via delivery count - Tests verify recipient email addresses match expected members - Removes expect(mailer).to receive(:invite_student/coach) mocks
…ertions Replaces mock expectations with ActionMailer::Base.deliveries count assertions for #send_waiting_list_emails tests. Changes: - 'emails coaches when there are free coach spots' - verifies email delivery - 'does not email coaches when no coach spots' - verifies no emails sent - 'emails students when there are free student spots' - verifies email delivery - 'does not email students when no student spots' - verifies no emails sent Removes expect(WorkshopInvitationMailer).to receive(:notify_waiting_list) mocks
Replaces mock expectations with ActionMailer::Base.deliveries count assertions for reminder email tests. Changes: - 'emails all attending members' (monthly) - uses _without_delay method - 'emails all attending members' (workshop) - verifies email delivery count - 'emails waiting list' - uses _without_delay method Notes: - Monthly and waiting list tests remain marked :wip as they were before - These methods are async and require _without_delay for sync testing Removes expect(Mailer).to receive(:attendance_reminder) mocks
Replaces mock expectations with ActionMailer::Base.deliveries count assertions for #send_meeting_emails tests. Changes: - 'emails all invitees that are not banned' - verifies email count excludes banned - 'emails valid invitees only once' - verifies email count excludes already invited Uses _without_delay method for synchronous testing. Removes expect(MeetingInvitationMailer).to receive(:invite) mocks
Replaces mock expectations with ActionMailer::Base.deliveries count assertions for async behavior tests. Changes: - 'sends invitation emails' (async context) - verifies email delivery - 'sends attendance reminder emails' (async context) - verifies email delivery Uses _without_delay methods for synchronous testing. Removes remaining expect(WorkshopInvitationMailer).to receive mocks
…ertions Simplifies after create hook tests by removing mock-based tests and replacing with direct email delivery verification. Changes: - Consolidates two mock-based tests into one email delivery test - Verifies email is sent and has expected subject Removes allow().to receive() and have_received() mocks
…ssertions Replaces spy-based mock test with email delivery verification. Changes: - 'sends an eligibility check email' - verifies email delivery count and recipient Removes allow().to receive() and have_received() mocks
…sertions Replaces spy-based mock test with email delivery verification. Changes: - 'sends an attendance warning email' - finds email by recipient and subject - Uses flexible matching to account for other emails in deliveries Removes allow().to receive() and have_received() mocks
… assertions Replaces expect_any_instance_of mocks with ActionMailer::Base.deliveries verification for all welcome email scenarios. Changes: - First-time coach subscription - verifies email sent and contains 'coach' - First-time student subscription - verifies email sent and contains 'student' - Second subscription tests - clears deliveries and verifies no new emails - Unsubscribe/resubscribe tests - verifies no duplicate emails sent Adds before hook to clear deliveries for consistent test isolation. Removes all expect_any_instance_of(MemberMailer) mocks which were problematic for parallel test execution.
…rtions Replaces expect_any_instance_of mocks with direct mail content verification for welcome email tests. Changes: - 'sends the coach welcome email to coaches' - verifies email body contains coach text - 'sends the student welcome email to students' - verifies email body contains student text - 'sends a ban email to a member' - verifies recipient and email body Removes all expect_any_instance_of(MemberMailer) mocks which were problematic for parallel test execution.
Collaborator
Author
|
@olleolleolle is this the most Rails idiomatic way of testing email sending? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replaces mock-based mailer tests with assertions on ActionMailer::Base.deliveries. This improves test isolation and eliminates flaky tests in parallel test execution.
Problem
The existing tests used
expect(Mailer).to receive(:method)andexpect_any_instance_of(Mailer)which:Solution
Replace mock expectations with assertions on actual email delivery:
expect { action }.to change { ActionMailer::Base.deliveries.count }expect(email.to).to include(member.email)expect(email.body.encoded).to match(/pattern/)Files Changed
spec/support/shared_examples/behaves_like_sending_workshop_emails.rbspec/models/invitation_manager_spec.rbspec/models/feedback_request_spec.rbspec/models/eligibility_inquiry_spec.rbspec/models/attendance_warning_spec.rbspec/features/subscribing_to_emails_spec.rbspec/mailers/member_mailer_spec.rbTest Results
All 59 affected tests pass:
Benefits