Skip to content

Commit e9075d5

Browse files
committed
Add back the support to pass at as a proc in the job assertions
1 parent 1853429 commit e9075d5

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

activejob/lib/active_job/test_helper.rb

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -351,9 +351,21 @@ def assert_no_performed_jobs(only: nil, except: nil, queue: nil, &block)
351351
# assert_enqueued_with(job: MyJob, at: Date.tomorrow.noon)
352352
# end
353353
#
354+
# The +at+ and +args+ arguments also accept a proc.
354355
#
355-
# The +args+ argument also accepts a proc which will get passed the actual
356-
# job's arguments. Your proc needs to return a boolean value determining if
356+
# To the +at+ proc, it will get passed the actual job's at argument.
357+
#
358+
# def test_assert_enqueued_with
359+
# expected_time = ->(at) do
360+
# (Date.yesterday..Date.tomorrow).cover?(at)
361+
# end
362+
#
363+
# MyJob.set(at: Date.today.noon).perform_later
364+
# assert_enqueued_with(job: MyJob, at: expected_time)
365+
# end
366+
#
367+
# To the +args+ proc, it will get passed the actual job's arguments
368+
# Your proc needs to return a boolean value determining if
357369
# the job's arguments matches your expectation. This is useful to check only
358370
# for a subset of arguments.
359371
#
@@ -366,7 +378,6 @@ def assert_no_performed_jobs(only: nil, except: nil, queue: nil, &block)
366378
# assert_enqueued_with(job: MyJob, args: expected_args, queue: 'low')
367379
# end
368380
#
369-
#
370381
# If a block is passed, asserts that the block will cause the job to be
371382
# enqueued with the given arguments.
372383
#
@@ -425,8 +436,21 @@ def assert_enqueued_with(job: nil, args: nil, at: nil, queue: nil)
425436
# assert_performed_with(job: MyJob, at: Date.tomorrow.noon)
426437
# end
427438
#
428-
# The +args+ argument also accepts a proc which will get passed the actual
429-
# job's arguments. Your proc needs to return a boolean value determining if
439+
# The +at+ and +args+ arguments also accept a proc.
440+
#
441+
# To the +at+ proc, it will get passed the actual job's at argument.
442+
#
443+
# def test_assert_enqueued_with
444+
# expected_time = ->(at) do
445+
# (Date.yesterday..Date.tomorrow).cover?(at)
446+
# end
447+
#
448+
# MyJob.set(at: Date.today.noon).perform_later
449+
# assert_enqueued_with(job: MyJob, at: expected_time)
450+
# end
451+
#
452+
# To the +args+ proc, it will get passed the actual job's arguments
453+
# Your proc needs to return a boolean value determining if
430454
# the job's arguments matches your expectation. This is useful to check only
431455
# for a subset of arguments.
432456
#
@@ -630,7 +654,7 @@ def flush_enqueued_jobs(only: nil, except: nil, queue: nil)
630654

631655
def prepare_args_for_assertion(args)
632656
args.dup.tap do |arguments|
633-
if arguments[:at]
657+
if arguments[:at] && !arguments[:at].respond_to?(:call)
634658
at_range = arguments[:at] - 1..arguments[:at] + 1
635659
arguments[:at] = ->(at) { at_range.cover?(at) }
636660
end

activejob/test/cases/test_helper_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,6 +1704,18 @@ def test_assert_performed_with_with_relative_at_option
17041704
end
17051705
end
17061706

1707+
def test_assert_performed_with_with_at_option_as_a_proc
1708+
assert_performed_with(job: HelloJob, at: ->(at) { (4.minutes.from_now..6.minutes.from_now).cover?(at) }) do
1709+
HelloJob.set(wait: 5.minutes).perform_later
1710+
end
1711+
1712+
assert_raise ActiveSupport::TestCase::Assertion do
1713+
assert_performed_with(job: HelloJob, at: ->(at) { (1.minute.from_now..3.minutes.from_now).cover?(at) }) do
1714+
HelloJob.set(wait: 1.minute).perform_later
1715+
end
1716+
end
1717+
end
1718+
17071719
def test_assert_performed_with_without_block_with_at_option
17081720
HelloJob.set(wait_until: Date.tomorrow.noon).perform_later
17091721

0 commit comments

Comments
 (0)