Provides helper methods for testing Active Job
- A
- P
- Q
Instance Public methods
assert_enqueued_jobs(number, only: nil, except: nil, queue: nil, &block)Link
Asserts that the number of enqueued jobs matches the given number.
deftest_jobsassert_enqueued_jobs0HelloJob.perform_later('david')assert_enqueued_jobs1HelloJob.perform_later('abdelkader')assert_enqueued_jobs2end
If a block is passed, asserts that the block will cause the specified number of jobs to be enqueued.
deftest_jobs_againassert_enqueued_jobs1doHelloJob.perform_later('cristian')endassert_enqueued_jobs2doHelloJob.perform_later('aaron')HelloJob.perform_later('rafael')endend
Asserts the number of times a specific job was enqueued by passing:only option.
deftest_logging_jobassert_enqueued_jobs1,only:LoggingJobdoLoggingJob.perform_laterHelloJob.perform_later('jeremy')endend
Asserts the number of times a job except specific class was enqueued by passing:except option.
deftest_logging_jobassert_enqueued_jobs1,except:HelloJobdoLoggingJob.perform_laterHelloJob.perform_later('jeremy')endend
:only and:except options acceptClass,Array ofClass, or Proc. When passed a Proc, a hash containing the job’s class and it’s argument are passed as argument.
Asserts the number of times a job is enqueued to a specific queue by passing:queue option.
deftest_logging_jobassert_enqueued_jobs2,queue:'default'doLoggingJob.perform_laterHelloJob.perform_later('elfassy')endend
# File activejob/lib/active_job/test_helper.rb, line 122defassert_enqueued_jobs(number,only:nil,except:nil,queue:nil,&block)require_active_job_test_adapter!("assert_enqueued_jobs")ifblock_given?original_jobs =enqueued_jobs_with(only:only,except:except,queue:queue)_assert_nothing_raised_or_warn("assert_enqueued_jobs",&block)new_jobs =enqueued_jobs_with(only:only,except:except,queue:queue)actual_count = (new_jobs-original_jobs).countelseactual_count =enqueued_jobs_with(only:only,except:except,queue:queue).countendassert_equalnumber,actual_count,"#{number} jobs expected, but #{actual_count} were enqueued"end
assert_enqueued_with(job: nil, args: nil, at: nil, queue: nil, priority: nil, &block)Link
Asserts that the job has been enqueued with the given arguments.
deftest_assert_enqueued_withMyJob.perform_later(1,2,3)assert_enqueued_with(job:MyJob,args: [1,2,3])MyJob.set(wait_until:Date.tomorrow.noon,queue:"my_queue").perform_laterassert_enqueued_with(at:Date.tomorrow.noon,queue:"my_queue")end
For keyword arguments, specify them as a hash inside an array:
deftest_assert_enqueued_with_keyword_argumentsMyJob.perform_later(arg1:'value1',arg2:'value2')assert_enqueued_with(job:MyJob,args: [{arg1:'value1',arg2:'value2' }])end
The given arguments may also be specified as matcher procs that return a boolean value indicating whether a job’s attribute meets certain criteria.
For example, a proc can be used to match a range of times:
deftest_assert_enqueued_withat_matcher =->(job_at) { (Date.yesterday..Date.tomorrow).cover?(job_at) }MyJob.set(wait_until:Date.today.noon).perform_laterassert_enqueued_with(job:MyJob,at:at_matcher)end
A proc can also be used to match a subset of a job’s args:
deftest_assert_enqueued_withargs_matcher =->(job_args) {job_args[0].key?(:foo) }MyJob.perform_later(foo:"bar",other_arg:"No need to check in the test")assert_enqueued_with(job:MyJob,args:args_matcher)end
If a block is passed, asserts that the block will cause the job to be enqueued with the given arguments.
deftest_assert_enqueued_withassert_enqueued_with(job:MyJob,args: [1,2,3])doMyJob.perform_later(1,2,3)endassert_enqueued_with(job:MyJob,at:Date.tomorrow.noon)doMyJob.set(wait_until:Date.tomorrow.noon).perform_laterendend
# File activejob/lib/active_job/test_helper.rb, line 406defassert_enqueued_with(job:nil,args:nil,at:nil,queue:nil,priority:nil,&block)require_active_job_test_adapter!("assert_enqueued_with")expected = {job:job,args:args,at:at,queue:queue,priority:priority }.compactexpected_args =prepare_args_for_assertion(expected)potential_matches = []ifblock_given?original_enqueued_jobs =enqueued_jobs.dup_assert_nothing_raised_or_warn("assert_enqueued_with",&block)jobs =enqueued_jobs-original_enqueued_jobselsejobs =enqueued_jobsendmatching_job =jobs.finddo|enqueued_job|deserialized_job =deserialize_args_for_assertion(enqueued_job)potential_matches<<deserialized_jobexpected_args.all?do|key,value|ifvalue.respond_to?(:call)value.call(deserialized_job[key])elsevalue==deserialized_job[key]endendendmatching_class =potential_matches.selectdo|enqueued_job|enqueued_job["job_class"]==job.to_sendmessage =+"No enqueued job found with #{expected}"ifpotential_matches.empty?message<<"\n\nNo jobs were enqueued"elsifmatching_class.empty?message<<"\n\nNo jobs of class #{expected[:job]} were enqueued, job classes enqueued: "message<<potential_matches.map {|job|job["job_class"] }.join(", ")elsemessage<<"\n\nPotential matches: #{matching_class.join("\n")}"endassertmatching_job,messageinstantiate_job(matching_job)end
assert_no_enqueued_jobs(only: nil, except: nil, queue: nil, &block)Link
Asserts that no jobs have been enqueued.
deftest_jobsassert_no_enqueued_jobsHelloJob.perform_later('jeremy')assert_enqueued_jobs1end
If a block is passed, asserts that the block will not cause any job to be enqueued.
deftest_jobs_againassert_no_enqueued_jobsdo# No job should be enqueued from this blockendend
Asserts that no jobs of a specific kind are enqueued by passing:only option.
deftest_no_loggingassert_no_enqueued_jobsonly:LoggingJobdoHelloJob.perform_later('jeremy')endend
Asserts that no jobs except specific class are enqueued by passing:except option.
deftest_no_loggingassert_no_enqueued_jobsexcept:HelloJobdoHelloJob.perform_later('jeremy')endend
:only and:except options acceptClass,Array ofClass, or Proc. When passed a Proc, a hash containing the job’s class and it’s argument are passed as argument.
Asserts that no jobs are enqueued to a specific queue by passing:queue option
deftest_no_loggingassert_no_enqueued_jobsqueue:'default'doLoggingJob.set(queue::some_queue).perform_laterendend
Note: This assertion is simply a shortcut for:
assert_enqueued_jobs0,&block
assert_no_performed_jobs(only: nil, except: nil, queue: nil, &block)Link
Asserts that no jobs have been performed.
deftest_jobsassert_no_performed_jobsperform_enqueued_jobsdoHelloJob.perform_later('matthew')assert_performed_jobs1endend
If a block is passed, asserts that the block will not cause any job to be performed.
deftest_jobs_againassert_no_performed_jobsdo# No job should be performed from this blockendend
The block form supports filtering. If the:only option is specified, then only the listed job(s) will not be performed.
deftest_no_loggingassert_no_performed_jobsonly:LoggingJobdoHelloJob.perform_later('jeremy')endend
Also if the:except option is specified, then the job(s) except specific class will not be performed.
deftest_no_loggingassert_no_performed_jobsexcept:HelloJobdoHelloJob.perform_later('jeremy')endend
:only and:except options acceptClass,Array ofClass, or Proc. When passed a Proc, an instance of the job will be passed as argument.
If the:queue option is specified, then only the job(s) enqueued to a specific queue will not be performed.
deftest_assert_no_performed_jobs_with_queue_optionassert_no_performed_jobsqueue::some_queuedoHelloJob.set(queue::other_queue).perform_later("jeremy")endend
Note: This assertion is simply a shortcut for:
assert_performed_jobs0,&block
assert_performed_jobs(number, only: nil, except: nil, queue: nil, &block)Link
Asserts that the number of performed jobs matches the given number. If no block is passed,perform_enqueued_jobs must be called around or after the job call.
deftest_jobsassert_performed_jobs0perform_enqueued_jobsdoHelloJob.perform_later('xavier')endassert_performed_jobs1HelloJob.perform_later('yves')perform_enqueued_jobsassert_performed_jobs2end
If a block is passed, asserts that the block will cause the specified number of jobs to be performed.
deftest_jobs_againassert_performed_jobs1doHelloJob.perform_later('robin')endassert_performed_jobs2doHelloJob.perform_later('carlos')HelloJob.perform_later('sean')endend
This method also supports filtering. If the:only option is specified, then only the listed job(s) will be performed.
deftest_hello_jobassert_performed_jobs1,only:HelloJobdoHelloJob.perform_later('jeremy')LoggingJob.perform_laterendend
Also if the:except option is specified, then the job(s) except specific class will be performed.
deftest_hello_jobassert_performed_jobs1,except:LoggingJobdoHelloJob.perform_later('jeremy')LoggingJob.perform_laterendend
An array may also be specified, to support testing multiple jobs.
deftest_hello_and_logging_jobsassert_nothing_raiseddoassert_performed_jobs2,only: [HelloJob,LoggingJob]doHelloJob.perform_later('jeremy')LoggingJob.perform_later('stewie')RescueJob.perform_later('david')endendend
A proc may also be specified. When passed a Proc, the job’s instance will be passed as argument.
deftest_hello_and_logging_jobsassert_nothing_raiseddoassert_performed_jobs(1,only:->(job) {job.is_a?(HelloJob) })doHelloJob.perform_later('jeremy')LoggingJob.perform_later('stewie')RescueJob.perform_later('david')endendend
If the:queue option is specified, then only the job(s) enqueued to a specific queue will be performed.
deftest_assert_performed_jobs_with_queue_optionassert_performed_jobs1,queue::some_queuedoHelloJob.set(queue::some_queue).perform_later("jeremy")HelloJob.set(queue::other_queue).perform_later("bogdan")endend
# File activejob/lib/active_job/test_helper.rb, line 278defassert_performed_jobs(number,only:nil,except:nil,queue:nil,&block)require_active_job_test_adapter!("assert_performed_jobs")ifblock_given?original_count =performed_jobs.sizeperform_enqueued_jobs(only:only,except:except,queue:queue,&block)new_count =performed_jobs.sizeperformed_jobs_size =new_count-original_countelseperformed_jobs_size =performed_jobs_with(only:only,except:except,queue:queue).countendassert_equalnumber,performed_jobs_size,"#{number} jobs expected, but #{performed_jobs_size} were performed"end
assert_performed_with(job: nil, args: nil, at: nil, queue: nil, priority: nil, &block)Link
Asserts that the job has been performed with the given arguments.
deftest_assert_performed_withMyJob.perform_later(1,2,3)perform_enqueued_jobsassert_performed_with(job:MyJob,args: [1,2,3])MyJob.set(wait_until:Date.tomorrow.noon,queue:"my_queue").perform_laterperform_enqueued_jobsassert_performed_with(at:Date.tomorrow.noon,queue:"my_queue")end
The given arguments may also be specified as matcher procs that return a boolean value indicating whether a job’s attribute meets certain criteria.
For example, a proc can be used to match a range of times:
deftest_assert_performed_withat_matcher =->(job_at) { (Date.yesterday..Date.tomorrow).cover?(job_at) }MyJob.set(wait_until:Date.today.noon).perform_laterperform_enqueued_jobsassert_performed_with(job:MyJob,at:at_matcher)end
A proc can also be used to match a subset of a job’s args:
deftest_assert_performed_withargs_matcher =->(job_args) {job_args[0].key?(:foo) }MyJob.perform_later(foo:"bar",other_arg:"No need to check in the test")perform_enqueued_jobsassert_performed_with(job:MyJob,args:args_matcher)end
If a block is passed, that block performs all of the jobs that were enqueued throughout the duration of the block and asserts that the job has been performed with the given arguments in the block.
deftest_assert_performed_withassert_performed_with(job:MyJob,args: [1,2,3])doMyJob.perform_later(1,2,3)endassert_performed_with(job:MyJob,at:Date.tomorrow.noon)doMyJob.set(wait_until:Date.tomorrow.noon).perform_laterendend
# File activejob/lib/active_job/test_helper.rb, line 510defassert_performed_with(job:nil,args:nil,at:nil,queue:nil,priority:nil,&block)require_active_job_test_adapter!("assert_performed_with")expected = {job:job,args:args,at:at,queue:queue,priority:priority }.compactexpected_args =prepare_args_for_assertion(expected)potential_matches = []ifblock_given?original_performed_jobs_count =performed_jobs.countperform_enqueued_jobs(&block)jobs =performed_jobs.drop(original_performed_jobs_count)elsejobs =performed_jobsendmatching_job =jobs.finddo|enqueued_job|deserialized_job =deserialize_args_for_assertion(enqueued_job)potential_matches<<deserialized_jobexpected_args.all?do|key,value|ifvalue.respond_to?(:call)value.call(deserialized_job[key])elsevalue==deserialized_job[key]endendendmatching_class =potential_matches.selectdo|enqueued_job|enqueued_job["job_class"]==job.to_sendmessage =+"No performed job found with #{expected}"ifpotential_matches.empty?message<<"\n\nNo jobs were performed"elsifmatching_class.empty?message<<"\n\nNo jobs of class #{expected[:job]} were performed, job classes performed: "message<<potential_matches.map {|job|job["job_class"] }.join(", ")elsemessage<<"\n\nPotential matches: #{matching_class.join("\n")}"endassertmatching_job,messageinstantiate_job(matching_job)end
perform_enqueued_jobs(only: nil, except: nil, queue: nil, at: nil, &block)Link
Performs all enqueued jobs. If a block is given, performs all of the jobs that were enqueued throughout the duration of the block. If a block is not given, performs all of the enqueued jobs up to this point in the test.
deftest_perform_enqueued_jobsperform_enqueued_jobsdoMyJob.perform_later(1,2,3)endassert_performed_jobs1enddeftest_perform_enqueued_jobs_without_blockMyJob.perform_later(1,2,3)perform_enqueued_jobsassert_performed_jobs1end
This method also supports filtering. If the:only option is specified, then only the listed job(s) will be performed.
deftest_perform_enqueued_jobs_with_onlyperform_enqueued_jobs(only:MyJob)doMyJob.perform_later(1,2,3)# will be performedHelloJob.perform_later(1,2,3)# will not be performedendassert_performed_jobs1end
Also if the:except option is specified, then the job(s) except specific class will be performed.
deftest_perform_enqueued_jobs_with_exceptperform_enqueued_jobs(except:HelloJob)doMyJob.perform_later(1,2,3)# will be performedHelloJob.perform_later(1,2,3)# will not be performedendassert_performed_jobs1end
:only and:except options acceptClass,Array ofClass, or Proc. When passed a Proc, an instance of the job will be passed as argument.
If the:queue option is specified, then only the job(s) enqueued to a specific queue will be performed.
deftest_perform_enqueued_jobs_with_queueperform_enqueued_jobsqueue::some_queuedoMyJob.set(queue::some_queue).perform_later(1,2,3)# will be performedHelloJob.set(queue::other_queue).perform_later(1,2,3)# will not be performedendassert_performed_jobs1end
If the:at option is specified, then only jobs that have been enqueued to run at or before the given time will be performed. This includes jobs that have been enqueued without a time.
Ifqueue_adapter_for_test is overridden to return a different adapter,perform_enqueued_jobs will merely execute the block.
# File activejob/lib/active_job/test_helper.rb, line 620defperform_enqueued_jobs(only:nil,except:nil,queue:nil,at:nil,&block)unlessblock_given?require_active_job_test_adapter!("perform_enqueued_jobs (without a block)")returnflush_enqueued_jobs(only:only,except:except,queue:queue,at:at)endreturn_assert_nothing_raised_or_warn("perform_enqueued_jobs",&block)unlessusing_test_adapter?validate_option(only:only,except:except)old_perform_enqueued_jobs =queue_adapter.perform_enqueued_jobsold_perform_enqueued_at_jobs =queue_adapter.perform_enqueued_at_jobsold_filter =queue_adapter.filterold_reject =queue_adapter.rejectold_queue =queue_adapter.queueold_at =queue_adapter.atbeginqueue_adapter.perform_enqueued_jobs =truequeue_adapter.perform_enqueued_at_jobs =truequeue_adapter.filter =onlyqueue_adapter.reject =exceptqueue_adapter.queue =queuequeue_adapter.at =at_assert_nothing_raised_or_warn("perform_enqueued_jobs",&block)ensurequeue_adapter.perform_enqueued_jobs =old_perform_enqueued_jobsqueue_adapter.perform_enqueued_at_jobs =old_perform_enqueued_at_jobsqueue_adapter.filter =old_filterqueue_adapter.reject =old_rejectqueue_adapter.queue =old_queuequeue_adapter.at =old_atendend
queue_adapter()Link
Accesses thequeue_adapter set byActiveJob::Base.
deftest_assert_job_has_custom_queue_adapter_setassert_instance_ofCustomQueueAdapter,HelloJob.queue_adapterend
queue_adapter_for_test()Link
Returns a queue adapter instance to use with all Active Job test helpers. By default, returns an instance ofActiveJob::QueueAdapters::TestAdapter. Override this method to specify a different adapter. The adapter must implement the same interface asActiveJob::QueueAdapters::TestAdapter.