Created
September 26, 2018 12:11
-
-
Save akshaymohite/8aeefd044272a9d5eae6b27a0272c782 to your computer and use it in GitHub Desktop.
active-job-enqueued-with-patch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ActiveJobTestHelper | |
def prepare_args_for_assertion(args) | |
args.dup.tap do |arguments| | |
arguments[:at] = arguments[:at].to_f if arguments[:at] | |
end | |
end | |
def assert_enqueued_with(job: nil, args: nil, at: nil, queue: nil) | |
expected = { job: job, args: args, at: at, queue: queue }.compact | |
expected_args = prepare_args_for_assertion(expected) | |
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 |enqueued_job| | |
deserialized_job = deserialize_args_for_assertion(enqueued_job) | |
expected_args.all? { |key, value| value == deserialized_job[key] } | |
end | |
assert matching_job, "No enqueued job found with #{expected}" | |
instantiate_job(matching_job) | |
end | |
end | |
ActiveJob::TestHelper.include ActiveJobTestHelper |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment