aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbogdanvlviv <bogdanvlviv@gmail.com>2018-06-30 18:20:04 +0300
committerbogdanvlviv <bogdanvlviv@gmail.com>2018-06-30 18:30:55 +0300
commit22c7d5650cb22ac75b5ee06830a58fcc22bb7acc (patch)
tree5ddfc6a769f5a871bbb2d4a79c1c770871bcc2d3
parentb3653aee947a5f8c3a298913e539493d17c1932b (diff)
downloadrails-22c7d5650cb22ac75b5ee06830a58fcc22bb7acc.tar.gz
rails-22c7d5650cb22ac75b5ee06830a58fcc22bb7acc.tar.bz2
rails-22c7d5650cb22ac75b5ee06830a58fcc22bb7acc.zip
Allow `queue` option to `assert_no_enqueued_jobs`
It can be asserted that no jobs are enqueued to a specific queue: ```ruby def test_no_logging assert_no_enqueued_jobs queue: 'default' do LoggingJob.set(queue: :some_queue).perform_later end end ```
-rw-r--r--activejob/CHANGELOG.md13
-rw-r--r--activejob/lib/active_job/test_helper.rb12
-rw-r--r--activejob/test/cases/test_helper_test.rb73
3 files changed, 96 insertions, 2 deletions
diff --git a/activejob/CHANGELOG.md b/activejob/CHANGELOG.md
index fea837585f..3ecbc2ff38 100644
--- a/activejob/CHANGELOG.md
+++ b/activejob/CHANGELOG.md
@@ -1,3 +1,16 @@
+* Allow `queue` option to `assert_no_enqueued_jobs`.
+
+ Example:
+ ```
+ def test_no_logging
+ assert_no_enqueued_jobs queue: 'default' do
+ LoggingJob.set(queue: :some_queue).perform_later
+ end
+ end
+ ```
+
+ *bogdanvlviv*
+
* Allow call `assert_enqueued_with` with no block.
Example:
diff --git a/activejob/lib/active_job/test_helper.rb b/activejob/lib/active_job/test_helper.rb
index 0ab3c5126d..04cde28a96 100644
--- a/activejob/lib/active_job/test_helper.rb
+++ b/activejob/lib/active_job/test_helper.rb
@@ -159,11 +159,19 @@ module ActiveJob
# end
# end
#
+ # It can be asserted that no jobs are enqueued to a specific queue:
+ #
+ # def test_no_logging
+ # assert_no_enqueued_jobs queue: 'default' do
+ # LoggingJob.set(queue: :some_queue).perform_later
+ # end
+ # end
+ #
# Note: This assertion is simply a shortcut for:
#
# assert_enqueued_jobs 0, &block
- def assert_no_enqueued_jobs(only: nil, except: nil, &block)
- assert_enqueued_jobs 0, only: only, except: except, &block
+ def assert_no_enqueued_jobs(only: nil, except: nil, queue: nil, &block)
+ assert_enqueued_jobs 0, only: only, except: except, queue: queue, &block
end
# Asserts that the number of performed jobs matches the given number.
diff --git a/activejob/test/cases/test_helper_test.rb b/activejob/test/cases/test_helper_test.rb
index 76d12bee82..d0a21a5da3 100644
--- a/activejob/test/cases/test_helper_test.rb
+++ b/activejob/test/cases/test_helper_test.rb
@@ -389,6 +389,79 @@ class EnqueuedJobsTest < ActiveJob::TestCase
assert_match(/`:only` and `:except`/, error.message)
end
+ def test_assert_no_enqueued_jobs_with_queue_option
+ assert_nothing_raised do
+ assert_no_enqueued_jobs queue: :default do
+ HelloJob.set(queue: :other_queue).perform_later
+ LoggingJob.set(queue: :other_queue).perform_later
+ end
+ end
+ end
+
+ def test_assert_no_enqueued_jobs_with_queue_option_failure
+ error = assert_raise ActiveSupport::TestCase::Assertion do
+ assert_no_enqueued_jobs queue: :other_queue do
+ HelloJob.set(queue: :other_queue).perform_later
+ end
+ end
+
+ assert_match(/0 .* but 1/, error.message)
+ end
+
+ def test_assert_no_enqueued_jobs_with_only_and_queue_option
+ assert_nothing_raised do
+ assert_no_enqueued_jobs only: HelloJob, queue: :some_queue do
+ HelloJob.set(queue: :other_queue).perform_later
+ HelloJob.set(queue: :other_queue).perform_later
+ LoggingJob.set(queue: :some_queue).perform_later
+ end
+ end
+ end
+
+ def test_assert_no_enqueued_jobs_with_only_and_queue_option_failure
+ error = assert_raise ActiveSupport::TestCase::Assertion do
+ assert_no_enqueued_jobs only: HelloJob, queue: :some_queue do
+ HelloJob.set(queue: :other_queue).perform_later
+ HelloJob.set(queue: :some_queue).perform_later
+ LoggingJob.set(queue: :some_queue).perform_later
+ end
+ end
+
+ assert_match(/0 .* but 1/, error.message)
+ end
+
+ def test_assert_no_enqueued_jobs_with_except_and_queue_option
+ assert_nothing_raised do
+ assert_no_enqueued_jobs except: LoggingJob, queue: :some_queue do
+ HelloJob.set(queue: :other_queue).perform_later
+ HelloJob.set(queue: :other_queue).perform_later
+ LoggingJob.set(queue: :some_queue).perform_later
+ end
+ end
+ end
+
+ def test_assert_no_enqueued_jobs_with_except_and_queue_option_failure
+ error = assert_raise ActiveSupport::TestCase::Assertion do
+ assert_no_enqueued_jobs except: LoggingJob, queue: :some_queue do
+ HelloJob.set(queue: :other_queue).perform_later
+ HelloJob.set(queue: :some_queue).perform_later
+ LoggingJob.set(queue: :some_queue).perform_later
+ end
+ end
+
+ assert_match(/0 .* but 1/, error.message)
+ end
+
+ def test_assert_no_enqueued_jobs_with_only_and_except_and_queue_option
+ error = assert_raise ArgumentError do
+ assert_no_enqueued_jobs only: HelloJob, except: HelloJob, queue: :some_queue do
+ HelloJob.set(queue: :other_queue).perform_later
+ end
+ end
+
+ assert_match(/`:only` and `:except`/, error.message)
+ end
+
def test_assert_enqueued_with
assert_enqueued_with(job: LoggingJob, queue: "default") do
LoggingJob.set(wait_until: Date.tomorrow.noon).perform_later