Skip to content

Commit 29e502f

Browse files
committed
Improve active_job test_helper error messages
Error messages from assert_enqueued_with and assert_performed_with shows which other jobs are enqueued or performed when you get an error. We can improve these messages to make it clearer why the job didn't match. This one error messages now have three different formats based on why the job didn't match 1. Didn't match because no jobs are queued at all. This now reports, ``` No performed job found with {:job=>HelloJob, :args=>[]} No jobs where performed ``` 2. Didn't match because that job class was not queued. This now reports, ``` No performed job found with {:job=>MultipleKwargsJob, :args=>[#<Person:0x00007fe38f9f8100 @id=11>]} No jobs of class MultipleKwargsJob where performed, job classes performed: HelloJob ``` 3. Doesn't match due to arguments, queue, priority or other reason. This now reports ``` No performed job found with {:job=>HelloJob, :args=>[#<Person:0x00007fe3a89fc2c8 @id=11>]} Potential matches: {"job_class"=>"HelloJob", "job_id"=>"f8636fd9-c7a0-4565-9198-17e175f30f0e", "provider_job_id"=>nil, "queue_name"=>"default", "priority"=>nil, "arguments"=>[{"_aj_globalid"=>"gid://aj/Person/9"}], "executions"=>0, "exception_executions"=>{}, "locale"=>"en", "timezone"=>nil, "enqueued_at"=>"2021-10-27T23:19:54Z", :job=>HelloJob, :args=>[#<Person:0x00007fe3a89dce00 @id="9">], :queue=>"default", :priority=>nil} ``` Which matches the old error message, but only reports jobs that where queued of the same class as the job you are asserting was queued.
1 parent 18922cc commit 29e502f

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

activejob/lib/active_job/test_helper.rb

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,8 +417,20 @@ def assert_enqueued_with(job: nil, args: nil, at: nil, queue: nil, priority: nil
417417
end
418418
end
419419

420+
matching_class = potential_matches.select do |enqueued_job|
421+
enqueued_job["job_class"] == job.to_s
422+
end
423+
420424
message = +"No enqueued job found with #{expected}"
421-
message << "\n\nPotential matches: #{potential_matches.join("\n")}" if potential_matches.present?
425+
if potential_matches.empty?
426+
message << "\n\nNo jobs where enqueued"
427+
elsif matching_class.empty?
428+
message << "\n\nNo jobs of class #{expected[:job]} where enqueued, job classes enqueued: "
429+
message << potential_matches.map { |job| job["job_class"] }.join(", ")
430+
else
431+
message << "\n\nPotential matches: #{matching_class.join("\n")}"
432+
end
433+
422434
assert matching_job, message
423435
instantiate_job(matching_job)
424436
end
@@ -507,8 +519,20 @@ def assert_performed_with(job: nil, args: nil, at: nil, queue: nil, priority: ni
507519
end
508520
end
509521

522+
matching_class = potential_matches.select do |enqueued_job|
523+
enqueued_job["job_class"] == job.to_s
524+
end
525+
510526
message = +"No performed job found with #{expected}"
511-
message << "\n\nPotential matches: #{potential_matches.join("\n")}" if potential_matches.present?
527+
if potential_matches.empty?
528+
message << "\n\nNo jobs where performed"
529+
elsif matching_class.empty?
530+
message << "\n\nNo jobs of class #{expected[:job]} where performed, job classes performed: "
531+
message << potential_matches.map { |job| job["job_class"] }.join(", ")
532+
else
533+
message << "\n\nPotential matches: #{matching_class.join("\n")}"
534+
end
535+
512536
assert matching_job, message
513537

514538
instantiate_job(matching_job)

activejob/test/cases/test_helper_test.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,10 +698,35 @@ def test_assert_enqueued_with_failure_with_global_id_args
698698
HelloJob.perform_later(ricardo)
699699
end
700700
end
701+
701702
assert_match(/No enqueued job found with {:job=>HelloJob, :args=>\[#{wilma.inspect}\]}/, error.message)
702703
assert_match(/Potential matches: {.*?:job=>HelloJob, :args=>\[#<Person.* @id="9">\], :queue=>"default".*?}/, error.message)
703704
end
704705

706+
def test_show_jobs_that_are_enqueued_when_job_is_not_queued_at_all
707+
ricardo = Person.new(9)
708+
wilma = Person.new(11)
709+
710+
error = assert_raise ActiveSupport::TestCase::Assertion do
711+
assert_enqueued_with(job: MultipleKwargsJob, args: [wilma]) do
712+
HelloJob.perform_later(ricardo)
713+
end
714+
end
715+
716+
assert_match(/No enqueued job found with {:job=>MultipleKwargsJob, :args=>\[#{wilma.inspect}\]}/, error.message)
717+
assert_match(/No jobs of class MultipleKwargsJob where enqueued, job classes enqueued: HelloJob/, error.message)
718+
end
719+
720+
def test_shows_no_jobs_enqueued_when_there_are_no_jobs
721+
error = assert_raise ActiveSupport::TestCase::Assertion do
722+
assert_enqueued_with(job: HelloJob, args: []) do
723+
end
724+
end
725+
726+
assert_match(/No enqueued job found with {:job=>HelloJob, :args=>\[\]}/, error.message)
727+
assert_match(/No jobs where enqueued/, error.message)
728+
end
729+
705730
def test_assert_enqueued_with_failure_with_no_block_with_global_id_args
706731
ricardo = Person.new(9)
707732
wilma = Person.new(11)
@@ -1954,6 +1979,28 @@ def test_assert_performed_with_without_block_failure_with_global_id_args
19541979
assert_match(/Potential matches: {.*?:job=>HelloJob, :args=>\[#<Person.* @id="9">\], :queue=>"default".*?}/, error.message)
19551980
end
19561981

1982+
def test_assert_performed_says_no_jobs_performed
1983+
error = assert_raise ActiveSupport::TestCase::Assertion do
1984+
assert_performed_with(job: HelloJob, args: [])
1985+
end
1986+
1987+
assert_match(/No performed job found with {:job=>HelloJob, :args=>\[\]}/, error.message)
1988+
assert_match(/No jobs where performed/, error.message)
1989+
end
1990+
1991+
def test_assert_performed_when_not_matching_the_class_shows_alteratives
1992+
ricardo = Person.new(9)
1993+
wilma = Person.new(11)
1994+
HelloJob.perform_later(ricardo)
1995+
perform_enqueued_jobs
1996+
error = assert_raise ActiveSupport::TestCase::Assertion do
1997+
assert_performed_with(job: MultipleKwargsJob, args: [wilma])
1998+
end
1999+
2000+
assert_match(/No performed job found with {:job=>MultipleKwargsJob, :args=>\[#<Person.* @id=11>\]}/, error.message)
2001+
assert_match(/No jobs of class MultipleKwargsJob where performed, job classes performed: HelloJob/, error.message)
2002+
end
2003+
19572004
def test_assert_performed_with_does_not_change_jobs_count
19582005
assert_performed_with(job: HelloJob) do
19592006
HelloJob.perform_later

0 commit comments

Comments
 (0)