mirror of https://github.com/rails/rails
[ActiveJob] TestCase (Will squash before merge)
This commit is contained in:
parent
fccf3d0b63
commit
d7add5352e
|
@ -6,10 +6,10 @@ module ActiveJob
|
|||
|
||||
# Provides a store of all the enqueued jobs with the TestAdapter so you can check them.
|
||||
def enqueued_jobs
|
||||
@@enqueued_jobs ||= []
|
||||
@enqueued_jobs ||= []
|
||||
end
|
||||
|
||||
# Allows you to over write the default enqueued jobs store from an array to some
|
||||
# Allows you to overwrite the default enqueued jobs store from an array to some
|
||||
# other object. If you just want to clear the store,
|
||||
# call ActiveJob::QueueAdapters::TestAdapter.enqueued_jobs.clear.
|
||||
#
|
||||
|
@ -21,15 +21,15 @@ module ActiveJob
|
|||
# * size
|
||||
# * and other common Array methods
|
||||
def enqueued_jobs=(val)
|
||||
@@enqueued_jobs = val
|
||||
@enqueued_jobs = val
|
||||
end
|
||||
|
||||
# Provides a store of all the performed jobs with the TestAdapter so you can check them.
|
||||
def performed_jobs
|
||||
@@performed_jobs ||= []
|
||||
@performed_jobs ||= []
|
||||
end
|
||||
|
||||
# Allows you to over write the default performed jobs store from an array to some
|
||||
# Allows you to overwrite the default performed jobs store from an array to some
|
||||
# other object. If you just want to clear the store,
|
||||
# call ActiveJob::QueueAdapters::TestAdapter.performed_jobs.clear.
|
||||
#
|
||||
|
@ -41,7 +41,7 @@ module ActiveJob
|
|||
# * size
|
||||
# * and other common Array methods
|
||||
def performed_jobs=(val)
|
||||
@@performed_jobs = val
|
||||
@performed_jobs = val
|
||||
end
|
||||
|
||||
def enqueue(job, *args)
|
||||
|
@ -63,13 +63,13 @@ module ActiveJob
|
|||
end
|
||||
|
||||
private
|
||||
def perform_enqueued_jobs?
|
||||
perform_enqueued_jobs
|
||||
end
|
||||
def perform_enqueued_jobs?
|
||||
perform_enqueued_jobs
|
||||
end
|
||||
|
||||
def perform_enqueued_at_jobs?
|
||||
perform_enqueued_at_jobs
|
||||
end
|
||||
def perform_enqueued_at_jobs?
|
||||
perform_enqueued_at_jobs
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
# encoding: utf-8
|
||||
require 'active_support/test_case'
|
||||
|
||||
module ActiveJob
|
||||
class TestCase < ActiveSupport::TestCase
|
||||
include ActiveJob::TestHelper
|
||||
include ActiveJob::TestHelper
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,13 +1,21 @@
|
|||
# encoding: utf-8
|
||||
module ActiveJob
|
||||
# Provides helper methods for testing Active Job
|
||||
module TestHelper
|
||||
extend ActiveSupport::Concern
|
||||
include ActiveSupport::Testing::ConstantLookup
|
||||
|
||||
included do
|
||||
class_attribute :_job_class
|
||||
setup :initialize_queue_test_adapter
|
||||
def before_setup
|
||||
@old_queue_adapter = queue_adapter
|
||||
ActiveJob::Base.queue_adapter = :test
|
||||
clear_enqueued_jobs
|
||||
clear_performed_jobs
|
||||
super
|
||||
end
|
||||
|
||||
def after_teardown
|
||||
super
|
||||
ActiveJob::Base.queue_adapter = @old_queue_adapter
|
||||
end
|
||||
|
||||
# Asserts that the number of enqueued jobs matches the given number.
|
||||
#
|
||||
|
@ -40,7 +48,8 @@ module ActiveJob
|
|||
assert_equal original_count + number, new_count,
|
||||
"#{number} jobs expected, but #{new_count - original_count} were enqueued"
|
||||
else
|
||||
assert_equal number, enqueued_jobs.size
|
||||
enqueued_jobs_size = enqueued_jobs.size
|
||||
assert_equal number, enqueued_jobs_size, "#{number} jobs expected, but #{enqueued_jobs_size} were enqueued"
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -98,7 +107,8 @@ module ActiveJob
|
|||
assert_equal original_count + number, new_count,
|
||||
"#{number} jobs expected, but #{new_count - original_count} were performed"
|
||||
else
|
||||
assert_equal number, performed_jobs.size
|
||||
performed_jobs_size = performed_jobs.size
|
||||
assert_equal number, performed_jobs_size, "#{number} jobs expected, but #{performed_jobs_size} were performed"
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -165,7 +175,6 @@ module ActiveJob
|
|||
queue_adapter.performed_jobs = original_performed_jobs + performed_jobs
|
||||
end
|
||||
|
||||
|
||||
def queue_adapter
|
||||
ActiveJob::Base.queue_adapter
|
||||
end
|
||||
|
@ -175,12 +184,6 @@ module ActiveJob
|
|||
to: :queue_adapter
|
||||
|
||||
private
|
||||
def initialize_queue_test_adapter
|
||||
ActiveJob::Base.queue_adapter = :test
|
||||
clear_enqueued_jobs
|
||||
clear_performed_jobs
|
||||
end
|
||||
|
||||
def clear_enqueued_jobs
|
||||
enqueued_jobs.clear
|
||||
end
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
# encoding: utf-8
|
||||
require 'helper'
|
||||
require 'jobs/hello_job'
|
||||
require 'jobs/logging_job'
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
# encoding: utf-8
|
||||
require 'helper'
|
||||
require 'active_support/core_ext/time'
|
||||
require 'active_support/core_ext/date'
|
||||
|
@ -74,7 +73,7 @@ class EnqueuedJobsTest < ActiveJob::TestCase
|
|||
|
||||
assert_match(/1 .* but 2/, error.message)
|
||||
end
|
||||
#
|
||||
|
||||
def test_assert_no_enqueued_jobs_failure
|
||||
error = assert_raise ActiveSupport::TestCase::Assertion do
|
||||
assert_no_enqueued_jobs do
|
||||
|
@ -182,7 +181,7 @@ class PerformedJobsTest < ActiveJob::TestCase
|
|||
|
||||
assert_match(/1 .* but 2/, error.message)
|
||||
end
|
||||
#
|
||||
|
||||
def test_assert_no_performed_jobs_failure
|
||||
error = assert_raise ActiveSupport::TestCase::Assertion do
|
||||
assert_no_performed_jobs do
|
||||
|
|
Loading…
Reference in New Issue