aboutsummaryrefslogtreecommitdiffstats
path: root/activejob/lib/active_job
diff options
context:
space:
mode:
authorRafael França <rafaelmfranca@gmail.com>2018-06-29 14:52:26 +0200
committerGitHub <noreply@github.com>2018-06-29 14:52:26 +0200
commit1f4202611cae45ca9047d9fec73a7b631e934fa7 (patch)
treecf1bdafc1d211f152bb498b9218fbbe83bdcd408 /activejob/lib/active_job
parentb906ee59c0d6436f2801e23093d89d59d7fa447d (diff)
parent4382fcbc22e6d7a30a7c7514ea531209ffc347dd (diff)
downloadrails-1f4202611cae45ca9047d9fec73a7b631e934fa7.tar.gz
rails-1f4202611cae45ca9047d9fec73a7b631e934fa7.tar.bz2
rails-1f4202611cae45ca9047d9fec73a7b631e934fa7.zip
Merge pull request #33258 from bogdanvlviv/allow-callassert_enqueued_with-with-no-block
Allow call `assert_enqueued_with` and `assert_enqueued_email_with` with no block
Diffstat (limited to 'activejob/lib/active_job')
-rw-r--r--activejob/lib/active_job/test_helper.rb30
1 files changed, 25 insertions, 5 deletions
diff --git a/activejob/lib/active_job/test_helper.rb b/activejob/lib/active_job/test_helper.rb
index 1cd2c40c15..0a9ca3a814 100644
--- a/activejob/lib/active_job/test_helper.rb
+++ b/activejob/lib/active_job/test_helper.rb
@@ -286,7 +286,18 @@ module ActiveJob
assert_performed_jobs 0, only: only, except: except, &block
end
- # Asserts that the job passed in the block has been enqueued with the given arguments.
+ # Asserts that the job has been enqueued with the given arguments.
+ #
+ # def test_assert_enqueued_with
+ # MyJob.perform_later(1,2,3)
+ # assert_enqueued_with(job: MyJob, args: [1,2,3], queue: 'low')
+ #
+ # MyJob.set(wait_until: Date.tomorrow.noon).perform_later
+ # assert_enqueued_with(job: MyJob, at: Date.tomorrow.noon)
+ # end
+ #
+ # If a block is passed, that block should cause the job to be
+ # enqueued with the given arguments.
#
# def test_assert_enqueued_with
# assert_enqueued_with(job: MyJob, args: [1,2,3], queue: 'low') do
@@ -298,14 +309,23 @@ module ActiveJob
# end
# end
def assert_enqueued_with(job: nil, args: nil, at: nil, queue: nil)
- original_enqueued_jobs_count = enqueued_jobs.count
expected = { job: job, args: args, at: at, queue: queue }.compact
serialized_args = serialize_args_for_assertion(expected)
- yield
- in_block_jobs = enqueued_jobs.drop(original_enqueued_jobs_count)
- matching_job = in_block_jobs.find do |in_block_job|
+
+ if block_given?
+ original_enqueued_jobs_count = enqueued_jobs.count
+
+ yield
+
+ jobs = enqueued_jobs.drop(original_enqueued_jobs_count)
+ else
+ jobs = enqueued_jobs
+ end
+
+ matching_job = jobs.find do |in_block_job|
serialized_args.all? { |key, value| value == in_block_job[key] }
end
+
assert matching_job, "No enqueued job found with #{expected}"
instantiate_job(matching_job)
end