Skip to content

Commit efdc71d

Browse files
committed
refactor(tests): convert subscription email feature tests to delivery 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.
1 parent 8737dbd commit efdc71d

1 file changed

Lines changed: 28 additions & 14 deletions

File tree

spec/features/subscribing_to_emails_spec.rb

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,62 +24,76 @@
2424
end
2525

2626
context 'a member receives a welcome email' do
27+
before do
28+
ActionMailer::Base.deliveries.clear
29+
end
30+
2731
scenario 'Subscribing to a coach mailing list for the first time sends a coach email to the user' do
2832
coach_group = Fabricate(:coaches)
29-
expect_any_instance_of(MemberMailer).to receive(:welcome_coach)
30-
expect_any_instance_of(MemberMailer).not_to receive(:welcome_students)
3133

3234
visit subscriptions_path
3335
click_on "#{coach_group.chapter.name}-coaches"
36+
37+
welcome_emails = ActionMailer::Base.deliveries.select { |e| e.to.include?(member.email) }
38+
expect(welcome_emails.count).to eq(1)
39+
expect(welcome_emails.first.body.encoded).to include('coach')
3440
end
3541

3642
scenario 'Subscribing to a student mailing list for the first time sends a student email to the user' do
37-
expect_any_instance_of(MemberMailer).to receive(:welcome_student)
38-
expect_any_instance_of(MemberMailer).not_to receive(:welcome_coach)
39-
4043
visit subscriptions_path
4144
click_on "#{group.chapter.name}-students"
45+
46+
welcome_emails = ActionMailer::Base.deliveries.select { |e| e.to.include?(member.email) }
47+
expect(welcome_emails.count).to eq(1)
48+
expect(welcome_emails.first.body.encoded).to include('student')
4249
end
4350

4451
scenario "Subscribing to a second coach mailing list doesn't send another mail" do
4552
coach_groups = Fabricate.times(2, :coaches)
46-
expect_any_instance_of(MemberMailer).to receive(:welcome_coach).once
47-
expect_any_instance_of(MemberMailer).not_to receive(:welcome_students)
4853

4954
visit subscriptions_path
5055
click_on "#{coach_groups[0].chapter.name}-coaches"
56+
ActionMailer::Base.deliveries.clear
5157
click_on "#{coach_groups[1].chapter.name}-coaches"
58+
59+
welcome_emails = ActionMailer::Base.deliveries.select { |e| e.to.include?(member.email) }
60+
expect(welcome_emails.count).to eq(0)
5261
end
5362

5463
scenario "Subscribing to a second student mailing list doesn't send another mail" do
55-
expect_any_instance_of(MemberMailer).to receive(:welcome_student).once
56-
expect_any_instance_of(MemberMailer).not_to receive(:welcome_coach)
5764
extra_student_group = Fabricate(:students)
5865

5966
visit subscriptions_path
6067
click_on "#{group.chapter.name}-students"
68+
ActionMailer::Base.deliveries.clear
6169
click_on "#{extra_student_group.chapter.name}-students"
70+
71+
welcome_emails = ActionMailer::Base.deliveries.select { |e| e.to.include?(member.email) }
72+
expect(welcome_emails.count).to eq(0)
6273
end
6374

6475
scenario "Unsubscribing and re-subscribing doesn't send a second mail to a coach" do
6576
coach_group = Fabricate(:coaches)
66-
expect_any_instance_of(MemberMailer).to receive(:welcome_coach).once
67-
expect_any_instance_of(MemberMailer).not_to receive(:welcome_students)
6877

6978
visit subscriptions_path
7079
click_on "#{coach_group.chapter.name}-coaches"
80+
ActionMailer::Base.deliveries.clear
7181
click_on "#{coach_group.chapter.name}-coaches"
7282
click_on "#{coach_group.chapter.name}-coaches"
83+
84+
welcome_emails = ActionMailer::Base.deliveries.select { |e| e.to.include?(member.email) }
85+
expect(welcome_emails.count).to eq(0)
7386
end
7487

7588
scenario "Unsubscribing and re-subscribing doesn't send a second mail to a student" do
76-
expect_any_instance_of(MemberMailer).to receive(:welcome_student).once
77-
expect_any_instance_of(MemberMailer).not_to receive(:welcome_coach)
78-
7989
visit subscriptions_path
8090
click_on "#{group.chapter.name}-students"
91+
ActionMailer::Base.deliveries.clear
8192
click_on "#{group.chapter.name}-students"
8293
click_on "#{group.chapter.name}-students"
94+
95+
welcome_emails = ActionMailer::Base.deliveries.select { |e| e.to.include?(member.email) }
96+
expect(welcome_emails.count).to eq(0)
8397
end
8498
end
8599
end

0 commit comments

Comments
 (0)