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

Matthew Krom matthew.krom at gmail.com
Thu Apr 16 08:46:11 EDT 2009

Also, just noticed your class-caching isn't keyed off user.  (I'm also
honestly not sure what @ instead of @@ means inside a self. class method;
I'd have to look that up and write specs to test it!)

Something like this may work better.

@expiry_date_cache ||= {}
@expiry_date_cache[user.id] ||= find_if_expiry_date_for(user)

(Also Rails 2.something has a memoize feature which does key off
arguments--see the Railscast.  Not sure if it works for class methods).


On Thu, Apr 16, 2009 at 7:51 AM, 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

matthew.krom at gmail.com
617 852 5130
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20090416/57650836/attachment.html>

More information about the rspec-users mailing list