mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
SetupAndTeardown#teardown
should call any subsequent after_teardown:
If you have a regular test that have a teardown block, and for any reason an exception get raised, ActiveSupport will not run subsequent after_teardown method provided by other module or gems. One of them being the ActiveRecord::TestFixtures which won't rollback the transation when the test ends making all subsequent test to be in a weird state. The default implementation of minitest is to run all teardown methods from the user's test, rescue all exceptions, run all after_teardown methods provided by libraries and finally re-raise the exception that happened in the user's teardown method. Rails should do the same.
This commit is contained in:
parent
d514ce9199
commit
70fae9a434
2 changed files with 42 additions and 1 deletions
|
@ -44,8 +44,15 @@ module ActiveSupport
|
|||
end
|
||||
|
||||
def after_teardown # :nodoc:
|
||||
run_callbacks :teardown
|
||||
begin
|
||||
run_callbacks :teardown
|
||||
rescue => e
|
||||
error = e
|
||||
end
|
||||
|
||||
super
|
||||
ensure
|
||||
raise error if error
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
34
activesupport/test/testing/setup_and_teardown_test.rb
Normal file
34
activesupport/test/testing/setup_and_teardown_test.rb
Normal file
|
@ -0,0 +1,34 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "abstract_unit"
|
||||
|
||||
module OtherAfterTeardown
|
||||
def after_teardown
|
||||
@witness = true
|
||||
end
|
||||
end
|
||||
|
||||
class SetupAndTeardownTest < Minitest::Test
|
||||
include OtherAfterTeardown
|
||||
include ActiveSupport::Testing::SetupAndTeardown
|
||||
|
||||
attr_writer :witness
|
||||
|
||||
MyError = Class.new(StandardError)
|
||||
|
||||
teardown do
|
||||
raise MyError, "Test raises an error, all after_teardown should still get called"
|
||||
end
|
||||
|
||||
def after_teardown
|
||||
assert_raises MyError do
|
||||
super
|
||||
end
|
||||
|
||||
assert_equal true, @witness
|
||||
end
|
||||
|
||||
def test_teardown_raise_but_all_after_teardown_method_are_called
|
||||
assert true
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue