Skip to content

Add support for ActiveJob argument matchers #1730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions lib/rspec/rails/matchers/active_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def supports_block_expectations?

def check(jobs)
@matching_jobs, @unmatching_jobs = jobs.partition do |job|
if serialized_attributes.all? { |key, value| value == job[key] }
if arguments_match?(job) && other_attributes_match?(job)
args = ::ActiveJob::Arguments.deserialize(job[:args])
@block.call(*args)
true
Expand Down Expand Up @@ -134,9 +134,21 @@ def base_job_message(job)
end
end

def arguments_match?(job)
if @args.any?
deserialized_args = ::ActiveJob::Arguments.deserialize(job[:args])
RSpec::Mocks::ArgumentListMatcher.new(*@args).args_match?(*deserialized_args)
else
true
end
end

def other_attributes_match?(job)
serialized_attributes.all? { |key, value| value == job[key] }
end

def serialized_attributes
{}.tap do |attributes|
attributes[:args] = ::ActiveJob::Arguments.serialize(@args) if @args.any?
attributes[:at] = @at.to_f if @at
attributes[:queue] = @queue if @queue
attributes[:job] = @job if @job
Expand Down
6 changes: 6 additions & 0 deletions spec/rspec/rails/matchers/active_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@ def perform; end
}.to have_enqueued_job.with(global_id_object)
end

it "passes with provided argument matchers" do
expect {
hello_job.perform_later(42, "David")
}.to have_enqueued_job.with(instance_of(Fixnum), instance_of(String))
end

it "generates failure message with all provided options" do
date = Date.tomorrow.noon
message = "expected to enqueue exactly 2 jobs, with [42], on queue low, at #{date}, but enqueued 0" + \
Expand Down