Skip to content

Commit

Permalink
Allow call assert_enqueued_with and assert_enqueued_email_with wi…
Browse files Browse the repository at this point in the history
…th 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
  • Loading branch information
bogdanvlviv committed Jun 29, 2018
1 parent b906ee5 commit 4382fcb
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 16 deletions.
17 changes: 17 additions & 0 deletions actionmailer/CHANGELOG.md
@@ -1,3 +1,20 @@
* Allow call `assert_enqueued_email_with` with no block.

Example:
```
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
```

*bogdanvlviv*

* Ensure mail gem is eager autoloaded when eager load is true to prevent thread deadlocks.

*Samuel Cochran*
Expand Down
31 changes: 29 additions & 2 deletions actionmailer/test/test_helper_test.rb
Expand Up @@ -252,7 +252,16 @@ def test_assert_enqueued_email_with
end
end

def test_assert_enqueued_email_with_args
def test_assert_enqueued_email_with_with_no_block
assert_nothing_raised do
silence_stream($stdout) do
TestHelperMailer.test.deliver_later
assert_enqueued_email_with TestHelperMailer, :test
end
end
end

def test_assert_enqueued_email_with_with_args
assert_nothing_raised do
assert_enqueued_email_with TestHelperMailer, :test_args, args: ["some_email", "some_name"] do
silence_stream($stdout) do
Expand All @@ -262,7 +271,16 @@ def test_assert_enqueued_email_with_args
end
end

def test_assert_enqueued_email_with_parameterized_args
def test_assert_enqueued_email_with_with_no_block_with_args
assert_nothing_raised do
silence_stream($stdout) do
TestHelperMailer.test_args("some_email", "some_name").deliver_later
assert_enqueued_email_with TestHelperMailer, :test_args, args: ["some_email", "some_name"]
end
end
end

def test_assert_enqueued_email_with_with_parameterized_args
assert_nothing_raised do
assert_enqueued_email_with TestHelperMailer, :test_parameter_args, args: { all: "good" } do
silence_stream($stdout) do
Expand All @@ -271,6 +289,15 @@ def test_assert_enqueued_email_with_parameterized_args
end
end
end

def test_assert_enqueued_email_with_with_no_block_wiht_parameterized_args
assert_nothing_raised do
silence_stream($stdout) do
TestHelperMailer.with(all: "good").test_parameter_args.deliver_later
assert_enqueued_email_with TestHelperMailer, :test_parameter_args, args: { all: "good" }
end
end
end
end

class AnotherTestHelperMailerTest < ActionMailer::TestCase
Expand Down
15 changes: 15 additions & 0 deletions activejob/CHANGELOG.md
@@ -1,3 +1,18 @@
* Allow call `assert_enqueued_with` with no block.

Example:
```
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
```

*bogdanvlviv*

* Allow passing multiple exceptions to `retry_on`, and `discard_on`.

*George Claghorn*
Expand Down
30 changes: 25 additions & 5 deletions activejob/lib/active_job/test_helper.rb
Expand Up @@ -286,7 +286,18 @@ def assert_no_performed_jobs(only: nil, except: nil, &block)
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
Expand All @@ -298,14 +309,23 @@ def assert_no_performed_jobs(only: nil, except: nil, &block)
# 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
Expand Down
83 changes: 74 additions & 9 deletions activejob/test/cases/test_helper_test.rb
Expand Up @@ -389,13 +389,18 @@ def test_assert_no_enqueued_jobs_with_only_and_except_option_as_array
assert_match(/`:only` and `:except`/, error.message)
end

def test_assert_enqueued_job
def test_assert_enqueued_with
assert_enqueued_with(job: LoggingJob, queue: "default") do
LoggingJob.set(wait_until: Date.tomorrow.noon).perform_later
end
end

def test_assert_enqueued_job_returns
def test_assert_enqueued_with_with_no_block
LoggingJob.set(wait_until: Date.tomorrow.noon).perform_later
assert_enqueued_with(job: LoggingJob, queue: "default")
end

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)
end
Expand All @@ -406,13 +411,28 @@ def test_assert_enqueued_job_returns
assert_equal [1, 2, 3], job.arguments
end

def test_assert_enqueued_job_failure
def test_assert_enqueued_with_with_no_block_returns
LoggingJob.set(wait_until: 5.minutes.from_now).perform_later(1, 2, 3)
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
end

def test_assert_enqueued_with_failure
assert_raise ActiveSupport::TestCase::Assertion do
assert_enqueued_with(job: LoggingJob, queue: "default") do
NestedJob.perform_later
end
end

assert_raise ActiveSupport::TestCase::Assertion do
LoggingJob.perform_later
assert_enqueued_with(job: LoggingJob) {}
end

error = assert_raise ActiveSupport::TestCase::Assertion do
assert_enqueued_with(job: NestedJob, queue: "low") do
NestedJob.perform_later
Expand All @@ -422,28 +442,54 @@ def test_assert_enqueued_job_failure
assert_equal 'No enqueued job found with {:job=>NestedJob, :queue=>"low"}', error.message
end

def test_assert_enqueued_job_args
def test_assert_enqueued_with_with_no_block_failure
assert_raise ActiveSupport::TestCase::Assertion do
NestedJob.perform_later
assert_enqueued_with(job: LoggingJob, queue: "default")
end

error = assert_raise ActiveSupport::TestCase::Assertion do
NestedJob.perform_later
assert_enqueued_with(job: NestedJob, queue: "low")
end

assert_equal 'No enqueued job found with {:job=>NestedJob, :queue=>"low"}', error.message
end

def test_assert_enqueued_with_args
assert_raise ArgumentError do
assert_enqueued_with(class: LoggingJob) do
NestedJob.set(wait_until: Date.tomorrow.noon).perform_later
end
end
end

def test_assert_enqueued_job_with_at_option
assert_enqueued_with(job: HelloJob, at: Date.tomorrow.noon) do
HelloJob.set(wait_until: Date.tomorrow.noon).perform_later
def test_assert_enqueued_with_with_no_block_args
assert_raise ArgumentError do
NestedJob.set(wait_until: Date.tomorrow.noon).perform_later
assert_enqueued_with(class: LoggingJob)
end
end

def test_assert_enqueued_job_with_global_id_args
def test_assert_enqueued_with_with_no_block_with_at_option
HelloJob.set(wait_until: Date.tomorrow.noon).perform_later
assert_enqueued_with(job: HelloJob, at: Date.tomorrow.noon)
end

def test_assert_enqueued_with_with_global_id_args
ricardo = Person.new(9)
assert_enqueued_with(job: HelloJob, args: [ricardo]) do
HelloJob.perform_later(ricardo)
end
end

def test_assert_enqueued_job_failure_with_global_id_args
def test_assert_enqueued_with_with_no_block_with_global_id_args
ricardo = Person.new(9)
HelloJob.perform_later(ricardo)
assert_enqueued_with(job: HelloJob, args: [ricardo])
end

def test_assert_enqueued_with_failure_with_global_id_args
ricardo = Person.new(9)
wilma = Person.new(11)
error = assert_raise ActiveSupport::TestCase::Assertion do
Expand All @@ -455,6 +501,17 @@ def test_assert_enqueued_job_failure_with_global_id_args
assert_equal "No enqueued job found with {:job=>HelloJob, :args=>[#{wilma.inspect}]}", error.message
end

def test_assert_enqueued_with_with_failure_with_no_block_with_global_id_args
ricardo = Person.new(9)
wilma = Person.new(11)
error = assert_raise ActiveSupport::TestCase::Assertion do
HelloJob.perform_later(ricardo)
assert_enqueued_with(job: HelloJob, args: [wilma])
end

assert_equal "No enqueued job found with {:job=>HelloJob, :args=>[#{wilma.inspect}]}", error.message
end

def test_assert_enqueued_job_does_not_change_jobs_count
HelloJob.perform_later
assert_enqueued_with(job: HelloJob) do
Expand All @@ -463,6 +520,14 @@ def test_assert_enqueued_job_does_not_change_jobs_count

assert_equal 2, queue_adapter.enqueued_jobs.count
end

def test_assert_enqueued_with_with_no_block_does_not_change_jobs_count
HelloJob.perform_later
HelloJob.perform_later
assert_enqueued_with(job: HelloJob)

assert_equal 2, queue_adapter.enqueued_jobs.count
end
end

class PerformedJobsTest < ActiveJob::TestCase
Expand Down

0 comments on commit 4382fcb

Please sign in to comment.