Permalink
Browse files

nullify ivars after each example

  • Loading branch information...
1 parent dc684ae commit f6cb5923912f7598e544e04a1e6a45a0c8a37778 @dchelimsky dchelimsky committed Mar 28, 2011
Showing with 32 additions and 0 deletions.
  1. +3 −0 lib/rspec/core/example.rb
  2. +29 −0 spec/rspec/core/example_spec.rb
@@ -58,6 +58,9 @@ def run(example_group_instance, reporter)
rescue Exception => e
set_exception(e)
ensure
+ @example_group_instance.instance_variables.each do |ivar|
+ @example_group_instance.instance_variable_set(ivar, nil)
+ end
@example_group_instance = nil
begin
@@ -176,6 +176,35 @@ def assert(val)
"around (after)"
])
end
+
+ context "clearing ivars" do
+ it "sets ivars to nil to prep them for GC" do
+ group = RSpec::Core::ExampleGroup.describe do
+ before(:all) { @before_all = :before_all }
+ before(:each) { @before_each = :before_each }
+ after(:each) { @after_each = :after_each }
+ after(:all) { @after_all = :after_all }
+ end
+ example = group.example("does something") do
+ @in_example = :in_example
+ end
+ example_group_instance = group.new
+ example.run(example_group_instance, double('reporter').as_null_object)
+
+ %w[@before_all @before_each @after_each @after_all].each do |ivar|
+ example_group_instance.instance_variable_get(ivar).should be_nil
+ end
+ end
+
+ it "does not impact the before_all_ivars which are copied to each example" do
+ group = RSpec::Core::ExampleGroup.describe do
+ before(:all) { @before_all = "abc" }
+ example("first") { @before_all.should_not be_nil }
+ example("second") { @before_all.should_not be_nil }
+ end
+ group.run.should be_true
+ end
+ end
end
describe "#pending" do

0 comments on commit f6cb592

Please sign in to comment.