diff options
author | bogdanvlviv <bogdanvlviv@gmail.com> | 2018-06-29 15:17:26 +0300 |
---|---|---|
committer | bogdanvlviv <bogdanvlviv@gmail.com> | 2018-06-29 15:37:02 +0300 |
commit | 4382fcbc22e6d7a30a7c7514ea531209ffc347dd (patch) | |
tree | cf1bdafc1d211f152bb498b9218fbbe83bdcd408 /activejob/lib/active_job/test_helper.rb | |
parent | b906ee59c0d6436f2801e23093d89d59d7fa447d (diff) | |
download | rails-4382fcbc22e6d7a30a7c7514ea531209ffc347dd.tar.gz rails-4382fcbc22e6d7a30a7c7514ea531209ffc347dd.tar.bz2 rails-4382fcbc22e6d7a30a7c7514ea531209ffc347dd.zip |
Allow call `assert_enqueued_with` and `assert_enqueued_email_with` with no block
Example of `assert_enqueued_with` with no block
```ruby
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
```
Example of `assert_enqueued_email_with` with no block:
```ruby
def test_email
ContactMailer.welcome.deliver_later
assert_enqueued_email_with ContactMailer, :welcome
end
def test_email_with_arguments
ContactMailer.welcome("Hello", "Goodbye").deliver_later
assert_enqueued_email_with ContactMailer, :welcome, args: ["Hello", "Goodbye"]
end
```
Related to #33243
Diffstat (limited to 'activejob/lib/active_job/test_helper.rb')
-rw-r--r-- | activejob/lib/active_job/test_helper.rb | 30 |
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 |