0

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?

1
  • "I thought RSpec had some magic that cleaned everything out in between specs" - can anyone comment on whether this is the case? Commented Oct 26, 2011 at 22:34

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

after(:each) do 
  @foo = nil
end

2 Comments

or even an around statement that remembers the value before the test and sets it back afterwards.
the author's instance variable is in the class under test, NOT in the test itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.