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
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

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.
  • Loading branch information...
garethson committed Aug 12, 2019
commit 060a1fb338854da2b99bced7709468f5d53ad368
@@ -1,3 +1,7 @@
* Log potential matches in `assert_enqueued_with` and `assert_performed_with`

*Gareth du Plooy*

* Add `at` argument to the `perform_enqueued_jobs` test helper.

*John Crepezzi*, *Eileen Uchitelle*
@@ -382,6 +382,7 @@ def assert_no_performed_jobs(only: nil, except: nil, queue: nil, &block)
def assert_enqueued_with(job: nil, args: nil, at: nil, queue: nil)
expected = { job: job, args: args, at: at, queue: queue }.compact
expected_args = prepare_args_for_assertion(expected)
potential_matches = []

if block_given?
original_enqueued_jobs_count = enqueued_jobs.count
@@ -395,6 +396,7 @@ def assert_enqueued_with(job: nil, args: nil, at: nil, queue: nil)

matching_job = jobs.find do |enqueued_job|
deserialized_job = deserialize_args_for_assertion(enqueued_job)
potential_matches << deserialized_job

expected_args.all? do |key, value|
if value.respond_to?(:call)
@@ -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 << "\n\nPotential matches: #{potential_matches.join("\n")}" if potential_matches.present?
assert matching_job, message
instantiate_job(matching_job)
end

@@ -457,6 +461,7 @@ def assert_enqueued_with(job: nil, args: nil, at: nil, queue: nil)
def assert_performed_with(job: nil, args: nil, at: nil, queue: nil, &block)
expected = { job: job, args: args, at: at, queue: queue }.compact
expected_args = prepare_args_for_assertion(expected)
potential_matches = []

if block_given?
original_performed_jobs_count = performed_jobs.count
@@ -470,6 +475,7 @@ def assert_performed_with(job: nil, args: nil, at: nil, queue: nil, &block)

matching_job = jobs.find do |enqueued_job|
deserialized_job = deserialize_args_for_assertion(enqueued_job)
potential_matches << deserialized_job

expected_args.all? do |key, value|
if value.respond_to?(:call)
@@ -480,7 +486,10 @@ def assert_performed_with(job: nil, args: nil, at: nil, queue: nil, &block)
end
end

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

instantiate_job(matching_job)
end

@@ -533,7 +533,7 @@ def test_assert_enqueued_with_failure
end
end

assert_equal 'No enqueued job found with {:job=>NestedJob, :queue=>"low"}', error.message
assert_match(/No enqueued job found with {:job=>NestedJob, :queue=>"low"}/, error.message)
end

def test_assert_enqueued_with_with_no_block_failure
@@ -547,7 +547,7 @@ def test_assert_enqueued_with_with_no_block_failure
assert_enqueued_with(job: NestedJob, queue: "low")
end

assert_equal 'No enqueued job found with {:job=>NestedJob, :queue=>"low"}', error.message
assert_match(/No enqueued job found with {:job=>NestedJob, :queue=>"low"}/, error.message)
end

def test_assert_enqueued_with_args
@@ -659,8 +659,8 @@ def test_assert_enqueued_with_failure_with_global_id_args
HelloJob.perform_later(ricardo)
end
end

assert_equal "No enqueued job found with {:job=>HelloJob, :args=>[#{wilma.inspect}]}", error.message
assert_match(/No enqueued job found with {:job=>HelloJob, :args=>\[#{wilma.inspect}\]}/, error.message)
assert_match(/Potential matches: {:job=>HelloJob, :args=>\[#<Person.* @id=\"9\"\>\], :queue=>\"default\"}/, error.message)
end

def test_assert_enqueued_with_failure_with_no_block_with_global_id_args
@@ -671,7 +671,8 @@ def test_assert_enqueued_with_failure_with_no_block_with_global_id_args
assert_enqueued_with(job: HelloJob, args: [wilma])
end

assert_equal "No enqueued job found with {:job=>HelloJob, :args=>[#{wilma.inspect}]}", error.message
assert_match(/No enqueued job found with {:job=>HelloJob, :args=>\[#{wilma.inspect}\]}/, error.message)
assert_match(/Potential matches: {:job=>HelloJob, :args=>\[#<Person.* @id=\"9\"\>\], :queue=>\"default\"}/, error.message)
end

def test_assert_enqueued_with_does_not_change_jobs_count
@@ -1823,8 +1824,8 @@ def test_assert_performed_with_failure_with_global_id_args
HelloJob.perform_later(ricardo)
end
end

assert_equal "No performed job found with {:job=>HelloJob, :args=>[#{wilma.inspect}]}", error.message
assert_match(/No performed job found with {:job=>HelloJob, :args=>\[#{wilma.inspect}\]}/, error.message)
assert_match(/Potential matches: {:job=>HelloJob, :args=>\[#<Person.* @id=\"9\"\>\], :queue=>\"default\"}/, error.message)
end

def test_assert_performed_with_without_block_failure_with_global_id_args
@@ -1836,7 +1837,8 @@ def test_assert_performed_with_without_block_failure_with_global_id_args
assert_performed_with(job: HelloJob, args: [wilma])
end

assert_equal "No performed job found with {:job=>HelloJob, :args=>[#{wilma.inspect}]}", error.message
assert_match(/No performed job found with {:job=>HelloJob, :args=>\[#{wilma.inspect}\]}/, error.message)
assert_match(/Potential matches: {:job=>HelloJob, :args=>\[#<Person.* @id=\"9\"\>\], :queue=>\"default\"}/, error.message)
end

def test_assert_performed_with_does_not_change_jobs_count
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.