Skip to content

Commit

Permalink
Merge pull request #18654 from liseki/fix-aj-test-helper
Browse files Browse the repository at this point in the history
Fix ActiveJob assertions with a GlobalID object argument
  • Loading branch information
rafaelfranca committed Jan 28, 2015
2 parents 56a3d5e + 9d3042d commit dae0eb1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
14 changes: 12 additions & 2 deletions activejob/lib/active_job/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,10 @@ def assert_enqueued_with(args = {}, &_block)
original_enqueued_jobs = enqueued_jobs.dup
clear_enqueued_jobs
args.assert_valid_keys(:job, :args, :at, :queue)
serialized_args = serialize_args_for_assertion(args)
yield
matching_job = enqueued_jobs.any? do |job|
args.all? { |key, value| value == job[key] }
serialized_args.all? { |key, value| value == job[key] }
end
assert matching_job, "No enqueued job found with #{args}"
ensure
Expand All @@ -195,9 +196,10 @@ def assert_performed_with(args = {}, &_block)
original_performed_jobs = performed_jobs.dup
clear_performed_jobs
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|
args.all? { |key, value| value == job[key] }
serialized_args.all? { |key, value| value == job[key] }
end
assert matching_job, "No performed job found with #{args}"
ensure
Expand Down Expand Up @@ -239,6 +241,14 @@ def enqueued_jobs_size(only: nil)
enqueued_jobs.size
end
end

def serialize_args_for_assertion(args)
serialized_args = args.dup
if job_args = serialized_args.delete(:args)
serialized_args[:args] = ActiveJob::Arguments.serialize(job_args)
end
serialized_args
end
end
end
end
39 changes: 39 additions & 0 deletions activejob/test/cases/test_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'jobs/hello_job'
require 'jobs/logging_job'
require 'jobs/nested_job'
require 'models/person'

class EnqueuedJobsTest < ActiveJob::TestCase
def test_assert_enqueued_jobs
Expand Down Expand Up @@ -175,6 +176,25 @@ def test_assert_enqueued_job_args
end
end
end

def test_assert_enqueued_job_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
ricardo = Person.new(9)
wilma = Person.new(11)
error = assert_raise ActiveSupport::TestCase::Assertion do
assert_enqueued_with(job: HelloJob, args: [wilma]) do
HelloJob.perform_later(ricardo)
end
end

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

class PerformedJobsTest < ActiveJob::TestCase
Expand Down Expand Up @@ -282,4 +302,23 @@ def test_assert_performed_job_failure
end
end
end

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

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

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

0 comments on commit dae0eb1

Please sign in to comment.