module ActiveJob::TestHelper
def after_teardown # :nodoc:
def after_teardown # :nodoc: super queue_adapter_changed_jobs.each { |klass| klass.disable_test_adapter } end
def assert_enqueued_jobs(number, only: nil, except: nil, queue: nil)
end
HelloJob.perform_later('elfassy')
LoggingJob.perform_later
assert_enqueued_jobs 2, queue: 'default' do
def test_logging_job
Asserts the number of times a job is enqueued to a specific queue by passing +:queue+ option.
a hash containing the job's class and it's argument are passed as argument.
+:only+ and +:except+ options accepts Class, Array of Class or Proc. When passed a Proc,
end
end
HelloJob.perform_later('jeremy')
LoggingJob.perform_later
assert_enqueued_jobs 1, except: HelloJob do
def test_logging_job
Asserts the number of times a job except specific class was enqueued by passing +:except+ option.
end
end
HelloJob.perform_later('jeremy')
LoggingJob.perform_later
assert_enqueued_jobs 1, only: LoggingJob do
def test_logging_job
Asserts the number of times a specific job was enqueued by passing +:only+ option.
end
end
HelloJob.perform_later('rafael')
HelloJob.perform_later('aaron')
assert_enqueued_jobs 2 do
end
HelloJob.perform_later('cristian')
assert_enqueued_jobs 1 do
def test_jobs_again
jobs to be enqueued.
If a block is passed, asserts that the block will cause the specified number of
end
assert_enqueued_jobs 2
HelloJob.perform_later('abdelkader')
assert_enqueued_jobs 1
HelloJob.perform_later('david')
assert_enqueued_jobs 0
def test_jobs
Asserts that the number of enqueued jobs matches the given number.
def assert_enqueued_jobs(number, only: nil, except: nil, queue: nil) if block_given? original_count = enqueued_jobs_with(only: only, except: except, queue: queue) yield new_count = enqueued_jobs_with(only: only, except: except, queue: queue) actual_count = new_count - original_count else actual_count = enqueued_jobs_with(only: only, except: except, queue: queue) end assert_equal number, actual_count, "#{number} jobs expected, but #{actual_count} were enqueued" end
def assert_enqueued_with(job: nil, args: nil, at: nil, queue: nil)
end
MyJob.set(wait_until: Date.tomorrow.noon).perform_later
assert_enqueued_with(job: MyJob, at: Date.tomorrow.noon) do
end
MyJob.perform_later(1,2,3)
assert_enqueued_with(job: MyJob, args: [1,2,3], queue: 'low') do
def test_assert_enqueued_with
enqueued with the given arguments.
If a block is passed, asserts that the block will cause the job to be
end
assert_enqueued_with(job: MyJob, args: expected_args, queue: 'low')
MyJob.perform_later(foo: 'bar', other_arg: 'No need to check in the test')
end
assert job_args.first.key?(:foo)
expected_args = ->(job_args) do
def test_assert_enqueued_with
for a subset of arguments.
the job's arguments matches your expectation. This is useful to check only
job's arguments. Your proc needs to return a boolean value determining if
The +args+ argument also accepts a proc which will get passed the actual
end
assert_enqueued_with(job: MyJob, at: Date.tomorrow.noon)
MyJob.set(wait_until: Date.tomorrow.noon).perform_later
assert_enqueued_with(job: MyJob, args: [1,2,3], queue: 'low')
MyJob.perform_later(1,2,3)
def test_assert_enqueued_with
Asserts that the job has been enqueued with the given arguments.
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? do |key, value| if value.respond_to?(:call) value.call(deserialized_job[key]) else value == deserialized_job[key] end end end assert matching_job, "No enqueued job found with #{expected}" instantiate_job(matching_job) end
def assert_no_enqueued_jobs(only: nil, except: nil, queue: nil, &block)
Note: This assertion is simply a shortcut for:
end
end
LoggingJob.set(queue: :some_queue).perform_later
assert_no_enqueued_jobs queue: 'default' do
def test_no_logging
Asserts that no jobs are enqueued to a specific queue by passing +:queue+ option
a hash containing the job's class and it's argument are passed as argument.
+:only+ and +:except+ options accepts Class, Array of Class or Proc. When passed a Proc,
end
end
HelloJob.perform_later('jeremy')
assert_no_enqueued_jobs except: HelloJob do
def test_no_logging
Asserts that no jobs except specific class are enqueued by passing +:except+ option.
end
end
HelloJob.perform_later('jeremy')
assert_no_enqueued_jobs only: LoggingJob do
def test_no_logging
Asserts that no jobs of a specific kind are enqueued by passing +:only+ option.
end
end
# No job should be enqueued from this block
assert_no_enqueued_jobs do
def test_jobs_again
If a block is passed, asserts that the block will not cause any job to be enqueued.
end
assert_enqueued_jobs 1
HelloJob.perform_later('jeremy')
assert_no_enqueued_jobs
def test_jobs
Asserts that no jobs have been enqueued.
def assert_no_enqueued_jobs(only: nil, except: nil, queue: nil, &block) assert_enqueued_jobs 0, only: only, except: except, queue: queue, &block end
def assert_no_performed_jobs(only: nil, except: nil, queue: nil, &block)
Note: This assertion is simply a shortcut for:
end
end
HelloJob.set(queue: :other_queue).perform_later("jeremy")
assert_no_performed_jobs queue: :some_queue do
def test_assert_no_performed_jobs_with_queue_option
then only the job(s) enqueued to a specific queue will not be performed.
If the +:queue+ option is specified,
an instance of the job will be passed as argument.
+:only+ and +:except+ options accepts Class, Array of Class or Proc. When passed a Proc,
end
end
HelloJob.perform_later('jeremy')
assert_no_performed_jobs except: HelloJob do
def test_no_logging
then the job(s) except specific class will not be performed.
Also if the +:except+ option is specified,
end
end
HelloJob.perform_later('jeremy')
assert_no_performed_jobs only: LoggingJob do
def test_no_logging
then only the listed job(s) will not be performed.
The block form supports filtering. If the +:only+ option is specified,
end
end
# No job should be performed from this block
assert_no_performed_jobs do
def test_jobs_again
If a block is passed, asserts that the block will not cause any job to be performed.
end
end
assert_performed_jobs 1
HelloJob.perform_later('matthew')
perform_enqueued_jobs do
assert_no_performed_jobs
def test_jobs
Asserts that no jobs have been performed.
def assert_no_performed_jobs(only: nil, except: nil, queue: nil, &block) assert_performed_jobs 0, only: only, except: except, queue: queue, &block end
def assert_performed_jobs(number, only: nil, except: nil, queue: nil, &block)
end
HelloJob.set(queue: :other_queue).perform_later("bogdan")
HelloJob.set(queue: :some_queue).perform_later("jeremy")
assert_performed_jobs 1, queue: :some_queue do
def test_assert_performed_jobs_with_queue_option
then only the job(s) enqueued to a specific queue will be performed.
If the +:queue+ option is specified,
end
end
end
RescueJob.perform_later('david')
LoggingJob.perform_later('stewie')
HelloJob.perform_later('jeremy')
assert_performed_jobs(1, only: ->(job) { job.is_a?(HelloJob) }) do
assert_nothing_raised do
def test_hello_and_logging_jobs
A proc may also be specified. When passed a Proc, the job's instance will be passed as argument.
end
end
end
RescueJob.perform_later('david')
LoggingJob.perform_later('stewie')
HelloJob.perform_later('jeremy')
assert_performed_jobs 2, only: [HelloJob, LoggingJob] do
assert_nothing_raised do
def test_hello_and_logging_jobs
An array may also be specified, to support testing multiple jobs.
end
end
LoggingJob.perform_later
HelloJob.perform_later('jeremy')
assert_performed_jobs 1, except: LoggingJob do
def test_hello_job
then the job(s) except specific class will be performed.
Also if the +:except+ option is specified,
end
end
LoggingJob.perform_later
HelloJob.perform_later('jeremy')
assert_performed_jobs 1, only: HelloJob do
def test_hello_job
then only the listed job(s) will be performed.
This method also supports filtering. If the +:only+ option is specified,
end
end
HelloJob.perform_later('sean')
HelloJob.perform_later('carlos')
assert_performed_jobs 2 do
end
HelloJob.perform_later('robin')
assert_performed_jobs 1 do
def test_jobs_again
jobs to be performed.
If a block is passed, asserts that the block will cause the specified number of
end
assert_performed_jobs 2
perform_enqueued_jobs
HelloJob.perform_later('yves')
assert_performed_jobs 1
end
HelloJob.perform_later('xavier')
perform_enqueued_jobs do
assert_performed_jobs 0
def test_jobs
must be called around or after the job call.
If no block is passed, perform_enqueued_jobs
Asserts that the number of performed jobs matches the given number.
def assert_performed_jobs(number, only: nil, except: nil, queue: nil, &block) if block_given? original_count = performed_jobs.size perform_enqueued_jobs(only: only, except: except, queue: queue, &block) new_count = performed_jobs.size performed_jobs_size = new_count - original_count else performed_jobs_size = performed_jobs_with(only: only, except: except, queue: queue) end assert_equal number, performed_jobs_size, "#{number} jobs expected, but #{performed_jobs_size} were performed" end
def assert_performed_with(job: nil, args: nil, at: nil, queue: nil, &block)
end
MyJob.set(wait_until: Date.tomorrow.noon).perform_later
assert_performed_with(job: MyJob, at: Date.tomorrow.noon) do
end
MyJob.perform_later(1,2,3)
assert_performed_with(job: MyJob, args: [1,2,3], queue: 'high') do
def test_assert_performed_with
the job has been performed with the given arguments in the block.
enqueued throughout the duration of the block and asserts that
If a block is passed, that block performs all of the jobs that were
end
assert_performed_with(job: MyJob, args: expected_args, queue: 'high')
perform_enqueued_jobs
MyJob.perform_later(foo: 'bar', other_arg: 'No need to check in the test')
end
assert job_args.first.key?(:foo)
expected_args = ->(job_args) do
def test_assert_performed_with
for a subset of arguments.
the job's arguments matches your expectation. This is useful to check only
job's arguments. Your proc needs to return a boolean value determining if
The +args+ argument also accepts a proc which will get passed the actual
end
assert_performed_with(job: MyJob, at: Date.tomorrow.noon)
perform_enqueued_jobs
MyJob.set(wait_until: Date.tomorrow.noon).perform_later
assert_performed_with(job: MyJob, args: [1,2,3], queue: 'high')
perform_enqueued_jobs
MyJob.perform_later(1,2,3)
def test_assert_performed_with
Asserts that the job has been performed with the given arguments.
def assert_performed_with(job: nil, args: nil, at: nil, queue: nil, &block) expected = { job: job, args: args, at: at, queue: queue }.compact expected_args = prepare_args_for_assertion(expected) if block_given? original_performed_jobs_count = performed_jobs.count perform_enqueued_jobs(&block) jobs = performed_jobs.drop(original_performed_jobs_count) else jobs = performed_jobs end matching_job = jobs.find do |enqueued_job| deserialized_job = deserialize_args_for_assertion(enqueued_job) expected_args.all? do |key, value| if value.respond_to?(:call) value.call(deserialized_job[key]) else value == deserialized_job[key] end end end assert matching_job, "No performed job found with #{expected}" instantiate_job(matching_job) end
def before_setup # :nodoc:
def before_setup # :nodoc: test_adapter = queue_adapter_for_test queue_adapter_changed_jobs.each do |klass| klass.enable_test_adapter(test_adapter) end clear_enqueued_jobs clear_performed_jobs super end
def clear_enqueued_jobs
def clear_enqueued_jobs enqueued_jobs.clear end
def clear_performed_jobs
def clear_performed_jobs performed_jobs.clear end
def deserialize_args_for_assertion(job)
def deserialize_args_for_assertion(job) job.dup.tap do |new_job| new_job[:args] = ActiveJob::Arguments.deserialize(new_job[:args]) if new_job[:args] end end
def enqueued_jobs_with(only: nil, except: nil, queue: nil, &block)
def enqueued_jobs_with(only: nil, except: nil, queue: nil, &block) jobs_with(enqueued_jobs, only: only, except: except, queue: queue, &block) end
def filter_as_proc(filter)
def filter_as_proc(filter) return filter if filter.is_a?(Proc) ->(job) { Array(filter).include?(job.fetch(:job)) } end
def flush_enqueued_jobs(only: nil, except: nil, queue: nil)
def flush_enqueued_jobs(only: nil, except: nil, queue: nil) enqueued_jobs_with(only: only, except: except, queue: queue) do |payload| instantiate_job(payload).perform_now queue_adapter.performed_jobs << payload end end
def instantiate_job(payload)
def instantiate_job(payload) args = ActiveJob::Arguments.deserialize(payload[:args]) job = payload[:job].new(*args) job.scheduled_at = Time.at(payload[:at]) if payload.key?(:at) job.queue_name = payload[:queue] job end
def jobs_with(jobs, only: nil, except: nil, queue: nil)
def jobs_with(jobs, only: nil, except: nil, queue: nil) validate_option(only: only, except: except) jobs.count do |job| job_class = job.fetch(:job) if only next false unless filter_as_proc(only).call(job) elsif except next false if filter_as_proc(except).call(job) end if queue next false unless queue.to_s == job.fetch(:queue, job_class.queue_name) end yield job if block_given? true end end
def perform_enqueued_jobs(only: nil, except: nil, queue: nil)
end
assert_performed_jobs 1
end
HelloJob.set(queue: :other_queue).perform_later(1, 2, 3) # will not be performed
MyJob.set(queue: :some_queue).perform_later(1, 2, 3) # will be performed
perform_enqueued_jobs queue: :some_queue do
def test_perform_enqueued_jobs_with_queue
then only the job(s) enqueued to a specific queue will be performed.
If the +:queue+ option is specified,
an instance of the job will be passed as argument.
+:only+ and +:except+ options accepts Class, Array of Class or Proc. When passed a Proc,
end
assert_performed_jobs 1
end
HelloJob.perform_later(1, 2, 3) # will not be performed
MyJob.perform_later(1, 2, 3) # will be performed
perform_enqueued_jobs(except: HelloJob) do
def test_perform_enqueued_jobs_with_except
then the job(s) except specific class will be performed.
Also if the +:except+ option is specified,
end
assert_performed_jobs 1
end
HelloJob.perform_later(1, 2, 3) # will not be performed
MyJob.perform_later(1, 2, 3) # will be performed
perform_enqueued_jobs(only: MyJob) do
def test_perform_enqueued_jobs_with_only
then only the listed job(s) will be performed.
This method also supports filtering. If the +:only+ option is specified,
end
assert_performed_jobs 1
perform_enqueued_jobs
MyJob.perform_later(1, 2, 3)
def test_perform_enqueued_jobs_without_block
end
assert_performed_jobs 1
end
MyJob.perform_later(1, 2, 3)
perform_enqueued_jobs do
def test_perform_enqueued_jobs
not given, performs all of the enqueued jobs up to this point in the test.
that were enqueued throughout the duration of the block. If a block is
Performs all enqueued jobs. If a block is given, performs all of the jobs
def perform_enqueued_jobs(only: nil, except: nil, queue: nil) return flush_enqueued_jobs(only: only, except: except, queue: queue) unless block_given? validate_option(only: only, except: except) old_perform_enqueued_jobs = queue_adapter.perform_enqueued_jobs old_perform_enqueued_at_jobs = queue_adapter.perform_enqueued_at_jobs old_filter = queue_adapter.filter old_reject = queue_adapter.reject old_queue = queue_adapter.queue begin queue_adapter.perform_enqueued_jobs = true queue_adapter.perform_enqueued_at_jobs = true queue_adapter.filter = only queue_adapter.reject = except queue_adapter.queue = queue yield ensure queue_adapter.perform_enqueued_jobs = old_perform_enqueued_jobs queue_adapter.perform_enqueued_at_jobs = old_perform_enqueued_at_jobs queue_adapter.filter = old_filter queue_adapter.reject = old_reject queue_adapter.queue = old_queue end end
def performed_jobs_with(only: nil, except: nil, queue: nil, &block)
def performed_jobs_with(only: nil, except: nil, queue: nil, &block) jobs_with(performed_jobs, only: only, except: except, queue: queue, &block) end
def prepare_args_for_assertion(args)
def prepare_args_for_assertion(args) args.dup.tap do |arguments| arguments[:at] = arguments[:at].to_f if arguments[:at] arguments[:args] = round_time_arguments(arguments[:args]) if arguments[:args] end end
def queue_adapter
assert_instance_of CustomQueueAdapter, HelloJob.queue_adapter
def test_assert_job_has_custom_queue_adapter_set
Accesses the queue_adapter set by ActiveJob::Base.
def queue_adapter ActiveJob::Base.queue_adapter end
def queue_adapter_changed_jobs
def queue_adapter_changed_jobs (ActiveJob::Base.descendants << ActiveJob::Base).select do |klass| # only override explicitly set adapters, a quirk of `class_attribute` klass.singleton_class.public_instance_methods(false).include?(:_queue_adapter) end end
def queue_adapter_for_test
in order to be used with the active job test helpers. Refer to
methods from those expected of a standard ActiveJob::QueueAdapter
Note: The adapter provided by this method must provide some additional
ActiveJob::QueueAdapters::TestAdapter.
Returns an instance of the queue adapter and defaults to
Specifies the queue adapter to use with all Active Job test helpers.
def queue_adapter_for_test ActiveJob::QueueAdapters::TestAdapter.new end
def round_time_arguments(argument)
def round_time_arguments(argument) case argument when Time, ActiveSupport::TimeWithZone, DateTime argument.change(usec: 0) when Hash argument.transform_values { |value| round_time_arguments(value) } when Array argument.map { |element| round_time_arguments(element) } else argument end end
def validate_option(only: nil, except: nil)
def validate_option(only: nil, except: nil) raise ArgumentError, "Cannot specify both `:only` and `:except` options." if only && except end