mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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 ```
This commit is contained in:
parent
e0cf042fa2
commit
d50fb21e4d
2 changed files with 70 additions and 2 deletions
|
@ -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"
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue