mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Make assert_enqueued_with and assert_performed_with returns the matched job
This commit is contained in:
parent
628a23cdd2
commit
b6d3a478fa
3 changed files with 38 additions and 2 deletions
|
@ -1,3 +1,8 @@
|
|||
* `assert_enqueued_with` and `assert_performed_with` now returns the matched
|
||||
job instance for further assertions.
|
||||
|
||||
*Jean Boussier*
|
||||
|
||||
* Include I18n.locale into job serialization/deserialization and use it around
|
||||
`perform`.
|
||||
|
||||
|
|
|
@ -233,10 +233,11 @@ module ActiveJob
|
|||
args.assert_valid_keys(:job, :args, :at, :queue)
|
||||
serialized_args = serialize_args_for_assertion(args)
|
||||
yield
|
||||
matching_job = enqueued_jobs.any? do |job|
|
||||
matching_job = enqueued_jobs.find do |job|
|
||||
serialized_args.all? { |key, value| value == job[key] }
|
||||
end
|
||||
assert matching_job, "No enqueued job found with #{args}"
|
||||
instanciate_job(matching_job)
|
||||
ensure
|
||||
queue_adapter.enqueued_jobs = original_enqueued_jobs + enqueued_jobs
|
||||
end
|
||||
|
@ -254,10 +255,11 @@ module ActiveJob
|
|||
args.assert_valid_keys(:job, :args, :at, :queue)
|
||||
serialized_args = serialize_args_for_assertion(args)
|
||||
perform_enqueued_jobs { yield }
|
||||
matching_job = performed_jobs.any? do |job|
|
||||
matching_job = performed_jobs.find do |job|
|
||||
serialized_args.all? { |key, value| value == job[key] }
|
||||
end
|
||||
assert matching_job, "No performed job found with #{args}"
|
||||
instanciate_job(matching_job)
|
||||
ensure
|
||||
queue_adapter.performed_jobs = original_performed_jobs + performed_jobs
|
||||
end
|
||||
|
@ -311,6 +313,13 @@ module ActiveJob
|
|||
end
|
||||
serialized_args
|
||||
end
|
||||
|
||||
def instanciate_job(payload)
|
||||
job = payload[:job].new(*payload[:args])
|
||||
job.scheduled_at = Time.at(payload[:at]) if payload.key?(:at)
|
||||
job.queue_name = payload[:queue]
|
||||
job
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -165,6 +165,17 @@ class EnqueuedJobsTest < ActiveJob::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
def test_assert_enqueued_job_returns
|
||||
job = assert_enqueued_with(job: LoggingJob) do
|
||||
LoggingJob.set(wait_until: 5.minutes.from_now).perform_later(1, 2, 3)
|
||||
end
|
||||
|
||||
assert_instance_of LoggingJob, job
|
||||
assert_in_delta 5.minutes.from_now, job.scheduled_at, 1
|
||||
assert_equal 'default', job.queue_name
|
||||
assert_equal [1, 2, 3], job.arguments
|
||||
end
|
||||
|
||||
def test_assert_enqueued_job_failure
|
||||
assert_raise ActiveSupport::TestCase::Assertion do
|
||||
assert_enqueued_with(job: LoggingJob, queue: 'default') do
|
||||
|
@ -397,6 +408,17 @@ class PerformedJobsTest < ActiveJob::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
def test_assert_performed_job_returns
|
||||
job = assert_performed_with(job: NestedJob, queue: 'default') do
|
||||
NestedJob.perform_later
|
||||
end
|
||||
|
||||
assert_instance_of NestedJob, job
|
||||
assert_nil job.scheduled_at
|
||||
assert_equal [], job.arguments
|
||||
assert_equal 'default', job.queue_name
|
||||
end
|
||||
|
||||
def test_assert_performed_job_failure
|
||||
assert_raise ActiveSupport::TestCase::Assertion do
|
||||
assert_performed_with(job: LoggingJob, at: Date.tomorrow.noon, queue: 'default') do
|
||||
|
|
Loading…
Reference in a new issue