From cdb16ac576198607916cde6d55fe14cb775a98c9 Mon Sep 17 00:00:00 2001 From: Edouard CHIN Date: Wed, 21 Nov 2018 22:43:14 +0100 Subject: Allow all ActiveJob assertion helper to accept Proc in their `only` kw: - That feature is useful to enqueue or assert that jobs got enqueued or performed based on dynamic conditions. We will be able to leverage that feature to fix all ActionMailer assertion helper issue when a Mailer define a custom delivery job (see next commit). --- activejob/test/cases/test_helper_test.rb | 56 ++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'activejob/test/cases') diff --git a/activejob/test/cases/test_helper_test.rb b/activejob/test/cases/test_helper_test.rb index 8dc32037ff..046033921d 100644 --- a/activejob/test/cases/test_helper_test.rb +++ b/activejob/test/cases/test_helper_test.rb @@ -114,6 +114,16 @@ class EnqueuedJobsTest < ActiveJob::TestCase end end + def test_assert_enqueued_jobs_with_only_option_as_proc + assert_nothing_raised do + assert_enqueued_jobs(1, only: ->(job) { job.fetch(:job).name == "HelloJob" }) do + HelloJob.perform_later("jeremy") + LoggingJob.perform_later + LoggingJob.perform_later + end + end + end + def test_assert_enqueued_jobs_with_except_option assert_nothing_raised do assert_enqueued_jobs 1, except: LoggingJob do @@ -124,6 +134,16 @@ class EnqueuedJobsTest < ActiveJob::TestCase end end + def test_assert_enqueued_jobs_with_except_option_as_proc + assert_nothing_raised do + assert_enqueued_jobs(1, except: ->(job) { job.fetch(:job).name == "LoggingJob" }) do + HelloJob.perform_later("jeremy") + LoggingJob.perform_later + LoggingJob.perform_later + end + end + end + def test_assert_enqueued_jobs_with_only_and_except_option error = assert_raise ArgumentError do assert_enqueued_jobs 1, only: HelloJob, except: HelloJob do @@ -911,6 +931,15 @@ class PerformedJobsTest < ActiveJob::TestCase end end + def test_assert_performed_jobs_with_only_option_as_proc + assert_nothing_raised do + assert_performed_jobs(1, only: ->(job) { job.is_a?(HelloJob) }) do + HelloJob.perform_later("jeremy") + LoggingJob.perform_later("bogdan") + end + end + end + def test_assert_performed_jobs_without_block_with_only_option HelloJob.perform_later("jeremy") LoggingJob.perform_later("bogdan") @@ -920,6 +949,15 @@ class PerformedJobsTest < ActiveJob::TestCase assert_performed_jobs 1, only: HelloJob end + def test_assert_performed_jobs_without_block_with_only_option_as_proc + HelloJob.perform_later("jeremy") + LoggingJob.perform_later("bogdan") + + perform_enqueued_jobs + + assert_performed_jobs(1, only: ->(job) { job.fetch(:job).name == "HelloJob" }) + end + def test_assert_performed_jobs_without_block_with_only_option_failure LoggingJob.perform_later("jeremy") LoggingJob.perform_later("bogdan") @@ -942,6 +980,15 @@ class PerformedJobsTest < ActiveJob::TestCase end end + def test_assert_performed_jobs_with_except_option_as_proc + assert_nothing_raised do + assert_performed_jobs(1, except: ->(job) { job.is_a?(HelloJob) }) do + HelloJob.perform_later("jeremy") + LoggingJob.perform_later("bogdan") + end + end + end + def test_assert_performed_jobs_without_block_with_except_option HelloJob.perform_later("jeremy") LoggingJob.perform_later("bogdan") @@ -951,6 +998,15 @@ class PerformedJobsTest < ActiveJob::TestCase assert_performed_jobs 1, except: HelloJob end + def test_assert_performed_jobs_without_block_with_except_option_as_proc + HelloJob.perform_later("jeremy") + LoggingJob.perform_later("bogdan") + + perform_enqueued_jobs + + assert_performed_jobs(1, except: ->(job) { job.fetch(:job).name == "HelloJob" }) + end + def test_assert_performed_jobs_without_block_with_except_option_failure HelloJob.perform_later("jeremy") HelloJob.perform_later("bogdan") -- cgit v1.2.3