Instance Variables are persisted between specs 2.8.0 #116

Closed
nigelr opened this Issue Feb 9, 2012 · 2 comments

Projects

None yet

2 participants

@nigelr

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

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.

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.

@dchelimsky
RSpec member

Dup of #104.

@dchelimsky dchelimsky closed this Feb 9, 2012
@nigelr

oops and sorry, should of looked at the closed items....
Have tried the latest version and works great, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment