[rspec-users] Testing discovered a problem in my code

Pat Maddox pat.maddox at gmail.com
Thu Apr 16 10:33:32 EDT 2009

You're setting an instance variable on a class, which is a global
variable and is not garbage collected. The state you set is maintained
across runs, screwing things up.

If you set config.cache_classes to false in environments/test.rb I
think it ought to reload the classes each time.

There's no way for rspec to know that you would want to clear that
variable, you have to do that yourself. This is one example of how
global variables suck.

Also your code doesn't make sense to me. I'd you called it twice, each
with different users, you would get the same result which is prob not
what you want.


On Thursday, April 16, 2009, Fernando Perez <lists at ruby-forum.com> wrote:
> When trying to test using sqlite in-memory in ran into a problem:
> - rake test raises an error on a test
> - running the failing test alone works perfectly.
> So what's the problem? here is the method giving the trouble:
> def self.expiry_date_for(user)
>   @expiry_date_cache ||= find_if_expiry_date_for(user)
> end
> That cached method is also called by another file, and therefore sets
> the @expiry_date_cache to some value therefore not acting correctly.
> So is my code flawed or is it the testing framework that doesn't clear
> correctly the cached variable? In production, would my code work
> correctly?
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

More information about the rspec-users mailing list