Skip to content

Commit 3e6db41

Browse files
petergoldsteinfabnpirj
committed
Fix arguments for have_enqueued_mail matcher
Add additional logic to the ActionMailer argument parsing to accomodate for differences under Ruby 3.1/Rails 6.1 Co-authored-by: Fabio Napoleoni <[email protected]> Co-authored-by: Phil Pirozhkov <[email protected]>
1 parent 37c8073 commit 3e6db41

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

.rubocop_todo.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ Layout/LineLength:
99
# Over time we'd like to get this down, but this is what we're at now.
1010
Metrics/MethodLength:
1111
Max: 43 # default: 10
12+
13+
Metrics/ClassLength:
14+
Exclude:
15+
- lib/rspec/rails/matchers/have_enqueued_mail.rb

lib/rspec/rails/feature_check.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ def has_action_mailbox?
4343
defined?(::ActionMailbox)
4444
end
4545

46+
def ruby_3_1?
47+
RUBY_VERSION >= "3.1"
48+
end
49+
4650
def type_metatag(type)
4751
"type: :#{type}"
4852
end

lib/rspec/rails/matchers/have_enqueued_mail.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
module RSpec
88
module Rails
99
module Matchers
10-
# rubocop: disable Metrics/ClassLength
1110
# Matcher class for `have_enqueued_mail`. Should not be instantiated directly.
1211
#
1312
# @private
@@ -91,7 +90,7 @@ def arguments_match?(job)
9190

9291
def process_arguments(job, given_mail_args)
9392
# Old matcher behavior working with all builtin classes but ActionMailer::MailDeliveryJob
94-
return given_mail_args unless defined?(ActionMailer::MailDeliveryJob) && job[:job] <= ActionMailer::MailDeliveryJob
93+
return given_mail_args if use_given_mail_args?(job)
9594

9695
# If matching args starts with a hash and job instance has params match with them
9796
if given_mail_args.first.is_a?(Hash) && job[:args][3]['params'].present?
@@ -101,6 +100,13 @@ def process_arguments(job, given_mail_args)
101100
end
102101
end
103102

103+
def use_given_mail_args?(job)
104+
return true if FeatureCheck.has_action_mailer_parameterized? && job[:job] <= ActionMailer::Parameterized::DeliveryJob
105+
return false if FeatureCheck.ruby_3_1?
106+
107+
!(FeatureCheck.has_action_mailer_unified_delivery? && job[:job] <= ActionMailer::MailDeliveryJob)
108+
end
109+
104110
def base_mailer_args
105111
[mailer_class_name, @method_name.to_s, MAILER_JOB_METHOD]
106112
end
@@ -143,13 +149,20 @@ def mail_job_message(job)
143149
mailer_args = deserialize_arguments(job)[3..-1]
144150
mailer_args = mailer_args.first[:args] if unified_mail?(job)
145151
msg_parts = []
146-
msg_parts << "with #{mailer_args}" if mailer_args.any?
152+
display_args = display_mailer_args(mailer_args)
153+
msg_parts << "with #{display_args}" if display_args.any?
147154
msg_parts << "on queue #{job[:queue]}" if job[:queue] && job[:queue] != 'mailers'
148155
msg_parts << "at #{Time.at(job[:at])}" if job[:at]
149156

150157
"#{mailer_method} #{msg_parts.join(', ')}".strip
151158
end
152159

160+
def display_mailer_args(mailer_args)
161+
return mailer_args unless mailer_args.first.is_a?(Hash) && mailer_args.first.key?(:args)
162+
163+
mailer_args.first[:args]
164+
end
165+
153166
def legacy_mail?(job)
154167
RSpec::Rails::FeatureCheck.has_action_mailer_legacy_delivery_job? && job[:job] <= ActionMailer::DeliveryJob
155168
end
@@ -162,7 +175,6 @@ def unified_mail?(job)
162175
RSpec::Rails::FeatureCheck.has_action_mailer_unified_delivery? && job[:job] <= ActionMailer::MailDeliveryJob
163176
end
164177
end
165-
# rubocop: enable Metrics/ClassLength
166178

167179
# @api public
168180
# Passes if an email has been enqueued inside block.

0 commit comments

Comments
 (0)