Skip to content

Commit

Permalink
nullify ivars after each example
Browse files Browse the repository at this point in the history
  • Loading branch information
dchelimsky committed Mar 29, 2011
1 parent dc684ae commit f6cb592
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/rspec/core/example.rb
Expand Up @@ -58,6 +58,9 @@ def run(example_group_instance, reporter)
rescue Exception => e rescue Exception => e
set_exception(e) set_exception(e)
ensure ensure
@example_group_instance.instance_variables.each do |ivar|
@example_group_instance.instance_variable_set(ivar, nil)
end
@example_group_instance = nil @example_group_instance = nil


begin begin
Expand Down
29 changes: 29 additions & 0 deletions spec/rspec/core/example_spec.rb
Expand Up @@ -176,6 +176,35 @@ def assert(val)
"around (after)" "around (after)"
]) ])
end 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 end


describe "#pending" do describe "#pending" do
Expand Down

0 comments on commit f6cb592

Please sign in to comment.