Skip to content

Commit 19883db

Browse files
author
Joel Lubrano
committed
Provide unmatching enqueued email info in failure messages
The ActiveJob matcher provides output for each unmatching, enqueued job in its failure messages. Those unmatching jobs have certainly helped me catch typos in the past, so it makes sense to provide similar feedback in enqueued email failure messages.
1 parent 99cac0e commit 19883db

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

lib/rspec/rails/matchers/active_job.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ module ActiveJob
1111
# rubocop: disable Style/ClassLength
1212
# @private
1313
class Base < RSpec::Matchers::BuiltIn::BaseMatcher
14+
attr_reader :matching_jobs, :unmatching_jobs
15+
1416
def initialize
1517
@args = []
1618
@queue = nil

lib/rspec/rails/matchers/have_enqueued_mail.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ def failure_message
5454
base_message.tap do |msg|
5555
msg << " #{expected_count_message}"
5656
msg << " with #{@args}" if @args.any?
57+
msg << " but enqueued #{@job_matcher.matching_jobs.size}"
58+
msg << "\n#{unmatching_mail_jobs_message}" if unmatching_mail_jobs.any?
5759
end
5860
end
5961

@@ -103,6 +105,26 @@ def set_expected_count(relativity, count)
103105
else Integer(count)
104106
end
105107
end
108+
109+
def unmatching_mail_jobs
110+
@job_matcher.unmatching_jobs.select do |job|
111+
job[:job] == ActionMailer::DeliveryJob
112+
end
113+
end
114+
115+
def unmatching_mail_jobs_message
116+
msg = "Queued deliveries:"
117+
118+
unmatching_mail_jobs.each do |job|
119+
mailer_method = job[:args][0..1].join('.')
120+
mailer_args = job[:args][3..-1]
121+
122+
msg << "\n #{mailer_method}"
123+
msg << " with #{mailer_args}" if mailer_args.any?
124+
end
125+
126+
msg
127+
end
106128
end
107129

108130
# @api public

spec/rspec/rails/matchers/have_enqueued_mail_spec.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,21 @@ def email_with_optional_args(required_arg, optional_arg = nil); end
144144
it "generates a failure message with arguments" do
145145
expect {
146146
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\]/)
147+
}.to raise_error(/expected to enqueue TestMailer.email_with_args exactly 1 time with \[1, 2\] but enqueued 0/)
148+
end
149+
150+
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" + \
152+
"\nQueued deliveries:" + \
153+
"\n TestMailer.test_email" + \
154+
"\n TestMailer.email_with_args with [3, 4]"
155+
156+
expect {
157+
expect {
158+
TestMailer.test_email.deliver_later
159+
TestMailer.email_with_args(3, 4).deliver_later
160+
}.to have_enqueued_email(TestMailer, :email_with_args).with(1, 2)
161+
}.to raise_error(message)
148162
end
149163

150164
it "throws descriptive error when no test adapter set" do

0 commit comments

Comments
 (0)