Skip to content

Commit 879065e

Browse files
committed
fix: execute Minitest before_all in the context of the test object
1 parent 03141e4 commit 879065e

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## master (unrealeased)
44

5+
- Execute Minitest `before_all` in the context of the current test object. ([@palkan][])
6+
57
## 0.12.1 (2020-09-01)
68

79
- Minor improvements.

lib/test_prof/recipes/minitest/before_all.rb

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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

spec/integrations/fixtures/minitest/before_all_fixture.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
prepend TransactionalMinitest
1313

1414
before_all do
15-
@user = TestProf::FactoryBot.create(:user)
15+
@user = TestProf::FactoryBot.create(:user, name: user_name)
16+
end
17+
18+
def user_name
19+
%w[Matroskin Sharik].sample
1620
end
1721

1822
def setup

0 commit comments

Comments
 (0)