instance method
assert_performed_with
Ruby on Rails 6.0.6
Since v4.2.9Signature
assert_performed_with(job: nil, args: nil, at: nil, queue: nil, &block)
Asserts that the job has been performed with the given arguments.
def test_assert_performed_with
MyJob.perform_later(1,2,3)
perform_enqueued_jobs
assert_performed_with(job: MyJob, args: [1,2,3], queue: 'high')
MyJob.set(wait_until: Date.tomorrow.noon).perform_later
perform_enqueued_jobs
assert_performed_with(job: MyJob, at: Date.tomorrow.noon)
end
The at and args arguments also accept a proc.
To the at proc, it will get passed the actual job’s at argument.
def test_assert_enqueued_with
expected_time = ->(at) do
(Date.yesterday..Date.tomorrow).cover?(at)
end
MyJob.set(at: Date.today.noon).perform_later
assert_enqueued_with(job: MyJob, at: expected_time)
end
To the args proc, it will get passed the actual job’s arguments Your proc needs to return a boolean value determining if the job’s arguments matches your expectation. This is useful to check only for a subset of arguments.
def test_assert_performed_with
expected_args = ->(job_args) do
assert job_args.first.key?(:foo)
end
MyJob.perform_later(foo: 'bar', other_arg: 'No need to check in the test')
perform_enqueued_jobs
assert_performed_with(job: MyJob, args: expected_args, queue: 'high')
end
If a block is passed, that block performs all of the jobs that were enqueued throughout the duration of the block and asserts that the job has been performed with the given arguments in the block.
def test_assert_performed_with
assert_performed_with(job: MyJob, args: [1,2,3], queue: 'high') do
MyJob.perform_later(1,2,3)
end
assert_performed_with(job: MyJob, at: Date.tomorrow.noon) do
MyJob.set(wait_until: Date.tomorrow.noon).perform_later
end
end
Parameters
-
jobkey = nil -
argskey = nil -
atkey = nil -
queuekey = nil -
blockblock
Source
# File activejob/lib/active_job/test_helper.rb, line 481
def assert_performed_with(job: nil, args: nil, at: nil, queue: nil, &block)
expected = { job: job, args: args, at: at, queue: queue }.compact
expected_args = prepare_args_for_assertion(expected)
if block_given?
original_performed_jobs_count = performed_jobs.count
perform_enqueued_jobs(&block)
jobs = performed_jobs.drop(original_performed_jobs_count)
else
jobs = performed_jobs
end
matching_job = jobs.find do |enqueued_job|
deserialized_job = deserialize_args_for_assertion(enqueued_job)
expected_args.all? do |key, value|
if value.respond_to?(:call)
value.call(deserialized_job[key])
else
value == deserialized_job[key]
end
end
end
assert matching_job, "No performed job found with #{expected}"
instantiate_job(matching_job)
end
Defined in activejob/lib/active_job/test_helper.rb line 481
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveJob::TestHelper