1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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
```
This commit is contained in:
bogdanvlviv 2018-06-30 18:20:04 +03:00
parent b3653aee94
commit 22c7d5650c
No known key found for this signature in database
GPG key ID: E4ACD76A6DB6DFDD
3 changed files with 96 additions and 2 deletions

View file

@ -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:

View file

@ -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.

View file

@ -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