From d50fb21e4d7a526de3b0edb9bb5032d4be6175ae Mon Sep 17 00:00:00 2001 From: bogdanvlviv Date: Thu, 16 Aug 2018 19:21:12 +0300 Subject: Allow `:queue` option to `assert_performed_jobs`. If the `:queue` option is specified, then only the job(s) enqueued to a specific queue will be performed. Example: ``` def test_assert_performed_jobs_with_queue_option assert_performed_jobs 1, queue: :some_queue do HelloJob.set(queue: :some_queue).perform_later("jeremy") HelloJob.set(queue: :other_queue).perform_later("bogdan") end end ``` --- activejob/lib/active_job/test_helper.rb | 14 ++++++-- activejob/test/cases/test_helper_test.rb | 58 ++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) (limited to 'activejob') diff --git a/activejob/lib/active_job/test_helper.rb b/activejob/lib/active_job/test_helper.rb index 22568c9aa5..3be74e504b 100644 --- a/activejob/lib/active_job/test_helper.rb +++ b/activejob/lib/active_job/test_helper.rb @@ -237,10 +237,20 @@ module ActiveJob # end # end # end - def assert_performed_jobs(number, only: nil, except: nil) + # + # If the +:queue+ option is specified, + # then only the job(s) enqueued to a specific queue will be performed. + # + # def test_assert_performed_jobs_with_queue_option + # assert_performed_jobs 1, queue: :some_queue do + # HelloJob.set(queue: :some_queue).perform_later("jeremy") + # HelloJob.set(queue: :other_queue).perform_later("bogdan") + # end + # end + def assert_performed_jobs(number, only: nil, except: nil, queue: nil) if block_given? original_count = performed_jobs.size - perform_enqueued_jobs(only: only, except: except) { yield } + perform_enqueued_jobs(only: only, except: except, queue: queue) { yield } new_count = performed_jobs.size assert_equal number, new_count - original_count, "#{number} jobs expected, but #{new_count - original_count} were performed" diff --git a/activejob/test/cases/test_helper_test.rb b/activejob/test/cases/test_helper_test.rb index f99619b44b..8287001d7c 100644 --- a/activejob/test/cases/test_helper_test.rb +++ b/activejob/test/cases/test_helper_test.rb @@ -1026,6 +1026,64 @@ class PerformedJobsTest < ActiveJob::TestCase assert_match(/`:only` and `:except`/, error.message) end + def test_assert_performed_jobs_with_queue_option + assert_performed_jobs 1, queue: :some_queue do + HelloJob.set(queue: :some_queue).perform_later("jeremy") + HelloJob.set(queue: :other_queue).perform_later("bogdan") + end + end + + def test_assert_performed_jobs_with_queue_option_failure + error = assert_raise ActiveSupport::TestCase::Assertion do + assert_performed_jobs 1, queue: :some_queue do + HelloJob.set(queue: :other_queue).perform_later("jeremy") + HelloJob.set(queue: :other_queue).perform_later("bogdan") + end + end + + assert_match(/1 .* but 0/, error.message) + end + + def test_assert_performed_jobs_with_only_and_queue_options + assert_performed_jobs 1, only: HelloJob, queue: :some_queue do + HelloJob.set(queue: :some_queue).perform_later("jeremy") + HelloJob.set(queue: :other_queue).perform_later("bogdan") + LoggingJob.set(queue: :some_queue).perform_later("jeremy") + end + end + + def test_assert_performed_jobs_with_only_and_queue_options_failure + error = assert_raise ActiveSupport::TestCase::Assertion do + assert_performed_jobs 1, only: HelloJob, queue: :some_queue do + HelloJob.set(queue: :other_queue).perform_later("jeremy") + HelloJob.set(queue: :other_queue).perform_later("bogdan") + LoggingJob.set(queue: :some_queue).perform_later("jeremy") + end + end + + assert_match(/1 .* but 0/, error.message) + end + + def test_assert_performed_jobs_with_except_and_queue_options + assert_performed_jobs 1, except: HelloJob, queue: :other_queue do + HelloJob.set(queue: :other_queue).perform_later("jeremy") + LoggingJob.set(queue: :some_queue).perform_later("bogdan") + LoggingJob.set(queue: :other_queue).perform_later("jeremy") + end + end + + def test_assert_performed_jobs_with_except_and_queue_options_failuree + error = assert_raise ActiveSupport::TestCase::Assertion do + assert_performed_jobs 1, except: HelloJob, queue: :other_queue do + HelloJob.set(queue: :other_queue).perform_later("jeremy") + LoggingJob.set(queue: :some_queue).perform_later("bogdan") + LoggingJob.set(queue: :some_queue).perform_later("jeremy") + end + end + + assert_match(/1 .* but 0/, error.message) + end + def test_assert_no_performed_jobs_with_only_option assert_nothing_raised do assert_no_performed_jobs only: HelloJob do -- cgit v1.2.3