Skip to content

Commit 39d0f9e

Browse files
author
Sam Phippen
authored
Merge pull request #1730 from antstorm/support-for-argument-matchers
Add support for ActiveJob argument matchers
2 parents 063f035 + bc947b2 commit 39d0f9e

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

lib/rspec/rails/matchers/active_job.rb

Lines changed: 14 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 serialized_attributes.all? { |key, value| value == job[key] }
100+
if arguments_match?(job) && other_attributes_match?(job)
101101
args = ::ActiveJob::Arguments.deserialize(job[:args])
102102
@block.call(*args)
103103
true
@@ -134,9 +134,21 @@ def base_job_message(job)
134134
end
135135
end
136136

137+
def arguments_match?(job)
138+
if @args.any?
139+
deserialized_args = ::ActiveJob::Arguments.deserialize(job[:args])
140+
RSpec::Mocks::ArgumentListMatcher.new(*@args).args_match?(*deserialized_args)
141+
else
142+
true
143+
end
144+
end
145+
146+
def other_attributes_match?(job)
147+
serialized_attributes.all? { |key, value| value == job[key] }
148+
end
149+
137150
def serialized_attributes
138151
{}.tap do |attributes|
139-
attributes[:args] = ::ActiveJob::Arguments.serialize(@args) if @args.any?
140152
attributes[:at] = @at.to_f if @at
141153
attributes[:queue] = @queue if @queue
142154
attributes[:job] = @job if @job

spec/rspec/rails/matchers/active_job_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,12 @@ def perform; end
209209
}.to have_enqueued_job.with(global_id_object)
210210
end
211211

212+
it "passes with provided argument matchers" do
213+
expect {
214+
hello_job.perform_later(42, "David")
215+
}.to have_enqueued_job.with(instance_of(Fixnum), instance_of(String))
216+
end
217+
212218
it "generates failure message with all provided options" do
213219
date = Date.tomorrow.noon
214220
message = "expected to enqueue exactly 2 jobs, with [42], on queue low, at #{date}, but enqueued 0" + \

0 commit comments

Comments
 (0)