moduleActiveJob# Provides helper methods for testing Active JobmoduleTestHelperextendActiveSupport::Concernincludeddodefbefore_setup@old_queue_adapter=queue_adapterActiveJob::Base.queue_adapter=:testclear_enqueued_jobsclear_performed_jobssuperenddefafter_teardownsuperActiveJob::Base.queue_adapter=@old_queue_adapterend# Asserts that the number of enqueued jobs matches the given number.## def test_jobs# assert_enqueued_jobs 0# HelloJob.perform_later('david')# assert_enqueued_jobs 1# HelloJob.perform_later('abdelkader')# assert_enqueued_jobs 2# end## If a block is passed, that block should cause the specified number of# jobs to be enqueued.## def test_jobs_again# assert_enqueued_jobs 1 do# HelloJob.perform_later('cristian')# end## assert_enqueued_jobs 2 do# HelloJob.perform_later('aaron')# HelloJob.perform_later('rafael')# end# enddefassert_enqueued_jobs(number)ifblock_given?original_count=enqueued_jobs.sizeyieldnew_count=enqueued_jobs.sizeassert_equaloriginal_count+number,new_count,"#{number} jobs expected, but #{new_count-original_count} were enqueued"elseenqueued_jobs_size=enqueued_jobs.sizeassert_equalnumber,enqueued_jobs_size,"#{number} jobs expected, but #{enqueued_jobs_size} were enqueued"endend# Assert that no job have been enqueued.## def test_jobs# assert_no_enqueued_jobs# HelloJob.perform_later('jeremy')# assert_enqueued_jobs 1# end## If a block is passed, that block should not cause any job to be enqueued.## def test_jobs_again# assert_no_enqueued_jobs do# # No job should be enqueued from this block# end# end## Note: This assertion is simply a shortcut for:## assert_enqueued_jobs 0defassert_no_enqueued_jobs(&block)assert_enqueued_jobs0,&blockend# Asserts that the number of performed jobs matches the given number.## def test_jobs# assert_performed_jobs 0# HelloJob.perform_later('xavier')# assert_performed_jobs 1# HelloJob.perform_later('yves')# assert_performed_jobs 2# end## If a block is passed, that block should cause the specified number of# jobs to be performed.## def test_jobs_again# assert_performed_jobs 1 do# HelloJob.perform_later('robin')# end## assert_performed_jobs 2 do# HelloJob.perform_later('carlos')# HelloJob.perform_later('sean')# end# enddefassert_performed_jobs(number)ifblock_given?original_count=performed_jobs.sizeyieldnew_count=performed_jobs.sizeassert_equaloriginal_count+number,new_count,"#{number} jobs expected, but #{new_count-original_count} were performed"elseperformed_jobs_size=performed_jobs.sizeassert_equalnumber,performed_jobs_size,"#{number} jobs expected, but #{performed_jobs_size} were performed"endend# Asserts that no jobs have been performed.## def test_jobs# assert_no_performed_jobs# HelloJob.perform_later('matthew')# assert_performed_jobs 1# end## If a block is passed, that block should not cause any job to be performed.## def test_jobs_again# assert_no_performed_jobs do# # No job should be performed from this block# end# end## Note: This assertion is simply a shortcut for:## assert_performed_jobs 0defassert_no_performed_jobs(&block)assert_performed_jobs0,&blockend# Asserts that the job passed in the block has been enqueued with the given arguments.## def assert_enqueued_job# assert_enqueued_with(job: MyJob, args: [1,2,3], queue: 'low') do# MyJob.perform_later(1,2,3)# end# enddefassert_enqueued_with(args={},&_block)original_enqueued_jobs=enqueued_jobs.dupclear_enqueued_jobsargs.assert_valid_keys(:job,:args,:at,:queue)yieldmatching_job=enqueued_jobs.any?do|job|args.all?{|key,value|value==job[key]}endassertmatching_job,"No enqueued job found with #{args}"ensurequeue_adapter.enqueued_jobs=original_enqueued_jobs+enqueued_jobsend# Asserts that the job passed in the block has been performed with the given arguments.## def test_assert_performed_with# assert_performed_with(job: MyJob, args: [1,2,3], queue: 'high') do# MyJob.perform_later(1,2,3)# end# enddefassert_performed_with(args={},&_block)original_performed_jobs=performed_jobs.dupclear_performed_jobsargs.assert_valid_keys(:job,:args,:at,:queue)yieldmatching_job=performed_jobs.any?do|job|args.all?{|key,value|value==job[key]}endassertmatching_job,"No performed job found with #{args}"ensurequeue_adapter.performed_jobs=original_performed_jobs+performed_jobsenddefqueue_adapterActiveJob::Base.queue_adapterenddelegate:enqueued_jobs,:enqueued_jobs=,:performed_jobs,:performed_jobs=,to: :queue_adapterprivatedefclear_enqueued_jobsenqueued_jobs.clearenddefclear_performed_jobsperformed_jobs.clearendendendend