Skip to content

Commit e389d1e

Browse files
author
Joel Lubrano
committed
Support mailer methods with default/optional arguments; Support matching email methods that accept parameters when no specific arguments are expected.
1 parent 547e4af commit e389d1e

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

lib/rspec/rails/matchers/have_enqueued_mail.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ module Matchers
99
#
1010
# @see RSpec::Rails::Matchers#have_enqueued_mail
1111
class HaveEnqueuedMail < RSpec::Matchers::BuiltIn::BaseMatcher
12+
include RSpec::Mocks::ExampleMethods
13+
1214
def initialize(mailer_class, method_name)
1315
@mailer_class = mailer_class
1416
@method_name = method_name
@@ -71,7 +73,21 @@ def expected_count_message
7173
end
7274

7375
def mailer_args
74-
[@mailer_class.name, @method_name.to_s, 'deliver_now'] + @args
76+
base_args = [@mailer_class.name, @method_name.to_s, 'deliver_now']
77+
78+
if @args.any?
79+
base_args + @args
80+
else
81+
mailer_method_arity = @mailer_class.instance_method(@method_name).arity
82+
83+
number_of_args = if mailer_method_arity.negative?
84+
(mailer_method_arity + 1).abs
85+
else
86+
mailer_method_arity
87+
end
88+
89+
base_args + Array.new(number_of_args) { anything }
90+
end
7591
end
7692

7793
def check_active_job_adapter

spec/rspec/rails/matchers/have_enqueued_mail_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
class TestMailer < ActionMailer::Base
99
def test_email; end
1010
def email_with_args(arg1, arg2); end
11+
def email_with_optional_args(required_arg, optional_arg = nil); end
1112
end
1213
end
1314

@@ -110,6 +111,20 @@ def email_with_args(arg1, arg2); end
110111
}.to have_enqueued_mail(TestMailer, :email_with_args)
111112
end
112113

114+
it "passes for mailer methods with default arguments" do
115+
expect {
116+
TestMailer.email_with_optional_args('required').deliver_later
117+
}.to have_enqueued_mail(TestMailer, :email_with_optional_args)
118+
119+
expect {
120+
TestMailer.email_with_optional_args('required').deliver_later
121+
}.to have_enqueued_mail(TestMailer, :email_with_optional_args).with('required')
122+
123+
expect {
124+
TestMailer.email_with_optional_args('required', 'optional').deliver_later
125+
}.to have_enqueued_mail(TestMailer, :email_with_optional_args).with('required', 'optional')
126+
end
127+
113128
it "passes with provided argument matchers" do
114129
expect {
115130
TestMailer.email_with_args(1, 2).deliver_later

0 commit comments

Comments
 (0)