Skip to content
Browse files

Fixing test running to run the teardown_once block whenever the last …

…actual test in a given context is run, even if there are subsequent empty contexts.
  • Loading branch information...
1 parent 9673b23 commit ca6939836750b889fa51f036951b38bb41544adc @harob harob committed
Showing with 18 additions and 7 deletions.
  1. +18 −7 lib/scope.rb
View
25 lib/scope.rb
@@ -131,7 +131,7 @@ def recursively_run_setup_and_teardown(test_case_instance, test_name, contexts,
# The ensure block guarantees that this context's teardown blocks will be run, even in an exception
# is thrown in a descendant context or in the test itself.
test_case_instance.instance_eval(&outer_context.teardown) if outer_context.teardown
- if outer_context.should_run_teardown_once? test_name
+ if outer_context.name_of_last_test == test_name
test_case_instance.instance_eval(&outer_context.teardown_once) if outer_context.teardown_once
end
end
@@ -150,12 +150,23 @@ def ancestor_contexts
ancestors
end
- def should_run_teardown_once?(test_name)
- # This handling of the empty context is not strictly correct; we should really do a DFS of the context
- # tree to find the last actual test, before which the teardown_once block should be run.
- return false if tests_and_subcontexts.empty?
- return (test_name == tests_and_subcontexts.last) if tests_and_subcontexts.last.is_a? String
- return tests_and_subcontexts.last.should_run_teardown_once?(test_name)
+ # Returns the name of the last actual test within this context (including within its descendant contexts),
+ # or nil if none exists.
+ def name_of_last_test
+ unless defined? @name_of_last_test
+ @name_of_last_test = nil
+ tests_and_subcontexts.reverse.each do |test_or_context|
+ if test_or_context.is_a? String
+ @name_of_last_test = test_or_context
+ break
+ end
+ unless test_or_context.name_of_last_test.nil?
+ @name_of_last_test = test_or_context.name_of_last_test
+ break
+ end
+ end
+ end
+ @name_of_last_test
end
end
end

0 comments on commit ca69398

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