Skip to content
Browse files

Get ActiveSupport::TestCase working with classic Test::Unit and MiniT…

…est. Fix broken Mocha + MiniTest. Assume ruby-core applies patch #771 fixing libraries which extend Test::Unit.
  • Loading branch information...
1 parent c79fb32 commit e7208d382a3d8bae9ab13d8a380b1a2a05fd99b0 @jeremy jeremy committed
View
56 activesupport/lib/active_support/test_case.rb
@@ -1,46 +1,40 @@
+require 'test/unit/testcase'
require 'active_support/testing/setup_and_teardown'
require 'active_support/testing/assertions'
require 'active_support/testing/declarative'
-module ActiveSupport
- # Prefer MiniTest with Test::Unit compatibility.
- begin
- require 'minitest/unit'
+begin
+ gem 'mocha', '>= 0.9.0'
+ require 'mocha'
- # Hack around the test/unit autorun.
- autorun_enabled = MiniTest::Unit.send(:class_variable_get, '@@installed_at_exit')
- if MiniTest::Unit.respond_to?(:disable_autorun)
- MiniTest::Unit.disable_autorun
- else
- MiniTest::Unit.send(:class_variable_set, '@@installed_at_exit', false)
- end
- require 'test/unit'
- MiniTest::Unit.send(:class_variable_set, '@@installed_at_exit', autorun_enabled)
+ if defined?(MiniTest)
+ require 'active_support/testing/mocha_minitest_adapter'
+ end
+rescue LoadError
+ # Fake Mocha::ExpectationError so we can rescue it in #run. Bleh.
+ Object.const_set :Mocha, Module.new
+ Mocha.const_set :ExpectationError, Class.new(StandardError)
+end
- class TestCase < ::Test::Unit::TestCase
+module ActiveSupport
+ class TestCase < ::Test::Unit::TestCase
+ if defined? MiniTest
Assertion = MiniTest::Assertion
- end
-
- # TODO: Figure out how to get the Rails::BacktraceFilter into minitest/unit
- # Test::Unit compatibility.
- rescue LoadError
- require 'test/unit/testcase'
- require 'active_support/testing/default'
-
- if defined?(Rails)
- require 'rails/backtrace_cleaner'
- Test::Unit::Util::BacktraceFilter.module_eval { include Rails::BacktraceFilterForTestUnit }
- end
+ else
+ # TODO: Figure out how to get the Rails::BacktraceFilter into minitest/unit
+ if defined?(Rails)
+ require 'rails/backtrace_cleaner'
+ Test::Unit::Util::BacktraceFilter.module_eval { include Rails::BacktraceFilterForTestUnit }
+ end
- class TestCase < ::Test::Unit::TestCase
Assertion = Test::Unit::AssertionFailedError
+
+ require 'active_support/testing/default'
include ActiveSupport::Testing::Default
- end
- end
+ end
- class TestCase
include ActiveSupport::Testing::SetupAndTeardown
include ActiveSupport::Testing::Assertions
extend ActiveSupport::Testing::Declarative
end
-end
+end
View
45 activesupport/lib/active_support/testing/mocha_minitest_adapter.rb
@@ -0,0 +1,45 @@
+class MiniTest::Unit::TestCase
+ include Mocha::Standalone
+
+ class MochaAssertionCounter
+ def initialize(runner) @runner = runner end
+ def increment; @runner.assertion_count += 1 end
+ end
+
+ def run(runner)
+ assertion_counter = MochaAssertionCounter.new(runner)
+ result = '.'
+ begin
+ begin
+ @passed = nil
+ setup
+ __send__ name
+ mocha_verify(assertion_counter)
+ @passed = true
+ rescue Exception => e
+ @passed = false
+ result = runner.puke(self.class, self.name, e)
+ ensure
+ begin
+ teardown
+ rescue Exception => e
+ result = runner.puke(self.class, self.name, e)
+ end
+ end
+ ensure
+ mocha_teardown
+ end
+ result
+ end
+end
+
+module Test
+ module Unit
+ remove_const :TestCase
+
+ class TestCase < MiniTest::Unit::TestCase
+ include Test::Unit::Assertions
+ def self.test_order; :sorted end
+ end
+ end
+end
View
112 activesupport/lib/active_support/testing/setup_and_teardown.rb
@@ -3,30 +3,15 @@
module ActiveSupport
module Testing
module SetupAndTeardown
- # For compatibility with Ruby < 1.8.6
- PASSTHROUGH_EXCEPTIONS =
- if defined?(Test::Unit::TestCase::PASSTHROUGH_EXCEPTIONS)
- Test::Unit::TestCase::PASSTHROUGH_EXCEPTIONS
- else
- [NoMemoryError, SignalException, Interrupt, SystemExit]
- end
-
def self.included(base)
base.class_eval do
include ActiveSupport::Callbacks
define_callbacks :setup, :teardown
- if defined?(::MiniTest)
+ if defined? MiniTest
include ForMiniTest
else
- begin
- require 'mocha'
- undef_method :run
- alias_method :run, :run_with_callbacks_and_mocha
- rescue LoadError
- undef_method :run
- alias_method :run, :run_with_callbacks_and_testunit
- end
+ include ForClassicTestUnit
end
end
end
@@ -50,74 +35,51 @@ def run(runner)
end
end
- # This redefinition is unfortunate but test/unit shows us no alternative.
- def run_with_callbacks_and_testunit(result) #:nodoc:
- return if @method_name.to_s == "default_test"
+ module ForClassicTestUnit
+ # For compatibility with Ruby < 1.8.6
+ PASSTHROUGH_EXCEPTIONS = Test::Unit::TestCase::PASSTHROUGH_EXCEPTIONS rescue [NoMemoryError, SignalException, Interrupt, SystemExit]
- yield(Test::Unit::TestCase::STARTED, name)
- @_result = result
- begin
- run_callbacks :setup
- setup
- __send__(@method_name)
- rescue Test::Unit::AssertionFailedError => e
- add_failure(e.message, e.backtrace)
- rescue *PASSTHROUGH_EXCEPTIONS
- raise
- rescue Exception
- add_error($!)
- ensure
- begin
- teardown
- run_callbacks :teardown, :enumerator => :reverse_each
- rescue Test::Unit::AssertionFailedError => e
- add_failure(e.message, e.backtrace)
- rescue *PASSTHROUGH_EXCEPTIONS
- raise
- rescue Exception
- add_error($!)
- end
- end
- result.add_run
- yield(Test::Unit::TestCase::FINISHED, name)
- end
+ # This redefinition is unfortunate but test/unit shows us no alternative.
+ # Doubly unfortunate: hax to support Mocha's hax.
+ def run(result)
+ return if @method_name.to_s == "default_test"
- # Doubly unfortunate: mocha does the same so we have to hax their hax.
- def run_with_callbacks_and_mocha(result)
- return if @method_name.to_s == "default_test"
+ if using_mocha = respond_to?(:mocha_verify)
+ assertion_counter = Mocha::TestCaseAdapter::AssertionCounter.new(result)
+ end
- assertion_counter = Mocha::TestCaseAdapter::AssertionCounter.new(result)
- yield(Test::Unit::TestCase::STARTED, name)
- @_result = result
- begin
+ yield(Test::Unit::TestCase::STARTED, name)
+ @_result = result
begin
- run_callbacks :setup
- setup
- __send__(@method_name)
- mocha_verify(assertion_counter)
- rescue Mocha::ExpectationError => e
- add_failure(e.message, e.backtrace)
- rescue Test::Unit::AssertionFailedError => e
- add_failure(e.message, e.backtrace)
- rescue Exception
- raise if Test::Unit::TestCase::PASSTHROUGH_EXCEPTIONS.include? $!.class
- add_error($!)
- ensure
begin
- teardown
- run_callbacks :teardown, :enumerator => :reverse_each
+ run_callbacks :setup
+ setup
+ __send__(@method_name)
+ mocha_verify(assertion_counter) if using_mocha
+ rescue Mocha::ExpectationError => e
+ add_failure(e.message, e.backtrace)
rescue Test::Unit::AssertionFailedError => e
add_failure(e.message, e.backtrace)
- rescue Exception
- raise if Test::Unit::TestCase::PASSTHROUGH_EXCEPTIONS.include? $!.class
- add_error($!)
+ rescue Exception => e
+ raise if PASSTHROUGH_EXCEPTIONS.include?(e.class)
+ add_error(e)
+ ensure
+ begin
+ teardown
+ run_callbacks :teardown, :enumerator => :reverse_each
+ rescue Test::Unit::AssertionFailedError => e
+ add_failure(e.message, e.backtrace)
+ rescue Exception => e
+ raise if PASSTHROUGH_EXCEPTIONS.include?(e.class)
+ add_error(e)
+ end
end
+ ensure
+ mocha_teardown if using_mocha
end
- ensure
- mocha_teardown
+ result.add_run
+ yield(Test::Unit::TestCase::FINISHED, name)
end
- result.add_run
- yield(Test::Unit::TestCase::FINISHED, name)
end
end
end

0 comments on commit e7208d3

Please sign in to comment.
Something went wrong with that request. Please try again.