Skip to content

Commit 3da8505

Browse files
fabnpirj
authored andcommitted
Change HaveEnqueuedEmail to work as usual in Rails 6
1 parent 48b007f commit 3da8505

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

lib/rspec/rails/matchers/have_enqueued_mail.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def job_match?(job)
7676
def arguments_match?(job)
7777
@args =
7878
if @mail_args.any?
79-
base_mailer_args + @mail_args
79+
base_mailer_args + process_arguments(job, @mail_args)
8080
elsif @mailer_class && @method_name
8181
base_mailer_args + [any_args]
8282
elsif @mailer_class
@@ -88,6 +88,18 @@ def arguments_match?(job)
8888
super(job)
8989
end
9090

91+
def process_arguments(job, given_mail_args)
92+
if job[:job] == ActionMailer::MailDeliveryJob
93+
if given_mail_args.first.is_a?(Hash) && job[:args][3]['params'].present?
94+
[hash_including(params: given_mail_args[0], args: given_mail_args.drop(1))]
95+
else
96+
[hash_including(args: given_mail_args)]
97+
end
98+
else
99+
given_mail_args
100+
end
101+
end
102+
91103
def base_mailer_args
92104
[mailer_class_name, @method_name.to_s, MAILER_JOB_METHOD]
93105
end
@@ -120,7 +132,6 @@ def unmatching_mail_jobs_message
120132

121133
def mail_job_message(job)
122134
mailer_method = job[:args][0..1].join('.')
123-
124135
mailer_args = job[:args][3..-1]
125136
msg_parts = []
126137
msg_parts << "with #{mailer_args}" if mailer_args.any?

spec/rspec/rails/matchers/have_enqueued_mail_spec.rb

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -393,18 +393,22 @@ def self.name; "NonMailerJob"; end
393393
}.to have_enqueued_mail(UnifiedMailer, :test_email).and have_enqueued_mail(UnifiedMailer, :email_with_args)
394394
end
395395

396-
it "passes with provided argument matchers" do
396+
it "matches arguments when mailer has only args" do
397+
expect {
398+
UnifiedMailer.email_with_args(1, 2).deliver_later
399+
}.to have_enqueued_mail(UnifiedMailer, :email_with_args).with(1, 2)
400+
end
401+
402+
it "matches arguments when mailer is parameterized" do
397403
expect {
398404
UnifiedMailer.with('foo' => 'bar').test_email.deliver_later
399-
}.to have_enqueued_mail(UnifiedMailer, :test_email).with(
400-
a_hash_including(params: {'foo' => 'bar'})
401-
)
405+
}.to have_enqueued_mail(UnifiedMailer, :test_email).with('foo' => 'bar')
406+
end
402407

408+
it "matches arguments when mixing parameterized and non-parameterized emails" do
403409
expect {
404410
UnifiedMailer.with('foo' => 'bar').email_with_args(1, 2).deliver_later
405-
}.to have_enqueued_mail(UnifiedMailer, :email_with_args).with(
406-
a_hash_including(params: {'foo' => 'bar'}, args: [1, 2])
407-
)
411+
}.to have_enqueued_mail(UnifiedMailer, :email_with_args).with({'foo' => 'bar'}, 1, 2)
408412
end
409413

410414
it "passes when using a mailer with `delivery_job` set to a sub class of `ActionMailer::DeliveryJob`" do

0 commit comments

Comments
 (0)