Class: ActiveJob::TestCase
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
::ActiveSupport::TestCase,
::ActiveSupport::DescendantsTracker,
::ActiveSupport::Testing::Declarative,
Minitest::Test
|
|
Instance Chain:
|
|
Inherits: |
ActiveSupport::TestCase
|
Defined in: | activejob/lib/active_job/test_case.rb |
Constant Summary
::ActiveSupport::Callbacks - Inherited
::ActiveSupport::TestCase - Inherited
TestHelper - Attributes & Methods
- #after_teardown
-
#assert_enqueued_jobs(number)
Asserts that the number of enqueued jobs matches the given number.
-
#assert_enqueued_with(args = {}, &_block)
Asserts that the job passed in the block has been enqueued with the given arguments.
-
#assert_no_enqueued_jobs(&block)
Asserts that no jobs have been enqueued.
-
#assert_no_performed_jobs(&block)
Asserts that no jobs have been performed.
-
#assert_performed_jobs(number)
Asserts that the number of performed jobs matches the given number.
-
#assert_performed_with(args = {}, &_block)
Asserts that the job passed in the block has been performed with the given arguments.
- #before_setup
- #enqueued_jobs rw
- #perform_enqueued_jobs
- #performed_jobs rw
- #queue_adapter
Class Attribute Summary
::ActiveSupport::TestCase - Inherited
.config, .config?, .fixture_class_names, .fixture_class_names?, .fixture_path, .fixture_path?, .fixture_table_names, .fixture_table_names?, .pre_loaded_fixtures, .pre_loaded_fixtures?, | |
.test_order | Returns the order in which test cases are run. |
.test_order= | Sets the order in which test cases are run. |
.use_instantiated_fixtures, .use_instantiated_fixtures?, .use_transactional_fixtures, .use_transactional_fixtures? |
Class Method Summary
::ActiveSupport::TestCase - Inherited
::ActiveSupport::DescendantsTracker - Inherited
clear, descendants, direct_descendants, | |
store_inherited | This is the only method that is not thread safe, but is only ever called during the eager loading phase. |
::ActiveSupport::Testing::Declarative - Extended
Instance Attribute Summary
::ActiveSupport::TestCase - Inherited
::ActiveRecord::TestFixtures - Included
Instance Method Summary
::ActiveSupport::TestCase - Inherited
#assert_no_match, #assert_not_empty, #assert_not_equal, #assert_not_in_delta, #assert_not_in_epsilon, #assert_not_includes, #assert_not_instance_of, #assert_not_kind_of, #assert_not_nil, #assert_not_operator, #assert_not_predicate, #assert_not_respond_to, #assert_not_same, | |
#assert_nothing_raised | Fails if the block raises an exception. |
#assert_raise | test/unit backwards compatibility methods. |
#method_name |
::ActiveRecord::TestFixtures - Included
::ActiveSupport::Testing::TimeHelpers - Included
#travel | Changes current time to the time in the future or in the past by a given time difference by stubbing |
#travel_back | Returns the current time back to its original state, by removing the stubs added by |
#travel_to | Changes current time to the given time by stubbing |
::ActiveSupport::Testing::Assertions - Included
#assert_difference | Test numeric difference between the return value of an expression as a result of what is evaluated in the yielded block. |
#assert_no_difference | Assertion that the numeric result of evaluating an expression is not changed before and after invoking the passed in block. |
#assert_not | Assert that an expression is not truthy. |
::ActiveSupport::Callbacks - Inherited
#run_callbacks | Runs the callbacks for the given event. |
Instance Attribute Details
#enqueued_jobs (rw)
[ GitHub ]# File 'activejob/lib/active_job/test_helper.rb', line 208
delegate :enqueued_jobs, :enqueued_jobs=, :performed_jobs, :performed_jobs=, to: :queue_adapter
#performed_jobs (rw)
[ GitHub ]# File 'activejob/lib/active_job/test_helper.rb', line 208
delegate :enqueued_jobs, :enqueued_jobs=, :performed_jobs, :performed_jobs=, to: :queue_adapter
Instance Method Details
#after_teardown
[ GitHub ]# File 'activejob/lib/active_job/test_helper.rb', line 17
def after_teardown super ActiveJob::Base.queue_adapter = @old_queue_adapter end
#assert_enqueued_jobs(number)
Asserts that the number of enqueued jobs matches the given number.
def test_jobs
assert_enqueued_jobs 0
HelloJob.perform_later('david')
assert_enqueued_jobs 1
HelloJob.perform_later('abdelkader')
assert_enqueued_jobs 2
end
If a block is passed, that block should cause the specified number of jobs to be enqueued.
def test_jobs_again
assert_enqueued_jobs 1 do
HelloJob.perform_later('cristian')
end
assert_enqueued_jobs 2 do
HelloJob.perform_later('aaron')
HelloJob.perform_later('rafael')
end
end
# File 'activejob/lib/active_job/test_helper.rb', line 45
def assert_enqueued_jobs(number) if block_given? original_count = enqueued_jobs.size yield new_count = enqueued_jobs.size assert_equal number, new_count - original_count, "#{number} jobs expected, but #{new_count - original_count} were enqueued" else enqueued_jobs_size = enqueued_jobs.size assert_equal number, enqueued_jobs_size, "#{number} jobs expected, but #{enqueued_jobs_size} were enqueued" end end
#assert_enqueued_with(args = {}, &_block)
Asserts that the job passed in the block has been enqueued with the given arguments.
def test_assert_enqueued_with
assert_enqueued_with(job: MyJob, args: [1,2,3], queue: 'low') do
MyJob.perform_later(1,2,3)
end
end
# File 'activejob/lib/active_job/test_helper.rb', line 158
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| serialized_args.all? { |key, value| value == job[key] } end assert matching_job, "No enqueued job found with #{args}" ensure queue_adapter.enqueued_jobs = original_enqueued_jobs + enqueued_jobs end
#assert_no_enqueued_jobs(&block)
Asserts that no jobs have been enqueued.
def test_jobs
assert_no_enqueued_jobs
HelloJob.perform_later('jeremy')
assert_enqueued_jobs 1
end
If a block is passed, that block should not cause any job to be enqueued.
def test_jobs_again
assert_no_enqueued_jobs do
# No job should be enqueued from this block
end
end
Note: This assertion is simply a shortcut for:
assert_enqueued_jobs 0, &block
# File 'activejob/lib/active_job/test_helper.rb', line 77
def assert_no_enqueued_jobs(&block) assert_enqueued_jobs 0, &block end
#assert_no_performed_jobs(&block)
Asserts that no jobs have been performed.
def test_jobs
assert_no_performed_jobs
perform_enqueued_jobs do
HelloJob.perform_later('matthew')
assert_performed_jobs 1
end
end
If a block is passed, that block should not cause any job to be performed.
def test_jobs_again
assert_no_performed_jobs do
# No job should be performed from this block
end
end
Note: This assertion is simply a shortcut for:
assert_performed_jobs 0, &block
# File 'activejob/lib/active_job/test_helper.rb', line 147
def assert_no_performed_jobs(&block) assert_performed_jobs 0, &block end
#assert_performed_jobs(number)
Asserts that the number of performed jobs matches the given number. If no block is passed, #perform_enqueued_jobs must be called around the job call.
def test_jobs
assert_performed_jobs 0
perform_enqueued_jobs do
HelloJob.perform_later('xavier')
end
assert_performed_jobs 1
perform_enqueued_jobs do
HelloJob.perform_later('yves')
assert_performed_jobs 2
end
end
If a block is passed, that block should cause the specified number of jobs to be performed.
def test_jobs_again
assert_performed_jobs 1 do
HelloJob.perform_later('robin')
end
assert_performed_jobs 2 do
HelloJob.perform_later('carlos')
HelloJob.perform_later('sean')
end
end
# File 'activejob/lib/active_job/test_helper.rb', line 112
def assert_performed_jobs(number) if block_given? original_count = performed_jobs.size perform_enqueued_jobs { yield } new_count = performed_jobs.size assert_equal number, new_count - original_count, "#{number} jobs expected, but #{new_count - original_count} were performed" else performed_jobs_size = performed_jobs.size assert_equal number, performed_jobs_size, "#{number} jobs expected, but #{performed_jobs_size} were performed" end end
#assert_performed_with(args = {}, &_block)
Asserts that the job passed in the block has been performed with the given arguments.
def test_assert_performed_with
assert_performed_with(job: MyJob, args: [1,2,3], queue: 'high') do
MyJob.perform_later(1,2,3)
end
end
# File 'activejob/lib/active_job/test_helper.rb', line 179
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| serialized_args.all? { |key, value| value == job[key] } end assert matching_job, "No performed job found with #{args}" ensure queue_adapter.performed_jobs = original_performed_jobs + performed_jobs end
#before_setup
[ GitHub ]# File 'activejob/lib/active_job/test_helper.rb', line 9
def before_setup @old_queue_adapter = queue_adapter ActiveJob::Base.queue_adapter = :test clear_enqueued_jobs clear_performed_jobs super end
#perform_enqueued_jobs
[ GitHub ]# File 'activejob/lib/active_job/test_helper.rb', line 193
def perform_enqueued_jobs @old_perform_enqueued_jobs = queue_adapter.perform_enqueued_jobs @old_perform_enqueued_at_jobs = queue_adapter.perform_enqueued_at_jobs queue_adapter.perform_enqueued_jobs = true queue_adapter.perform_enqueued_at_jobs = true yield ensure queue_adapter.perform_enqueued_jobs = @old_perform_enqueued_jobs queue_adapter.perform_enqueued_at_jobs = @old_perform_enqueued_at_jobs end