From 060a1fb338854da2b99bced7709468f5d53ad368 Mon Sep 17 00:00:00 2001 From: Gareth du Plooy Date: Mon, 12 Aug 2019 14:12:23 -0500 Subject: [PATCH] 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. --- activejob/CHANGELOG.md | 4 ++++ activejob/lib/active_job/test_helper.rb | 13 +++++++++++-- activejob/test/cases/test_helper_test.rb | 18 ++++++++++-------- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/activejob/CHANGELOG.md b/activejob/CHANGELOG.md index 52acf381cb..d2703f9923 100644 --- a/activejob/CHANGELOG.md +++ b/activejob/CHANGELOG.md @@ -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* diff --git a/activejob/lib/active_job/test_helper.rb b/activejob/lib/active_job/test_helper.rb index a9103ffc25..dbc0310bc8 100644 --- a/activejob/lib/active_job/test_helper.rb +++ b/activejob/lib/active_job/test_helper.rb @@ -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 diff --git a/activejob/test/cases/test_helper_test.rb b/activejob/test/cases/test_helper_test.rb index f653c7874f..f72fe2e081 100644 --- a/activejob/test/cases/test_helper_test.rb +++ b/activejob/test/cases/test_helper_test.rb @@ -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=>\[#\], :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=>\[#\], :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=>\[#\], :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=>\[#\], :queue=>\"default\"}/, error.message) end def test_assert_performed_with_does_not_change_jobs_count