aboutsummaryrefslogtreecommitdiffstats
path: root/activejob
diff options
context:
space:
mode:
authorJean Boussier <jean.boussier@gmail.com>2015-07-23 18:45:07 -0400
committerJean Boussier <jean.boussier@gmail.com>2015-08-10 20:14:42 -0400
commitb6d3a478fa22365207637454e0ac5c805a67af06 (patch)
treee0800b4538d76415ab52fe637d7f06ad6d8cb02f /activejob
parent628a23cdd230f7f830ecbba137ab2430f69e8db5 (diff)
downloadrails-b6d3a478fa22365207637454e0ac5c805a67af06.tar.gz
rails-b6d3a478fa22365207637454e0ac5c805a67af06.tar.bz2
rails-b6d3a478fa22365207637454e0ac5c805a67af06.zip
Make assert_enqueued_with and assert_performed_with returns the matched job
Diffstat (limited to 'activejob')
-rw-r--r--activejob/CHANGELOG.md5
-rw-r--r--activejob/lib/active_job/test_helper.rb13
-rw-r--r--activejob/test/cases/test_helper_test.rb22
3 files changed, 38 insertions, 2 deletions
diff --git a/activejob/CHANGELOG.md b/activejob/CHANGELOG.md
index 1bd97eed38..d8944a5338 100644
--- a/activejob/CHANGELOG.md
+++ b/activejob/CHANGELOG.md
@@ -1,3 +1,8 @@
+* `assert_enqueued_with` and `assert_performed_with` now returns the matched
+ job instance for further assertions.
+
+ *Jean Boussier*
+
* Include I18n.locale into job serialization/deserialization and use it around
`perform`.
diff --git a/activejob/lib/active_job/test_helper.rb b/activejob/lib/active_job/test_helper.rb
index e71a3b4f85..74a12884ff 100644
--- a/activejob/lib/active_job/test_helper.rb
+++ b/activejob/lib/active_job/test_helper.rb
@@ -233,10 +233,11 @@ module ActiveJob
args.assert_valid_keys(:job, :args, :at, :queue)
serialized_args = serialize_args_for_assertion(args)
yield
- matching_job = enqueued_jobs.any? do |job|
+ matching_job = enqueued_jobs.find do |job|
serialized_args.all? { |key, value| value == job[key] }
end
assert matching_job, "No enqueued job found with #{args}"
+ instanciate_job(matching_job)
ensure
queue_adapter.enqueued_jobs = original_enqueued_jobs + enqueued_jobs
end
@@ -254,10 +255,11 @@ module ActiveJob
args.assert_valid_keys(:job, :args, :at, :queue)
serialized_args = serialize_args_for_assertion(args)
perform_enqueued_jobs { yield }
- matching_job = performed_jobs.any? do |job|
+ matching_job = performed_jobs.find do |job|
serialized_args.all? { |key, value| value == job[key] }
end
assert matching_job, "No performed job found with #{args}"
+ instanciate_job(matching_job)
ensure
queue_adapter.performed_jobs = original_performed_jobs + performed_jobs
end
@@ -311,6 +313,13 @@ module ActiveJob
end
serialized_args
end
+
+ def instanciate_job(payload)
+ job = payload[:job].new(*payload[:args])
+ job.scheduled_at = Time.at(payload[:at]) if payload.key?(:at)
+ job.queue_name = payload[:queue]
+ job
+ end
end
end
end
diff --git a/activejob/test/cases/test_helper_test.rb b/activejob/test/cases/test_helper_test.rb
index 04c4c446e2..8c60f037d2 100644
--- a/activejob/test/cases/test_helper_test.rb
+++ b/activejob/test/cases/test_helper_test.rb
@@ -165,6 +165,17 @@ class EnqueuedJobsTest < ActiveJob::TestCase
end
end
+ def test_assert_enqueued_job_returns
+ job = assert_enqueued_with(job: LoggingJob) do
+ LoggingJob.set(wait_until: 5.minutes.from_now).perform_later(1, 2, 3)
+ 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
+ end
+
def test_assert_enqueued_job_failure
assert_raise ActiveSupport::TestCase::Assertion do
assert_enqueued_with(job: LoggingJob, queue: 'default') do
@@ -397,6 +408,17 @@ class PerformedJobsTest < ActiveJob::TestCase
end
end
+ def test_assert_performed_job_returns
+ job = assert_performed_with(job: NestedJob, queue: 'default') do
+ NestedJob.perform_later
+ end
+
+ assert_instance_of NestedJob, job
+ assert_nil job.scheduled_at
+ assert_equal [], job.arguments
+ assert_equal 'default', job.queue_name
+ end
+
def test_assert_performed_job_failure
assert_raise ActiveSupport::TestCase::Assertion do
assert_performed_with(job: LoggingJob, at: Date.tomorrow.noon, queue: 'default') do