Skip to content

Commit fd9d4d5

Browse files
author
Joel Lubrano
committed
Add tests for have_enqueued_mail matcher at_least and at_most modifiers
1 parent 19883db commit fd9d4d5

File tree

2 files changed

+58
-13
lines changed

2 files changed

+58
-13
lines changed

lib/rspec/rails/matchers/have_enqueued_mail.rb

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,32 @@ def with(*args)
5050
end
5151
end
5252

53+
def times
54+
self
55+
end
56+
5357
def failure_message
54-
base_message.tap do |msg|
55-
msg << " #{expected_count_message}"
56-
msg << " with #{@args}" if @args.any?
57-
msg << " but enqueued #{@job_matcher.matching_jobs.size}"
58+
"expected to enqueue #{base_message}".tap do |msg|
5859
msg << "\n#{unmatching_mail_jobs_message}" if unmatching_mail_jobs.any?
5960
end
6061
end
6162

63+
def failure_message_when_negated
64+
"expected not to enqueue #{base_message}"
65+
end
66+
6267
def supports_block_expectations?
6368
true
6469
end
6570

6671
private
6772

6873
def base_message
69-
"expected to enqueue #{@mailer_class.name}.#{@method_name}"
74+
"#{@mailer_class.name}.#{@method_name}".tap do |msg|
75+
msg << " #{expected_count_message}"
76+
msg << " with #{@args}" if @args.any?
77+
msg << ", but enqueued #{@job_matcher.matching_jobs.size}"
78+
end
7079
end
7180

7281
def expected_count_message

spec/rspec/rails/matchers/have_enqueued_mail_spec.rb

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,28 @@ def email_with_optional_args(required_arg, optional_arg = nil); end
7070
}.to raise_error(/expected to enqueue TestMailer.test_email exactly 1 time/)
7171
end
7272

73+
it "matches based on mailer class and method name" do
74+
expect {
75+
TestMailer.test_email.deliver_later
76+
TestMailer.email_with_args(1, 2).deliver_later
77+
}.to have_enqueued_mail(TestMailer, :test_email).once
78+
end
79+
80+
it "passes with multiple emails" do
81+
expect {
82+
TestMailer.test_email.deliver_later
83+
TestMailer.email_with_args(1, 2).deliver_later
84+
}.to have_enqueued_mail(TestMailer, :test_email).and have_enqueued_mail(TestMailer, :email_with_args)
85+
end
86+
87+
it 'fails when negated and mail is enqueued' do
88+
expect {
89+
expect {
90+
TestMailer.test_email.deliver_later
91+
}.not_to have_enqueued_mail(TestMailer, :test_email)
92+
}.to raise_error(/expected not to enqueue TestMailer.test_email exactly 1 time, but enqueued 1/)
93+
end
94+
7395
it "passes with :once count" do
7496
expect {
7597
TestMailer.test_email.deliver_later
@@ -91,18 +113,32 @@ def email_with_optional_args(required_arg, optional_arg = nil); end
91113
}.to have_enqueued_mail(TestMailer, :test_email).thrice
92114
end
93115

94-
it "matches based on mailer class and method name" do
116+
it "passes with at_least when enqueued emails are over the limit" do
95117
expect {
96118
TestMailer.test_email.deliver_later
97-
TestMailer.email_with_args(1, 2).deliver_later
98-
}.to have_enqueued_mail(TestMailer, :test_email).once
119+
TestMailer.test_email.deliver_later
120+
}.to have_enqueued_mail(TestMailer, :test_email).at_least(:once)
99121
end
100122

101-
it "passes with multiple emails" do
123+
it "passes with at_most when enqueued emails are under the limit" do
102124
expect {
103125
TestMailer.test_email.deliver_later
104-
TestMailer.email_with_args(1, 2).deliver_later
105-
}.to have_enqueued_mail(TestMailer, :test_email).and have_enqueued_mail(TestMailer, :email_with_args)
126+
}.to have_enqueued_mail(TestMailer, :test_email).at_most(:twice)
127+
end
128+
129+
it "generates a failure message with at least hint" do
130+
expect {
131+
expect { }.to have_enqueued_mail(TestMailer, :test_email).at_least(:once)
132+
}.to raise_error(/expected to enqueue TestMailer.test_email at least 1 time, but enqueued 0/)
133+
end
134+
135+
it "generates a failure message with at most hint" do
136+
expect {
137+
expect {
138+
TestMailer.test_email.deliver_later
139+
TestMailer.test_email.deliver_later
140+
}.to have_enqueued_mail(TestMailer, :test_email).at_most(:once)
141+
}.to raise_error(/expected to enqueue TestMailer.test_email at most 1 time, but enqueued 2/)
106142
end
107143

108144
it "passes for mailer methods that accept arguments when the provided argument matcher is not used" do
@@ -144,11 +180,11 @@ def email_with_optional_args(required_arg, optional_arg = nil); end
144180
it "generates a failure message with arguments" do
145181
expect {
146182
expect { }.to have_enqueued_email(TestMailer, :email_with_args).with(1, 2)
147-
}.to raise_error(/expected to enqueue TestMailer.email_with_args exactly 1 time with \[1, 2\] but enqueued 0/)
183+
}.to raise_error(/expected to enqueue TestMailer.email_with_args exactly 1 time with \[1, 2\], but enqueued 0/)
148184
end
149185

150186
it "generates a failure message with unmatching enqueued mail jobs" do
151-
message = "expected to enqueue TestMailer.email_with_args exactly 1 time with [1, 2] but enqueued 0" + \
187+
message = "expected to enqueue TestMailer.email_with_args exactly 1 time with [1, 2], but enqueued 0" + \
152188
"\nQueued deliveries:" + \
153189
"\n TestMailer.test_email" + \
154190
"\n TestMailer.email_with_args with [3, 4]"

0 commit comments

Comments
 (0)