@@ -9,65 +9,30 @@ module Matchers
9
9
# rubocop: disable Style/ClassLength
10
10
# @private
11
11
# @see RSpec::Rails::Matchers#have_enqueued_mail
12
- class HaveEnqueuedMail < RSpec :: Matchers :: BuiltIn :: BaseMatcher
12
+ class HaveEnqueuedMail < ActiveJob :: HaveEnqueuedJob
13
13
include RSpec ::Mocks ::ExampleMethods
14
14
15
15
def initialize ( mailer_class , method_name )
16
+ super ( mailer_job )
16
17
@mailer_class = mailer_class
17
18
@method_name = method_name
18
- @args = [ ]
19
- @at = nil
20
- @queue = nil
21
- @job_matcher = ActiveJob ::HaveEnqueuedJob . new ( ActionMailer ::DeliveryJob )
22
- set_expected_count ( :exactly , 1 )
19
+ @mail_args = [ ]
20
+ @args = mailer_args
23
21
end
24
22
25
23
def description
26
24
"enqueues #{ @mailer_class . name } .#{ @method_name } "
27
25
end
28
26
29
- def matches? ( block )
30
- raise ArgumentError , 'have_enqueued_mail and enqueue_mail only work with block arguments' unless block . respond_to? ( :call )
31
- check_active_job_adapter
32
-
33
- @job_matcher . with ( *mailer_args )
34
- @job_matcher . matches? ( block )
35
- end
36
-
37
27
def with ( *args )
38
- @args = args
39
- self
40
- end
41
-
42
- def at ( send_time )
43
- @at = send_time
44
- @job_matcher . at ( send_time )
45
- self
46
- end
47
-
48
- def on_queue ( queue )
49
- @queue = queue
50
- @job_matcher . on_queue ( queue )
51
- self
52
- end
53
-
54
- [ :exactly , :at_least , :at_most ] . each do |method |
55
- define_method ( method ) do |count |
56
- @job_matcher . public_send ( method , count )
57
- set_expected_count ( method , count )
58
- self
59
- end
28
+ @mail_args = args
29
+ super ( *mailer_args )
60
30
end
61
31
62
- [ :once , :twice , :thrice ] . each do |method |
63
- define_method ( method ) do
64
- @job_matcher . public_send ( method )
65
- exactly ( method )
66
- end
67
- end
68
-
69
- def times
70
- self
32
+ def matches? ( block )
33
+ raise ArgumentError , 'have_enqueued_mail and enqueue_mail only work with block arguments' unless block . respond_to? ( :call )
34
+ check_active_job_adapter
35
+ super
71
36
end
72
37
73
38
def failure_message
@@ -80,31 +45,27 @@ def failure_message_when_negated
80
45
"expected not to enqueue #{ base_message } "
81
46
end
82
47
83
- def supports_block_expectations?
84
- true
85
- end
86
-
87
48
private
88
49
89
50
def base_message
90
51
"#{ @mailer_class . name } .#{ @method_name } " . tap do |msg |
91
52
msg << " #{ expected_count_message } "
92
- msg << " with #{ @args } ," if @args . any?
53
+ msg << " with #{ @mail_args } ," if @mail_args . any?
93
54
msg << " on queue #{ @queue } ," if @queue
94
55
msg << " at #{ @at . inspect } ," if @at
95
- msg << " but enqueued #{ @job_matcher . matching_jobs . size } "
56
+ msg << " but enqueued #{ @matching_jobs . size } "
96
57
end
97
58
end
98
59
99
60
def expected_count_message
100
- "#{ @expected_count_type . to_s . tr ( '_' , ' ' ) } #{ @expected_count } #{ @expected_count == 1 ? 'time' : 'times' } "
61
+ "#{ message_expectation_modifier } #{ @expected_number } #{ @expected_number == 1 ? 'time' : 'times' } "
101
62
end
102
63
103
64
def mailer_args
104
65
base_args = [ @mailer_class . name , @method_name . to_s , 'deliver_now' ]
105
66
106
- if @args . any?
107
- base_args + @args
67
+ if @mail_args . any?
68
+ base_args + @mail_args
108
69
else
109
70
mailer_method_arity = @mailer_class . instance_method ( @method_name ) . arity
110
71
@@ -123,19 +84,9 @@ def check_active_job_adapter
123
84
raise StandardError , "To use HaveEnqueuedMail matcher set `ActiveJob::Base.queue_adapter = :test`"
124
85
end
125
86
126
- def set_expected_count ( relativity , count )
127
- @expected_count_type = relativity
128
- @expected_count = case count
129
- when :once then 1
130
- when :twice then 2
131
- when :thrice then 3
132
- else Integer ( count )
133
- end
134
- end
135
-
136
87
def unmatching_mail_jobs
137
- @job_matcher . unmatching_jobs . select do |job |
138
- job [ :job ] == ActionMailer :: DeliveryJob
88
+ @unmatching_jobs . select do |job |
89
+ job [ :job ] == mailer_job
139
90
end
140
91
end
141
92
@@ -160,6 +111,10 @@ def mail_job_message(job)
160
111
161
112
"#{ mailer_method } #{ msg_parts . join ( ', ' ) } " . strip
162
113
end
114
+
115
+ def mailer_job
116
+ ActionMailer ::DeliveryJob
117
+ end
163
118
end
164
119
# rubocop: enable Style/ClassLength
165
120
0 commit comments