Skip to content
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

Log active_job potential matches when asserting #36920

Merged

Conversation

@garethson
Copy link
Contributor

commented Aug 12, 2019

Adds logging of potential matches when calling assert_enqueued_with and assert_performed_with to provide more information on test failures.

Summary

When testing ActiveJob, it's quite helpful to view the jobs that were performed (or enqueued) when these assertions fail.

Before:

# Running:

...........F

Failure:
PerformedJobsTest#test_assert_performed_with_selective_args [/Users/garethduplooy/src/github.com/rails/activejob/test/cases/test_helper_test.rb:1761]:
No performed job found with {:job=>MultipleKwargsJob, :args=>{:argument2=>{:b=>3, :a=>4}, :argument1=>1}}

After:

# Running:

...........F

Failure:
PerformedJobsTest#test_assert_performed_with_selective_args [/Users/garethduplooy/src/github.com/rails/activejob/test/cases/test_helper_test.rb:1761]:
No performed job found with {:job=>MultipleKwargsJob, :args=>{:argument2=>{:b=>3, :a=>4}, :argument1=>1}}
Potential matches: {:job=>MultipleKwargsJob, :args=>[{:argument2=>{:b=>2, :a=>1}, :argument1=>1}], :queue=>"default"}

The added context makes it much easier to determine the source of failure (wrong job, jobs, wrong params, wrong queue, etc).

cc @gmcgibbon @rafaelfranca

@rails-bot rails-bot bot added the activejob label Aug 12, 2019

@@ -405,7 +407,9 @@ def assert_enqueued_with(job: nil, args: nil, at: nil, queue: nil)
end
end

assert matching_job, "No enqueued job found with #{expected}"
message = "No enqueued job found with #{expected}"
message += "\nPotential matches: #{potential_matches.join("\n")}" if potential_matches.present?

This comment has been minimized.

Copy link
@gmcgibbon

gmcgibbon Aug 12, 2019

Member

I think << is cheaper right? I would also prefer if potential_matches.any? here.

This comment has been minimized.

Copy link
@itsWill

itsWill Aug 12, 2019

Contributor

note since the # frozen_string_literal: true comment is enabled, << will fail, in which case you can do:

message = +"No enqueued job found with #{expected}"

And then << should work correctly.

@gmcgibbon

This comment has been minimized.

Copy link
Member

commented Aug 12, 2019

I think I would also prefer a line break in the output:

# Running:

...........F

Failure:
PerformedJobsTest#test_assert_performed_with_selective_args [/Users/garethduplooy/src/github.com/rails/activejob/test/cases/test_helper_test.rb:1761]:
No performed job found with {:job=>MultipleKwargsJob, :args=>{:argument2=>{:b=>3, :a=>4}, :argument1=>1}}

Potential matches: {:job=>MultipleKwargsJob, :args=>[{:argument2=>{:b=>2, :a=>1}, :argument1=>1}], :queue=>"default"}
Log active_job potential matches when asserting
Adds logging of potential matches when calling `assert_enqueued_with` and `assert_performed_with` to provide more information on test failures.

@garethson garethson force-pushed the garethson:log-active-job-potential-matches branch from 0757a83 to 060a1fb Aug 12, 2019

@rafaelfranca rafaelfranca merged commit ac9416f into rails:master Aug 12, 2019

2 checks passed

buildkite/rails Build #63019 passed (9 minutes, 33 seconds)
Details
codeclimate All good!
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.