rspec/rspec-expectations — Issue #116: Instance Variables are persisted between specs 2.8.0
Author: nigelr
Created: 2012-02-09 08:53:17+00:00
State: open

Running a test suite, I found custom matchers were failing due to instance variables retaining there values between each example.

Here is a contrived example

````ruby
require "rspec"

describe "Forget me please" do
it "First Spec" do
puts "First"
"rspec".should forget_instance_variables
end
it "Second Spec" do
puts "Second"
"rspec".should forget_instance_variables
end
end

RSpec::Matchers.define :forget_instance_variables do
match do |expected|
p @an_instance_variable
@an_instance_variable = true
true
end
end
````
and here is the output (first using rspec 2.7.0 and then using 2.8.0). The True is remembered on the second spec run on 2.8.0.

````text
nigelr@ProPark ~/Rails/tmp/chain4 $ rspec spec/
First
nil
.Second
nil
.

Finished in 0.10964 seconds
2 examples, 0 failures
nigelr@ProPark ~/Rails/tmp/chain4 $ gem list rspec

*** LOCAL GEMS ***

rspec (2.7.0)
rspec-core (2.7.1, 2.6.4, 2.6.3, 2.5.1)
rspec-expectations (2.7.0, 2.6.0, 2.5.0)
rspec-mocks (2.7.0, 2.6.0, 2.5.0)
rspec-rails (2.6.1, 2.6.0, 2.5.0)
nigelr@ProPark ~/Rails/tmp/chain4 $ rspec spec/
First
nil
.Second
nil
.

Finished in 0.11027 seconds
2 examples, 0 failures
nigelr@ProPark ~/Rails/tmp/chain4 $ gem install rspec
Fetching: rspec-core-2.8.0.gem (100%)
Fetching: rspec-expectations-2.8.0.gem (100%)
Fetching: rspec-mocks-2.8.0.gem (100%)
Fetching: rspec-2.8.0.gem (100%)
Successfully installed rspec-core-2.8.0
Successfully installed rspec-expectations-2.8.0
Successfully installed rspec-mocks-2.8.0
Successfully installed rspec-2.8.0
4 gems installed
nigelr@ProPark ~/Rails/tmp/chain4 $ rspec spec/
First
nil
.Second
true
.

Finished in 0.00106 seconds
2 examples, 0 failures
````

This caused issues in a custom matcher with a chain feature which set an instance variable, the next spec that did not use that chain would fail as it has the instance variable set.