Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Setup and teardown now use new callbacks.

  • Loading branch information...
commit e58ad8ed9cb0e67fde93c7edc8a0e13b8bc342ae 1 parent 97204fc
@josevalim josevalim authored
View
2  activesupport/lib/active_support/callbacks.rb
@@ -90,7 +90,7 @@ def run_callbacks(kind, *args, &block)
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
View
73 activesupport/lib/active_support/testing/setup_and_teardown.rb
@@ -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 ForMiniTest
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 ForClassicTestUnit
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 @@ def run(result)
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 @@ def run(result)
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
View
9 activesupport/test/test_test.rb
@@ -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 @@ def teardown
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
Please sign in to comment.
Something went wrong with that request. Please try again.