mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Setup and teardown now use new callbacks.
This commit is contained in:
parent
97204fc0bc
commit
e58ad8ed9c
3 changed files with 52 additions and 32 deletions
|
@ -90,7 +90,7 @@ module ActiveSupport
|
|||
class Callback
|
||||
@@_callback_sequence = 0
|
||||
|
||||
attr_accessor :chain, :filter, :kind, :options, :per_key, :klass
|
||||
attr_accessor :chain, :filter, :kind, :options, :per_key, :klass, :raw_filter
|
||||
|
||||
def initialize(chain, filter, kind, options, klass)
|
||||
@chain, @kind, @klass = chain, kind, klass
|
||||
|
|
|
@ -1,16 +1,26 @@
|
|||
module ActiveSupport
|
||||
module Testing
|
||||
module SetupAndTeardown
|
||||
def self.included(base)
|
||||
base.class_eval do
|
||||
include ActiveSupport::DeprecatedCallbacks
|
||||
define_callbacks :setup, :teardown
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
if defined?(MiniTest::Assertions) && TestCase < MiniTest::Assertions
|
||||
include ForMiniTest
|
||||
else
|
||||
include ForClassicTestUnit
|
||||
end
|
||||
included do
|
||||
include ActiveSupport::Callbacks
|
||||
define_callbacks :setup, :teardown
|
||||
|
||||
if defined?(MiniTest::Assertions) && TestCase < MiniTest::Assertions
|
||||
include ForMiniTest
|
||||
else
|
||||
include ForClassicTestUnit
|
||||
end
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
def setup(*args, &block)
|
||||
set_callback(:setup, :before, *args, &block)
|
||||
end
|
||||
|
||||
def teardown(*args, &block)
|
||||
set_callback(:teardown, :after, *args, &block)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -18,13 +28,14 @@ module ActiveSupport
|
|||
def run(runner)
|
||||
result = '.'
|
||||
begin
|
||||
run_callbacks :setup
|
||||
result = super
|
||||
_run_setup_callbacks do
|
||||
result = super
|
||||
end
|
||||
rescue Exception => e
|
||||
result = runner.puke(self.class, method_name, e)
|
||||
ensure
|
||||
begin
|
||||
run_callbacks :teardown, :enumerator => :reverse_each
|
||||
_run_teardown_callbacks
|
||||
rescue Exception => e
|
||||
result = runner.puke(self.class, method_name, e)
|
||||
end
|
||||
|
@ -42,23 +53,17 @@ module ActiveSupport
|
|||
def run(result)
|
||||
return if @method_name.to_s == "default_test"
|
||||
|
||||
if using_mocha = respond_to?(:mocha_verify)
|
||||
assertion_counter_klass = if defined?(Mocha::TestCaseAdapter::AssertionCounter)
|
||||
Mocha::TestCaseAdapter::AssertionCounter
|
||||
else
|
||||
Mocha::Integration::TestUnit::AssertionCounter
|
||||
end
|
||||
assertion_counter = assertion_counter_klass.new(result)
|
||||
end
|
||||
|
||||
mocha_counter = retrieve_mocha_counter(result)
|
||||
yield(Test::Unit::TestCase::STARTED, name)
|
||||
@_result = result
|
||||
|
||||
begin
|
||||
begin
|
||||
run_callbacks :setup
|
||||
setup
|
||||
__send__(@method_name)
|
||||
mocha_verify(assertion_counter) if using_mocha
|
||||
_run_setup_callbacks do
|
||||
setup
|
||||
__send__(@method_name)
|
||||
mocha_verify(mocha_counter) if mocha_counter
|
||||
end
|
||||
rescue Mocha::ExpectationError => e
|
||||
add_failure(e.message, e.backtrace)
|
||||
rescue Test::Unit::AssertionFailedError => e
|
||||
|
@ -69,7 +74,7 @@ module ActiveSupport
|
|||
ensure
|
||||
begin
|
||||
teardown
|
||||
run_callbacks :teardown, :enumerator => :reverse_each
|
||||
_run_teardown_callbacks
|
||||
rescue Test::Unit::AssertionFailedError => e
|
||||
add_failure(e.message, e.backtrace)
|
||||
rescue Exception => e
|
||||
|
@ -78,12 +83,26 @@ module ActiveSupport
|
|||
end
|
||||
end
|
||||
ensure
|
||||
mocha_teardown if using_mocha
|
||||
mocha_teardown if mocha_counter
|
||||
end
|
||||
|
||||
result.add_run
|
||||
yield(Test::Unit::TestCase::FINISHED, name)
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def retrieve_mocha_counter(result) #:nodoc:
|
||||
if using_mocha = respond_to?(:mocha_verify)
|
||||
if defined?(Mocha::TestCaseAdapter::AssertionCounter)
|
||||
Mocha::TestCaseAdapter::AssertionCounter.new(result)
|
||||
else
|
||||
Mocha::Integration::TestUnit::AssertionCounter.new(result)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -102,9 +102,9 @@ class SetupAndTeardownTest < ActiveSupport::TestCase
|
|||
teardown :foo, :sentinel, :foo
|
||||
|
||||
def test_inherited_setup_callbacks
|
||||
assert_equal [:reset_callback_record, :foo], self.class.setup_callback_chain.map(&:method)
|
||||
assert_equal [:reset_callback_record, :foo], self.class._setup_callbacks.map(&:raw_filter)
|
||||
assert_equal [:foo], @called_back
|
||||
assert_equal [:foo, :sentinel, :foo], self.class.teardown_callback_chain.map(&:method)
|
||||
assert_equal [:foo, :sentinel, :foo], self.class._teardown_callbacks.map(&:raw_filter)
|
||||
end
|
||||
|
||||
def setup
|
||||
|
@ -114,6 +114,7 @@ class SetupAndTeardownTest < ActiveSupport::TestCase
|
|||
end
|
||||
|
||||
protected
|
||||
|
||||
def reset_callback_record
|
||||
@called_back = []
|
||||
end
|
||||
|
@ -133,9 +134,9 @@ class SubclassSetupAndTeardownTest < SetupAndTeardownTest
|
|||
teardown :bar
|
||||
|
||||
def test_inherited_setup_callbacks
|
||||
assert_equal [:reset_callback_record, :foo, :bar], self.class.setup_callback_chain.map(&:method)
|
||||
assert_equal [:reset_callback_record, :foo, :bar], self.class._setup_callbacks.map(&:raw_filter)
|
||||
assert_equal [:foo, :bar], @called_back
|
||||
assert_equal [:foo, :sentinel, :foo, :bar], self.class.teardown_callback_chain.map(&:method)
|
||||
assert_equal [:foo, :sentinel, :foo, :bar], self.class._teardown_callbacks.map(&:raw_filter)
|
||||
end
|
||||
|
||||
protected
|
||||
|
|
Loading…
Reference in a new issue