提供用于测试 Active Job 的辅助方法
- A
- P
- Q
实例公共方法
assert_enqueued_jobs(number, only: nil, except: nil, queue: nil, &block) 链接
断言已入队的作业数量与给定数量匹配。
def test_jobs
assert_enqueued_jobs 0
HelloJob.perform_later('david')
assert_enqueued_jobs 1
HelloJob.perform_later('abdelkader')
assert_enqueued_jobs 2
end
如果传递了一个块,则断言该块将导致指定数量的作业入队。
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
通过传递:only
选项,断言特定作业入队的次数。
def test_logging_job
assert_enqueued_jobs 1, only: LoggingJob do
LoggingJob.perform_later
HelloJob.perform_later('jeremy')
end
end
通过传递:except
选项,断言除了特定类以外的作业入队的次数。
def test_logging_job
assert_enqueued_jobs 1, except: HelloJob do
LoggingJob.perform_later
HelloJob.perform_later('jeremy')
end
end
:only
和:except
选项接受类
、数组
的类
或Proc。当传递一个Proc时,将传递一个包含作业类及其参数的哈希作为参数。
通过传递:queue
选项,断言作业入队到特定队列的次数。
def test_logging_job
assert_enqueued_jobs 2, queue: 'default' do
LoggingJob.perform_later
HelloJob.perform_later('elfassy')
end
end
# File activejob/lib/active_job/test_helper.rb, line 122 def assert_enqueued_jobs(number, only: nil, except: nil, queue: nil, &block) require_active_job_test_adapter!("assert_enqueued_jobs") if block_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).count else actual_count = enqueued_jobs_with(only: only, except: except, queue: queue).count end assert_equal number, 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) 链接
断言作业已使用给定的参数入队。
def test_assert_enqueued_with
MyJob.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_later
assert_enqueued_with(at: Date.tomorrow.noon, queue: "my_queue")
end
对于关键字参数,将它们指定为数组中的哈希。
def test_assert_enqueued_with_keyword_arguments
MyJob.perform_later(arg1: 'value1', arg2: 'value2')
assert_enqueued_with(job: MyJob, args: [{ arg1: 'value1', arg2: 'value2' }])
end
给定的参数也可以指定为匹配器Proc,这些Proc返回一个布尔值,指示作业的属性是否满足某些条件。
例如,可以使用Proc来匹配时间范围
def test_assert_enqueued_with
at_matcher = ->(job_at) { (Date.yesterday..Date.tomorrow).cover?(job_at) }
MyJob.set(wait_until: Date.today.noon).perform_later
assert_enqueued_with(job: MyJob, at: at_matcher)
end
Proc也可以用于匹配作业参数的子集
def test_assert_enqueued_with
args_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
如果传递了一个块,则断言该块将导致作业使用给定的参数入队。
def test_assert_enqueued_with
assert_enqueued_with(job: MyJob, args: [1,2,3]) do
MyJob.perform_later(1,2,3)
end
assert_enqueued_with(job: MyJob, at: Date.tomorrow.noon) do
MyJob.set(wait_until: Date.tomorrow.noon).perform_later
end
end
# File activejob/lib/active_job/test_helper.rb, line 406 def assert_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 }.compact expected_args = prepare_args_for_assertion(expected) potential_matches = [] if block_given? original_enqueued_jobs = enqueued_jobs.dup _assert_nothing_raised_or_warn("assert_enqueued_with", &block) jobs = enqueued_jobs - original_enqueued_jobs else jobs = enqueued_jobs end matching_job = jobs.find do |enqueued_job| deserialized_job = deserialize_args_for_assertion(enqueued_job) potential_matches << deserialized_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 matching_class = potential_matches.select do |enqueued_job| enqueued_job["job_class"] == job.to_s end message = +"No enqueued job found with #{expected}" if potential_matches.empty? message << "\n\nNo jobs were enqueued" elsif matching_class.empty? message << "\n\nNo jobs of class #{expected[:job]} were enqueued, job classes enqueued: " message << potential_matches.map { |job| job["job_class"] }.join(", ") else message << "\n\nPotential matches: #{matching_class.join("\n")}" end assert matching_job, message instantiate_job(matching_job) end
assert_no_enqueued_jobs(only: nil, except: nil, queue: nil, &block) 链接
断言没有作业入队。
def test_jobs
assert_no_enqueued_jobs
HelloJob.perform_later('jeremy')
assert_enqueued_jobs 1
end
如果传递了一个块,则断言该块不会导致任何作业入队。
def test_jobs_again
assert_no_enqueued_jobs do
# No job should be enqueued from this block
end
end
通过传递:only
选项,断言没有特定类型的作业入队。
def test_no_logging
assert_no_enqueued_jobs only: LoggingJob do
HelloJob.perform_later('jeremy')
end
end
通过传递:except
选项,断言除了特定类以外的作业没有入队。
def test_no_logging
assert_no_enqueued_jobs except: HelloJob do
HelloJob.perform_later('jeremy')
end
end
:only
和:except
选项接受类
、数组
的类
或Proc。当传递一个Proc时,将传递一个包含作业类及其参数的哈希作为参数。
通过传递:queue
选项,断言没有作业入队到特定队列。
def test_no_logging
assert_no_enqueued_jobs queue: 'default' do
LoggingJob.set(queue: :some_queue).perform_later
end
end
注意:此断言只是assert_enqueued_jobs(0, ...)
的快捷方式。
assert_enqueued_jobs 0, &block
assert_no_performed_jobs(only: nil, except: nil, queue: nil, &block) 链接
断言没有作业已执行。
def test_jobs
assert_no_performed_jobs
perform_enqueued_jobs do
HelloJob.perform_later('matthew')
assert_performed_jobs 1
end
end
如果传递了一个块,则断言该块不会导致任何作业执行。
def test_jobs_again
assert_no_performed_jobs do
# No job should be performed from this block
end
end
块形式支持过滤。如果指定了:only
选项,则只列出的作业不会执行。
def test_no_logging
assert_no_performed_jobs only: LoggingJob do
HelloJob.perform_later('jeremy')
end
end
同样,如果指定了:except
选项,则除了特定类以外的作业不会执行。
def test_no_logging
assert_no_performed_jobs except: HelloJob do
HelloJob.perform_later('jeremy')
end
end
:only
和:except
选项接受类
、数组
的类
或Proc。当传递一个Proc时,将传递作业的实例作为参数。
如果指定了:queue
选项,则只入队到特定队列的作业不会执行。
def test_assert_no_performed_jobs_with_queue_option
assert_no_performed_jobs queue: :some_queue do
HelloJob.set(queue: :other_queue).perform_later("jeremy")
end
end
注意:此断言只是assert_enqueued_jobs(0, ...)
的快捷方式。
assert_performed_jobs 0, &block
assert_performed_jobs(number, only: nil, except: nil, queue: nil, &block) 链接
断言已执行的作业数量与给定数量匹配。如果没有传递块,则必须在作业调用前后调用perform_enqueued_jobs
。
def test_jobs
assert_performed_jobs 0
perform_enqueued_jobs do
HelloJob.perform_later('xavier')
end
assert_performed_jobs 1
HelloJob.perform_later('yves')
perform_enqueued_jobs
assert_performed_jobs 2
end
如果传递了一个块,则断言该块将导致指定数量的作业执行。
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
此方法还支持过滤。如果指定了:only
选项,则只列出的作业将执行。
def test_hello_job
assert_performed_jobs 1, only: HelloJob do
HelloJob.perform_later('jeremy')
LoggingJob.perform_later
end
end
同样,如果指定了:except
选项,则除了特定类以外的作业将执行。
def test_hello_job
assert_performed_jobs 1, except: LoggingJob do
HelloJob.perform_later('jeremy')
LoggingJob.perform_later
end
end
还可以指定一个数组,以支持测试多个作业。
def test_hello_and_logging_jobs
assert_nothing_raised do
assert_performed_jobs 2, only: [HelloJob, LoggingJob] do
HelloJob.perform_later('jeremy')
LoggingJob.perform_later('stewie')
RescueJob.perform_later('david')
end
end
end
也可以指定一个Proc。当传递一个Proc时,将传递作业的实例作为参数。
def test_hello_and_logging_jobs
assert_nothing_raised do
assert_performed_jobs(1, only: ->(job) { job.is_a?(HelloJob) }) do
HelloJob.perform_later('jeremy')
LoggingJob.perform_later('stewie')
RescueJob.perform_later('david')
end
end
end
如果指定了:queue
选项,则只入队到特定队列的作业将执行。
def test_assert_performed_jobs_with_queue_option
assert_performed_jobs 1, queue: :some_queue do
HelloJob.set(queue: :some_queue).perform_later("jeremy")
HelloJob.set(queue: :other_queue).perform_later("bogdan")
end
end
# File activejob/lib/active_job/test_helper.rb, line 278 def assert_performed_jobs(number, only: nil, except: nil, queue: nil, &block) require_active_job_test_adapter!("assert_performed_jobs") 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).count end assert_equal number, 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) 链接
断言作业已使用给定的参数执行。
def test_assert_performed_with
MyJob.perform_later(1,2,3)
perform_enqueued_jobs
assert_performed_with(job: MyJob, args: [1,2,3])
MyJob.set(wait_until: Date.tomorrow.noon, queue: "my_queue").perform_later
perform_enqueued_jobs
assert_performed_with(at: Date.tomorrow.noon, queue: "my_queue")
end
给定的参数也可以指定为匹配器Proc,这些Proc返回一个布尔值,指示作业的属性是否满足某些条件。
例如,可以使用Proc来匹配时间范围
def test_assert_performed_with
at_matcher = ->(job_at) { (Date.yesterday..Date.tomorrow).cover?(job_at) }
MyJob.set(wait_until: Date.today.noon).perform_later
perform_enqueued_jobs
assert_performed_with(job: MyJob, at: at_matcher)
end
Proc也可以用于匹配作业参数的子集
def test_assert_performed_with
args_matcher = ->(job_args) { job_args[0].key?(:foo) }
MyJob.perform_later(foo: "bar", other_arg: "No need to check in the test")
perform_enqueued_jobs
assert_performed_with(job: MyJob, args: args_matcher)
end
如果传递了一个块,则该块执行在块持续时间内入队的全部作业,并断言作业已使用块中的给定参数执行。
def test_assert_performed_with
assert_performed_with(job: MyJob, args: [1,2,3]) do
MyJob.perform_later(1,2,3)
end
assert_performed_with(job: MyJob, at: Date.tomorrow.noon) do
MyJob.set(wait_until: Date.tomorrow.noon).perform_later
end
end
# File activejob/lib/active_job/test_helper.rb, line 510 def assert_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 }.compact expected_args = prepare_args_for_assertion(expected) potential_matches = [] 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) potential_matches << deserialized_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 matching_class = potential_matches.select do |enqueued_job| enqueued_job["job_class"] == job.to_s end message = +"No performed job found with #{expected}" if potential_matches.empty? message << "\n\nNo jobs were performed" elsif matching_class.empty? message << "\n\nNo jobs of class #{expected[:job]} were performed, job classes performed: " message << potential_matches.map { |job| job["job_class"] }.join(", ") else message << "\n\nPotential matches: #{matching_class.join("\n")}" end assert matching_job, message instantiate_job(matching_job) end
perform_enqueued_jobs(only: nil, except: nil, queue: nil, at: nil, &block) 链接
执行所有入队的作业。如果给出了一个块,则执行在块持续时间内入队的全部作业。如果没有给出块,则执行到测试此时为止的所有入队作业。
def test_perform_enqueued_jobs
perform_enqueued_jobs do
MyJob.perform_later(1, 2, 3)
end
assert_performed_jobs 1
end
def test_perform_enqueued_jobs_without_block
MyJob.perform_later(1, 2, 3)
perform_enqueued_jobs
assert_performed_jobs 1
end
此方法还支持过滤。如果指定了:only
选项,则只列出的作业将执行。
def test_perform_enqueued_jobs_with_only
perform_enqueued_jobs(only: MyJob) do
MyJob.perform_later(1, 2, 3) # will be performed
HelloJob.perform_later(1, 2, 3) # will not be performed
end
assert_performed_jobs 1
end
同样,如果指定了:except
选项,则除了特定类以外的作业将执行。
def test_perform_enqueued_jobs_with_except
perform_enqueued_jobs(except: HelloJob) do
MyJob.perform_later(1, 2, 3) # will be performed
HelloJob.perform_later(1, 2, 3) # will not be performed
end
assert_performed_jobs 1
end
:only
和:except
选项接受类
、数组
的类
或Proc。当传递一个Proc时,将传递作业的实例作为参数。
如果指定了:queue
选项,则只入队到特定队列的作业将执行。
def test_perform_enqueued_jobs_with_queue
perform_enqueued_jobs queue: :some_queue do
MyJob.set(queue: :some_queue).perform_later(1, 2, 3) # will be performed
HelloJob.set(queue: :other_queue).perform_later(1, 2, 3) # will not be performed
end
assert_performed_jobs 1
end
如果指定了:at
选项,则只执行已入队在给定时间或之前运行的作业。这包括已入队但没有时间的作业。
如果queue_adapter_for_test
被重写为返回不同的适配器,则perform_enqueued_jobs
将仅仅执行块。
# File activejob/lib/active_job/test_helper.rb, line 620 def perform_enqueued_jobs(only: nil, except: nil, queue: nil, at: nil, &block) unless block_given? require_active_job_test_adapter!("perform_enqueued_jobs (without a block)") return flush_enqueued_jobs(only: only, except: except, queue: queue, at: at) end return _assert_nothing_raised_or_warn("perform_enqueued_jobs", &block) unless using_test_adapter? 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 old_at = queue_adapter.at 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 queue_adapter.at = at _assert_nothing_raised_or_warn("perform_enqueued_jobs", &block) 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 queue_adapter.at = old_at end end
queue_adapter() 链接
访问由ActiveJob::Base
设置的queue_adapter
。
def test_assert_job_has_custom_queue_adapter_set
assert_instance_of CustomQueueAdapter, HelloJob.queue_adapter
end