diff options
author | George Claghorn <george.claghorn@gmail.com> | 2015-01-07 23:33:01 -0500 |
---|---|---|
committer | George Claghorn <george.claghorn@gmail.com> | 2015-01-07 23:44:08 -0500 |
commit | b5e88317cc83436971d4b6d5d56629f9e43476d7 (patch) | |
tree | 34d681e047f3afe7de2483495aa476f7bfb7838d /activejob | |
parent | f6b21d48ef15d4f39a530653c2ce7d0cfb458b46 (diff) | |
download | rails-b5e88317cc83436971d4b6d5d56629f9e43476d7.tar.gz rails-b5e88317cc83436971d4b6d5d56629f9e43476d7.tar.bz2 rails-b5e88317cc83436971d4b6d5d56629f9e43476d7.zip |
Add :only option to assert_enqueued_jobs
With the option, assert_enqueued_jobs will check the number of times a specific kind of job is enqueued.
Diffstat (limited to 'activejob')
-rw-r--r-- | activejob/CHANGELOG.md | 13 | ||||
-rw-r--r-- | activejob/lib/active_job/test_helper.rb | 30 | ||||
-rw-r--r-- | activejob/test/cases/test_helper_test.rb | 40 |
3 files changed, 76 insertions, 7 deletions
diff --git a/activejob/CHANGELOG.md b/activejob/CHANGELOG.md index afdd42be33..bdeae29a50 100644 --- a/activejob/CHANGELOG.md +++ b/activejob/CHANGELOG.md @@ -1,3 +1,16 @@ +* Add :only option to assert_enqueued_jobs + + With the option, assert_enqueued_jobs will check the number of times a specific kind of job is enqueued: + + def test_logging_job + assert_enqueued_jobs 1, only: LoggingJob do + LoggingJob.perform_later + HelloJob.perform_later('jeremy') + end + end + + *George Claghorn* + * `ActiveJob::Base.deserialize` delegates to the job class diff --git a/activejob/lib/active_job/test_helper.rb b/activejob/lib/active_job/test_helper.rb index 2efcea7f2e..0d895a17db 100644 --- a/activejob/lib/active_job/test_helper.rb +++ b/activejob/lib/active_job/test_helper.rb @@ -42,16 +42,24 @@ module ActiveJob # HelloJob.perform_later('rafael') # end # end - def assert_enqueued_jobs(number) + # + # The number of times a specific job is enqueued can be asserted. + # + # def test_logging_job + # assert_enqueued_jobs 2, only: LoggingJob do + # LoggingJob.perform_later + # HelloJob.perform_later('jeremy') + # end + # end + def assert_enqueued_jobs(number, only: nil) if block_given? - original_count = enqueued_jobs.size + original_count = enqueued_jobs_size(only: only) yield - new_count = enqueued_jobs.size - assert_equal original_count + number, new_count, - "#{number} jobs expected, but #{new_count - original_count} were enqueued" + new_count = enqueued_jobs_size(only: only) + assert_equal original_count + number, new_count, "#{number} jobs expected, but #{new_count - original_count} were enqueued" else - enqueued_jobs_size = enqueued_jobs.size - assert_equal number, enqueued_jobs_size, "#{number} jobs expected, but #{enqueued_jobs_size} were enqueued" + actual_count = enqueued_jobs_size(only: only) + assert_equal number, actual_count, "#{number} jobs expected, but #{actual_count} were enqueued" end end @@ -215,6 +223,14 @@ module ActiveJob def clear_performed_jobs performed_jobs.clear end + + def enqueued_jobs_size(only: nil) + if only + enqueued_jobs.select { |job| job[:job] == only }.size + else + enqueued_jobs.size + end + end end end end diff --git a/activejob/test/cases/test_helper_test.rb b/activejob/test/cases/test_helper_test.rb index 784ede3674..21fe3db148 100644 --- a/activejob/test/cases/test_helper_test.rb +++ b/activejob/test/cases/test_helper_test.rb @@ -87,6 +87,46 @@ class EnqueuedJobsTest < ActiveJob::TestCase assert_match(/0 .* but 1/, error.message) end + def test_assert_enqueued_jobs_with_only_option + assert_nothing_raised do + assert_enqueued_jobs 1, only: HelloJob do + HelloJob.perform_later('jeremy') + LoggingJob.perform_later + end + end + end + + def test_assert_enqueued_jobs_with_only_option_and_none_sent + error = assert_raise ActiveSupport::TestCase::Assertion do + assert_enqueued_jobs 1, only: HelloJob do + LoggingJob.perform_later + end + end + + assert_match(/1 .* but 0/, error.message) + end + + def test_assert_enqueued_jobs_with_only_option_and_too_few_sent + error = assert_raise ActiveSupport::TestCase::Assertion do + assert_enqueued_jobs 5, only: HelloJob do + HelloJob.perform_later('jeremy') + 4.times { LoggingJob.perform_later } + end + end + + assert_match(/5 .* but 1/, error.message) + end + + def test_assert_enqueued_jobs_with_only_option_and_too_many_sent + error = assert_raise ActiveSupport::TestCase::Assertion do + assert_enqueued_jobs 1, only: HelloJob do + 2.times { HelloJob.perform_later('jeremy') } + end + end + + assert_match(/1 .* but 2/, error.message) + end + def test_assert_enqueued_job assert_enqueued_with(job: LoggingJob, queue: 'default') do LoggingJob.set(wait_until: Date.tomorrow.noon).perform_later |