Skip to content

Commit 274299a

Browse files
committed
add support for parameterized mailer when RAILS_VERSION >= 5.1
1 parent cc2d4ab commit 274299a

File tree

4 files changed

+54
-7
lines changed

4 files changed

+54
-7
lines changed

lib/rspec/rails/feature_check.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ def has_action_mailer_show_preview?
3838
::ActionMailer::Base.respond_to?(:show_previews=)
3939
end
4040

41+
def has_action_mailer_parameterized?
42+
has_action_mailer? && defined?(::ActionMailer::Parameterized)
43+
end
44+
4145
def has_action_mailbox?
4246
defined?(::ActionMailbox)
4347
end

lib/rspec/rails/matchers/active_job.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def supports_block_expectations?
9797

9898
def check(jobs)
9999
@matching_jobs, @unmatching_jobs = jobs.partition do |job|
100-
if arguments_match?(job) && other_attributes_match?(job)
100+
if job_match?(job) && arguments_match?(job) && other_attributes_match?(job)
101101
args = deserialize_arguments(job)
102102
@block.call(*args)
103103
true
@@ -134,6 +134,10 @@ def base_job_message(job)
134134
end
135135
end
136136

137+
def job_match?(job)
138+
@job ? @job == job[:job] : true
139+
end
140+
137141
def arguments_match?(job)
138142
if @args.any?
139143
deserialized_args = deserialize_arguments(job)
@@ -151,7 +155,6 @@ def serialized_attributes
151155
{}.tap do |attributes|
152156
attributes[:at] = serialized_at if @at
153157
attributes[:queue] = @queue if @queue
154-
attributes[:job] = @job if @job
155158
end
156159
end
157160

lib/rspec/rails/matchers/have_enqueued_mail.rb

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def initialize(mailer_class, method_name)
1818
@mailer_class = mailer_class
1919
@method_name = method_name
2020
@mail_args = []
21-
@args = mailer_args
21+
@args = []
2222
end
2323

2424
def description
@@ -27,7 +27,7 @@ def description
2727

2828
def with(*args, &block)
2929
@mail_args = args
30-
block.nil? ? super(*mailer_args) : super(*mailer_args, &yield_mail_args(block))
30+
block.nil? ? super : super(&yield_mail_args(block))
3131
end
3232

3333
def matches?(block)
@@ -63,9 +63,17 @@ def expected_count_message
6363
"#{message_expectation_modifier} #{@expected_number} #{@expected_number == 1 ? 'time' : 'times'}"
6464
end
6565

66-
def mailer_args
66+
def job_match?(job)
67+
super(job) || parameterized_mail?(job)
68+
end
69+
70+
def parameterized_mail?(job)
71+
RSpec::Rails::FeatureCheck.has_action_mailer_parameterized? && job[:job] == parameterized_mailer_job
72+
end
73+
74+
def arguments_match?(job)
6775
if @mail_args.any?
68-
base_mailer_args + @mail_args
76+
@args = base_mailer_args + @mail_args
6977
else
7078
mailer_method_arity = @mailer_class.instance_method(@method_name).arity
7179

@@ -74,9 +82,12 @@ def mailer_args
7482
else
7583
mailer_method_arity
7684
end
85+
number_of_args += 1 if parameterized_mail?(job)
7786

78-
base_mailer_args + Array.new(number_of_args) { anything }
87+
@args = base_mailer_args + Array.new(number_of_args) { anything }
7988
end
89+
90+
super(job)
8091
end
8192

8293
def base_mailer_args
@@ -124,6 +135,10 @@ def mail_job_message(job)
124135
def mailer_job
125136
ActionMailer::DeliveryJob
126137
end
138+
139+
def parameterized_mailer_job
140+
ActionMailer::Parameterized::DeliveryJob
141+
end
127142
end
128143
# @api public
129144
# Passes if an email has been enqueued inside block.

spec/rspec/rails/matchers/have_enqueued_mail_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,5 +283,30 @@ def self.name; "NonMailerJob"; end
283283
expect(second_arg).to eq('noon')
284284
}
285285
end
286+
287+
context 'when parameterized', :skip => !RSpec::Rails::FeatureCheck.has_action_mailer_parameterized? do
288+
it "passes when mailer is parameterized" do
289+
expect {
290+
TestMailer.with('foo' => 'bar').test_email.deliver_later
291+
}.to have_enqueued_mail(TestMailer, :test_email)
292+
end
293+
294+
it "passes when mixing parameterized and non-parameterized emails" do
295+
expect {
296+
TestMailer.with('foo' => 'bar').test_email.deliver_later
297+
TestMailer.email_with_args(1, 2).deliver_later
298+
}.to have_enqueued_mail(TestMailer, :test_email).and have_enqueued_mail(TestMailer, :email_with_args)
299+
end
300+
301+
it "passes with provided argument matchers" do
302+
expect {
303+
TestMailer.with('foo' => 'bar').test_email.deliver_later
304+
}.to have_enqueued_mail(TestMailer, :test_email).with('foo' => 'bar')
305+
306+
expect {
307+
TestMailer.with('foo' => 'bar').email_with_args(1, 2).deliver_later
308+
}.not_to have_enqueued_mail(TestMailer, :email_with_args).with({ 'foo' => 'bar' }, 3, 4)
309+
end
310+
end
286311
end
287312
end

0 commit comments

Comments
 (0)