Skip to content

Commit

Permalink
Add back the support to pass at as a proc in the job assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelfranca committed May 1, 2020
1 parent 348e142 commit 684db54
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
36 changes: 30 additions & 6 deletions activejob/lib/active_job/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,21 @@ def assert_no_performed_jobs(only: nil, except: nil, queue: nil, &block)
# assert_enqueued_with(job: MyJob, at: Date.tomorrow.noon)
# end
#
# The +at+ and +args+ arguments also accept a proc.
#
# The +args+ argument also accepts a proc which will get passed the actual
# job's arguments. Your proc needs to return a boolean value determining if
# To the +at+ proc, it will get passed the actual job's at argument.
#
# def test_assert_enqueued_with
# expected_time = ->(at) do
# (Date.yesterday..Date.tomorrow).cover?(at)
# end
#
# MyJob.set(at: Date.today.noon).perform_later
# assert_enqueued_with(job: MyJob, at: expected_time)
# end
#
# To the +args+ proc, it will get passed the actual job's arguments
# Your proc needs to return a boolean value determining if
# the job's arguments matches your expectation. This is useful to check only
# for a subset of arguments.
#
Expand All @@ -366,7 +378,6 @@ def assert_no_performed_jobs(only: nil, except: nil, queue: nil, &block)
# assert_enqueued_with(job: MyJob, args: expected_args, queue: 'low')
# end
#
#
# If a block is passed, asserts that the block will cause the job to be
# enqueued with the given arguments.
#
Expand Down Expand Up @@ -429,8 +440,21 @@ def assert_enqueued_with(job: nil, args: nil, at: nil, queue: nil, &block)
# assert_performed_with(job: MyJob, at: Date.tomorrow.noon)
# end
#
# The +args+ argument also accepts a proc which will get passed the actual
# job's arguments. Your proc needs to return a boolean value determining if
# The +at+ and +args+ arguments also accept a proc.
#
# To the +at+ proc, it will get passed the actual job's at argument.
#
# def test_assert_enqueued_with
# expected_time = ->(at) do
# (Date.yesterday..Date.tomorrow).cover?(at)
# end
#
# MyJob.set(at: Date.today.noon).perform_later
# assert_enqueued_with(job: MyJob, at: expected_time)
# end
#
# To the +args+ proc, it will get passed the actual job's arguments
# Your proc needs to return a boolean value determining if
# the job's arguments matches your expectation. This is useful to check only
# for a subset of arguments.
#
Expand Down Expand Up @@ -649,7 +673,7 @@ def flush_enqueued_jobs(only: nil, except: nil, queue: nil, at: nil)

def prepare_args_for_assertion(args)
args.dup.tap do |arguments|
if arguments[:at]
if arguments[:at] && !arguments[:at].respond_to?(:call)
at_range = arguments[:at] - 1..arguments[:at] + 1
arguments[:at] = ->(at) { at_range.cover?(at) }
end
Expand Down
12 changes: 12 additions & 0 deletions activejob/test/cases/test_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1804,6 +1804,18 @@ def test_assert_performed_with_with_relative_at_option
end
end

def test_assert_performed_with_with_at_option_as_a_proc
assert_performed_with(job: HelloJob, at: ->(at) { (4.minutes.from_now..6.minutes.from_now).cover?(at) }) do
HelloJob.set(wait: 5.minutes).perform_later
end

assert_raise ActiveSupport::TestCase::Assertion do
assert_performed_with(job: HelloJob, at: ->(at) { (1.minute.from_now..3.minutes.from_now).cover?(at) }) do
HelloJob.set(wait: 1.minute).perform_later
end
end
end

def test_assert_performed_with_without_block_with_at_option
HelloJob.set(wait_until: Date.tomorrow.noon).perform_later

Expand Down

0 comments on commit 684db54

Please sign in to comment.