File tree Expand file tree Collapse file tree 2 files changed +32
-1
lines changed
spec/rspec/rails/matchers Expand file tree Collapse file tree 2 files changed +32
-1
lines changed Original file line number Diff line number Diff line change @@ -9,6 +9,8 @@ module Matchers
9
9
#
10
10
# @see RSpec::Rails::Matchers#have_enqueued_mail
11
11
class HaveEnqueuedMail < RSpec ::Matchers ::BuiltIn ::BaseMatcher
12
+ include RSpec ::Mocks ::ExampleMethods
13
+
12
14
def initialize ( mailer_class , method_name )
13
15
@mailer_class = mailer_class
14
16
@method_name = method_name
@@ -71,7 +73,21 @@ def expected_count_message
71
73
end
72
74
73
75
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
75
91
end
76
92
77
93
def check_active_job_adapter
Original file line number Diff line number Diff line change 8
8
class TestMailer < ActionMailer ::Base
9
9
def test_email ; end
10
10
def email_with_args ( arg1 , arg2 ) ; end
11
+ def email_with_optional_args ( required_arg , optional_arg = nil ) ; end
11
12
end
12
13
end
13
14
@@ -110,6 +111,20 @@ def email_with_args(arg1, arg2); end
110
111
} . to have_enqueued_mail ( TestMailer , :email_with_args )
111
112
end
112
113
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
+
113
128
it "passes with provided argument matchers" do
114
129
expect {
115
130
TestMailer . email_with_args ( 1 , 2 ) . deliver_later
You can’t perform that action at this time.
0 commit comments