Skip to content

Improve failure message for AJ matcher #1722

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 12, 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
23 changes: 21 additions & 2 deletions lib/rspec/rails/matchers/active_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,14 @@ def thrice
end

def failure_message
"expected to enqueue #{base_message}"
"expected to enqueue #{base_message}".tap do |msg|
if @unmatching_jobs.any?
msg << "\nQueued jobs:"
@unmatching_jobs.each do |job|
msg << "\n #{base_job_message(job)}"
end
end
end
end

def failure_message_when_negated
Expand All @@ -89,7 +96,7 @@ def supports_block_expectations?
private

def check(jobs)
@matching_jobs_count = jobs.count do |job|
@matching_jobs, @unmatching_jobs = jobs.partition do |job|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this return jobs from another class?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it will return all jobs that were queued during given action.
I think most of the time number of jobs should be low, so we don't need to scope only to the same class jobs.
This way it's clear for end user what's going on, and it may happen that mistake is also in job class name (like copy pasting) ;-)

if serialized_attributes.all? { |key, value| value == job[key] }
args = ::ActiveJob::Arguments.deserialize(job[:args])
@block.call(*args)
Expand All @@ -98,6 +105,7 @@ def check(jobs)
false
end
end
@matching_jobs_count = @matching_jobs.size

case @expectation_type
when :exactly then @expected_number == @matching_jobs_count
Expand All @@ -115,6 +123,17 @@ def base_message
end
end

def base_job_message(job)
msg_parts = []
msg_parts << "with #{::ActiveJob::Arguments.deserialize(job[:args])}" if job[:args].any?
msg_parts << "on queue #{job[:queue]}" if job[:queue]
msg_parts << "at #{Time.at(job[:at])}" if job[:at]

"#{job[:job].class.name} job".tap do |msg|
msg << " #{msg_parts.join(', ')}" if msg_parts.any?
end
end

def serialized_attributes
{}.tap do |attributes|
attributes[:args] = ::ActiveJob::Arguments.serialize(@args) if @args.any?
Expand Down
6 changes: 4 additions & 2 deletions spec/rspec/rails/matchers/active_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,13 @@ def perform; 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"
message = "expected to enqueue exactly 2 jobs, with [42], on queue low, at #{date}, but enqueued 0" + \
"\nQueued jobs:" + \
"\n Class job with [1], on queue default"

expect {
expect {
hello_job.perform_later
hello_job.perform_later(1)
}.to have_enqueued_job(hello_job).with(42).on_queue("low").at(date).exactly(2).times
}.to raise_error(message)
end
Expand Down