@@ -8,20 +8,21 @@ module BeforeAll
88 # store instance variables
99 module Minitest # :nodoc: all
1010 class Executor
11- attr_reader :active
11+ attr_reader :active , :block , :captured_ivars
1212
1313 alias active? active
1414
1515 def initialize ( &block )
1616 @block = block
17+ @captured_ivars = [ ]
1718 end
1819
19- def activate! ( test_class )
20- return if active?
20+ def activate! ( test_object )
21+ return restore_ivars ( test_object ) if active?
2122 @active = true
22- @examples_left = test_class . runnable_methods . size
23+ @examples_left = test_object . class . runnable_methods . size
2324 BeforeAll . begin_transaction do
24- capture!
25+ capture! ( test_object )
2526 end
2627 end
2728
@@ -33,16 +34,21 @@ def try_deactivate!
3334 BeforeAll . rollback_transaction
3435 end
3536
36- def capture!
37- instance_eval ( &@block )
37+ def capture! ( test_object )
38+ before_ivars = test_object . instance_variables
39+
40+ test_object . instance_eval ( &block )
41+
42+ ( test_object . instance_variables - before_ivars ) . each do |ivar |
43+ captured_ivars << [ ivar , test_object . instance_variable_get ( ivar ) ]
44+ end
3845 end
3946
40- def restore_to ( test_object )
41- instance_variables . each do |ivar |
42- next if ivar == :@block
47+ def restore_ivars ( test_object )
48+ captured_ivars . each do |( ivar , val ) |
4349 test_object . instance_variable_set (
4450 ivar ,
45- instance_variable_get ( ivar )
51+ val
4652 )
4753 end
4854 end
@@ -62,8 +68,7 @@ def before_all(&block)
6268
6369 prepend ( Module . new do
6470 def setup
65- self . class . before_all_executor . activate! ( self . class )
66- self . class . before_all_executor . restore_to ( self )
71+ self . class . before_all_executor . activate! ( self )
6772 super
6873 end
6974
0 commit comments