bloudermilk
bloudermilk

Reputation: 18109

Clearing class instance variables in between Rspec examples

I'm writing specs for a gem of mine that extends ActiveRecord. One of the things it has to do is set a class instance variable like so:

class MyModel < ActiveRecord::Base
  @foo = "asd"
end

Right now when I set @foo in one it "should" {} it persists to the next one. I understand this is normal Ruby behavior but I thought RSpec had some magic that cleaned everything out in between specs. I'd like to know how I can re-use a single AR model for all my tests (since creating a bunch of tables would be a pain) while being sure that @foo is being cleared between each test. Do I need to do this manually?

Upvotes: 0

Views: 1899

Answers (2)

bloudermilk
bloudermilk

Reputation: 18109

I wound up generating a method in my helper class that generated new classes with Class.new, so I could be sure that nothing was being left over in between tests.

Upvotes: 1

apneadiving
apneadiving

Reputation: 115511

You should simply make good use of the after :each block.

after(:each) do 
  @foo = nil
end

Upvotes: 0

Related Questions