Skip to content

Commit b593ede

Browse files
author
Joel Lubrano
committed
Support with blocks
1 parent e0b14e7 commit b593ede

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

lib/rspec/rails/matchers/have_enqueued_mail.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ def description
2424
"enqueues #{@mailer_class.name}.#{@method_name}"
2525
end
2626

27-
def with(*args)
27+
def with(*args, &block)
2828
@mail_args = args
29-
super(*mailer_args)
29+
30+
super(*mailer_args) do |*job_args|
31+
block.call(*(job_args - base_mailer_args)) if block.present?
32+
end
3033
end
3134

3235
def matches?(block)
@@ -62,10 +65,8 @@ def expected_count_message
6265
end
6366

6467
def mailer_args
65-
base_args = [@mailer_class.name, @method_name.to_s, 'deliver_now']
66-
6768
if @mail_args.any?
68-
base_args + @mail_args
69+
base_mailer_args + @mail_args
6970
else
7071
mailer_method_arity = @mailer_class.instance_method(@method_name).arity
7172

@@ -75,10 +76,14 @@ def mailer_args
7576
mailer_method_arity
7677
end
7778

78-
base_args + Array.new(number_of_args) { anything }
79+
base_mailer_args + Array.new(number_of_args) { anything }
7980
end
8081
end
8182

83+
def base_mailer_args
84+
[@mailer_class.name, @method_name.to_s, 'deliver_now']
85+
end
86+
8287
def check_active_job_adapter
8388
return if ::ActiveJob::QueueAdapters::TestAdapter === ::ActiveJob::Base.queue_adapter
8489
raise StandardError, "To use HaveEnqueuedMail matcher set `ActiveJob::Base.queue_adapter = :test`"

spec/rspec/rails/matchers/have_enqueued_mail_spec.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,5 +242,40 @@ def email_with_optional_args(required_arg, optional_arg = nil); end
242242

243243
ActiveJob::Base.queue_adapter = queue_adapter
244244
end
245+
246+
it "fails with with block with incorrect data" do
247+
expect {
248+
expect {
249+
TestMailer.email_with_args('asdf', 'zxcv').deliver_later
250+
}.to have_enqueued_mail(TestMailer, :email_with_args).with { |first_arg, second_arg|
251+
expect(first_arg).to eq("zxcv")
252+
}
253+
}.to raise_error { |e|
254+
expect(e.message).to match(/expected: "zxcv"/)
255+
expect(e.message).to match(/got: "asdf"/)
256+
}
257+
end
258+
259+
it "passes multiple arguments to with block" do
260+
expect {
261+
TestMailer.email_with_args('asdf', 'zxcv').deliver_later
262+
}.to have_enqueued_mail(TestMailer, :email_with_args).with { |first_arg, second_arg|
263+
expect(first_arg).to eq("asdf")
264+
expect(second_arg).to eq("zxcv")
265+
}
266+
end
267+
268+
it "only calls with block if other conditions are met" do
269+
noon = Date.tomorrow.noon
270+
midnight = Date.tomorrow.midnight
271+
272+
expect {
273+
TestMailer.email_with_args('high', 'noon').deliver_later(wait_until: noon)
274+
TestMailer.email_with_args('midnight', 'rider').deliver_later(wait_until: midnight)
275+
}.to have_enqueued_mail(TestMailer, :email_with_args).at(noon).with { |first_arg, second_arg|
276+
expect(first_arg).to eq('high')
277+
expect(second_arg).to eq('noon')
278+
}
279+
end
245280
end
246281
end

0 commit comments

Comments
 (0)