提交 3ebc2290 编写于 作者: A Alan Wu

Include deserialized arguments in jobs returned by AJ test helpers

`assert_enqueued_with` and `assert_performed_with` return a instantiated
instance of the matching job for further assertion (#21010).

Before this commit the `arguments` method on the returned instance
returns a serialized version of the arguments.
上级 c9e0a61d
* Include deserialized arguments in job instances returned from
`assert_enqueued_with` and `assert_performed_with`
*Alan Wu*
* Allow `assert_enqueued_with`/`assert_performed_with` methods to accept
a proc for the `args` argument. This is useful to check if only a subset of arguments
matches your expectations.
......
......@@ -594,8 +594,7 @@ def performed_jobs_with(only: nil, except: nil, queue: nil, &block)
def flush_enqueued_jobs(only: nil, except: nil, queue: nil)
enqueued_jobs_with(only: only, except: except, queue: queue) do |payload|
args = ActiveJob::Arguments.deserialize(payload[:args])
instantiate_job(payload.merge(args: args)).perform_now
instantiate_job(payload).perform_now
queue_adapter.performed_jobs << payload
end
end
......@@ -613,7 +612,8 @@ def deserialize_args_for_assertion(job)
end
def instantiate_job(payload)
job = payload[:job].new(*payload[:args])
args = ActiveJob::Arguments.deserialize(payload[:args])
job = payload[:job].new(*args)
job.scheduled_at = Time.at(payload[:at]) if payload.key?(:at)
job.queue_name = payload[:queue]
job
......
......@@ -476,23 +476,23 @@ def test_assert_enqueued_with_with_no_block
def test_assert_enqueued_with_returns
job = assert_enqueued_with(job: LoggingJob) do
LoggingJob.set(wait_until: 5.minutes.from_now).perform_later(1, 2, 3)
LoggingJob.set(wait_until: 5.minutes.from_now).perform_later(1, 2, 3, keyword: true)
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
assert_equal [1, 2, 3, { keyword: true }], job.arguments
end
def test_assert_enqueued_with_with_no_block_returns
LoggingJob.set(wait_until: 5.minutes.from_now).perform_later(1, 2, 3)
LoggingJob.set(wait_until: 5.minutes.from_now).perform_later(1, 2, 3, keyword: true)
job = assert_enqueued_with(job: LoggingJob)
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
assert_equal [1, 2, 3, { keyword: true }], job.arguments
end
def test_assert_enqueued_with_failure
......@@ -1515,26 +1515,26 @@ def test_assert_performed_with_without_block
end
def test_assert_performed_with_returns
job = assert_performed_with(job: NestedJob, queue: "default") do
NestedJob.perform_later
job = assert_performed_with(job: LoggingJob, queue: "default") do
LoggingJob.perform_later(keyword: :sym)
end
assert_instance_of NestedJob, job
assert_instance_of LoggingJob, job
assert_nil job.scheduled_at
assert_equal [], job.arguments
assert_equal [{ keyword: :sym }], job.arguments
assert_equal "default", job.queue_name
end
def test_assert_performed_with_without_block_returns
NestedJob.perform_later
LoggingJob.perform_later(keyword: :sym)
perform_enqueued_jobs
job = assert_performed_with(job: NestedJob, queue: "default")
job = assert_performed_with(job: LoggingJob, queue: "default")
assert_instance_of NestedJob, job
assert_instance_of LoggingJob, job
assert_nil job.scheduled_at
assert_equal [], job.arguments
assert_equal [{ keyword: :sym }], job.arguments
assert_equal "default", job.queue_name
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册