Skip to content

Commit

Permalink
Added missing specs for not modifying queues when using AJ test helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
morgoth committed Oct 7, 2015
1 parent e70ec9e commit c2854af
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
24 changes: 10 additions & 14 deletions activejob/lib/active_job/test_helper.rb
Expand Up @@ -231,19 +231,17 @@ def assert_no_performed_jobs(only: nil, &block)
# MyJob.set(wait_until: Date.tomorrow.noon).perform_later
# end
# end
def assert_enqueued_with(args = {}, &_block)
original_enqueued_jobs = enqueued_jobs.dup
clear_enqueued_jobs
def assert_enqueued_with(args = {})
original_enqueued_jobs_count = enqueued_jobs.count
args.assert_valid_keys(:job, :args, :at, :queue)
serialized_args = serialize_args_for_assertion(args)
yield
matching_job = enqueued_jobs.find do |job|
in_block_jobs = enqueued_jobs.drop(original_enqueued_jobs_count)
matching_job = in_block_jobs.find do |job|
serialized_args.all? { |key, value| value == job[key] }
end
assert matching_job, "No enqueued job found with #{args}"
instantiate_job(matching_job)
ensure
queue_adapter.enqueued_jobs = original_enqueued_jobs + enqueued_jobs
end

# Asserts that the job passed in the block has been performed with the given arguments.
Expand All @@ -257,19 +255,17 @@ def assert_enqueued_with(args = {}, &_block)
# MyJob.set(wait_until: Date.tomorrow.noon).perform_later
# end
# end
def assert_performed_with(args = {}, &_block)
original_performed_jobs = performed_jobs.dup
clear_performed_jobs
def assert_performed_with(args = {})
original_performed_jobs_count = performed_jobs.count
args.assert_valid_keys(:job, :args, :at, :queue)
serialized_args = serialize_args_for_assertion(args)
perform_enqueued_jobs { yield }
matching_job = performed_jobs.find do |job|
in_block_jobs = performed_jobs.drop(original_performed_jobs_count)
matching_job = in_block_jobs.find do |job|
serialized_args.all? { |key, value| value == job[key] }
end
assert matching_job, "No performed job found with #{args}"
instantiate_job(matching_job)
ensure
queue_adapter.performed_jobs = original_performed_jobs + performed_jobs
end

def perform_enqueued_jobs(only: nil)
Expand Down Expand Up @@ -308,9 +304,9 @@ def clear_performed_jobs # :nodoc:

def enqueued_jobs_size(only: nil) # :nodoc:
if only
enqueued_jobs.select { |job| Array(only).include?(job.fetch(:job)) }.size
enqueued_jobs.count { |job| Array(only).include?(job.fetch(:job)) }
else
enqueued_jobs.size
enqueued_jobs.count
end
end

Expand Down
21 changes: 21 additions & 0 deletions activejob/test/cases/test_helper_test.rb
Expand Up @@ -242,6 +242,15 @@ 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_job_does_not_change_jobs_count
HelloJob.perform_later
assert_enqueued_with(job: HelloJob) do
HelloJob.perform_later
end

assert_equal 2, ActiveJob::Base.queue_adapter.enqueued_jobs.count
end
end

class PerformedJobsTest < ActiveJob::TestCase
Expand Down Expand Up @@ -487,4 +496,16 @@ def test_assert_performed_job_failure_with_global_id_args

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

def test_assert_performed_job_does_not_change_jobs_count
assert_performed_with(job: HelloJob) do
HelloJob.perform_later
end

assert_performed_with(job: HelloJob) do
HelloJob.perform_later
end

assert_equal 2, ActiveJob::Base.queue_adapter.performed_jobs.count
end
end

0 comments on commit c2854af

Please sign in to comment.