# Should cache-money work with rails unit tests?

**URL:** <https://www.ruby-forum.com/t/should-cache-money-work-with-rails-unit-tests/178282>\
**Category:** Rails\
**Created:** [November 2, 2009, 4:16am UTC](https://www.ruby-forum.com/t/should-cache-money-work-with-rails-unit-tests/178282 "2009-11-02T04:16:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tnyplz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f17d59/32.png) [@tnyplz](https://www.ruby-forum.com/u/tnyplz)\
**Post date:** [November 2, 2009, 4:16am UTC](https://www.ruby-forum.com/t/should-cache-money-work-with-rails-unit-tests/178282/1 "2009-11-02T04:16:12Z")

</div>

The Cache Money page ([GitHub - nkallen/cache-money: A Write-Through Cacheing Library for ActiveRecord](http://github.com/nkallen/cache-money)) mentions

“For your unit tests, it is faster to use a Memcached mock than the real  
deal.”

Faster is fine but will it work with memcached? It doesn’t work with  
mine, I suspect the cache is not cleared between tests. I suspect this  
is just a documentation issue.

This is a great GEM but it’s sad that documentation corrections are on  
various forums. :-\

One other question. to get it to work I needed to require memcache in  
my initializer. Anyone know why this is? Here it is (pieced together  
from various forums)

require ‘memcache’  
require ‘cache\_money’  
if RAILS\_ENV == ‘test’  
$memcache = Cash::Mock.new  
else  
if RAILS\_ENV != ‘development’  
config = YAML.load(IO.read(File.join(RAILS\_ROOT, “config”,  
“memcached.yml”)))[RAILS\_ENV]  
$memcache = MemCache.new(config)  
$memcache.servers = config[‘servers’]

```
$local = Cash::Local.new($memcache)
$lock = Cash::Lock.new($memcache)
$cache = Cash::Transactional.new($local, $lock)

class ActiveRecord::Base
  is_cached :repository => $cache
end

```

else

# If we’re in development mode, we don’t want to

# deal with cacheing oddities, so let’s overrite

# cache-money’s #index method to do nothing…

```
class ActiveRecord::Base
  def self.index(*args)
  end
end

```

end  
end

---

<div class="post-metadata">

**Author:** ![tnyplz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f17d59/32.png) [@tnyplz](https://www.ruby-forum.com/u/tnyplz)\
**Post date:** [November 2, 2009, 4:52am UTC](https://www.ruby-forum.com/t/should-cache-money-work-with-rails-unit-tests/178282/2 "2009-11-02T04:52:50Z")

</div>

On Sun, Nov 1, 2009 at 7:16 PM, Tony P. \<  
removed\_email\_address@domain.invalid\> wrote:

> This is a great GEM but it’s sad that documentation corrections are on  
> else  
> class ActiveRecord::Base  
> end  
> end

Hi, I would recommend taking a look at the following for proper use of:

[http://github.com/ngmoco/cache-money/blob/master/spec/memcached\_wrapper\_test.rb](http://github.com/ngmoco/cache-money/blob/master/spec/memcached_wrapper_test.rb)

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![tnyplz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f17d59/32.png) [@tnyplz](https://www.ruby-forum.com/u/tnyplz)\
**Post date:** [November 2, 2009, 7:07am UTC](https://www.ruby-forum.com/t/should-cache-money-work-with-rails-unit-tests/178282/3 "2009-11-02T07:07:28Z")

</div>

On Sun, Nov 1, 2009 at 7:51 PM, Conrad T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > mine, I suspect the cache is not cleared between tests. I suspect this  
> > is just a documentation issue.

For properly clearing the cache before each test, you should reference  
the  
link below to create a suitable ‘setup’ and ‘teardown’ methods for your  
unit,  
functional, and integration tests.

> This is a great GEM but it’s sad that documentation corrections are on
> 
> > various forums. :-\
> > 
> > One other question. to get it to work I needed to require memcache in  
> > my initializer. Anyone know why this is? Here it is (pieced together  
> > from various forums)

In short, it’s part of the Rails application initialization process and  
it  
must be  
completed before the Rails application is ready for use. For example,  
if  
you take  
a look at the file in #{RAILS\_ROOT}/config/initializers, you’ll see  
other  
files  
that are part of the application initialization process. Furthermore,  
this  
process  
usually takes place after the loading of both plugins and gems.

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![tnyplz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f17d59/32.png) [@tnyplz](https://www.ruby-forum.com/u/tnyplz)\
**Post date:** [November 2, 2009, 1:59pm UTC](https://www.ruby-forum.com/t/should-cache-money-work-with-rails-unit-tests/178282/4 "2009-11-02T13:59:31Z")

</div>

Thanks for the pointers Conrad. It seems this project has several  
active forks on github. How does one go about choosing one? My hunch  
is to go with a fork that is still active but my fear is that it may  
diverge from what the original author intended and will become  
deprecated.

Any recommendations here?

---

<div class="post-metadata">

**Author:** ![tnyplz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f17d59/32.png) [@tnyplz](https://www.ruby-forum.com/u/tnyplz)\
**Post date:** [November 2, 2009, 4:20pm UTC](https://www.ruby-forum.com/t/should-cache-money-work-with-rails-unit-tests/178282/5 "2009-11-02T16:20:00Z")

</div>

On Nov 2, 8:13 am, Conrad T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi, I would recommend getting the version from
> 
> [http://www.gemcutter.org/gems/ngmoco-cache-money](http://www.gemcutter.org/gems/ngmoco-cache-money)
> 
> -Conrad

It looks like these newer versions don’t bother with the initializer  
making the setup easier (i guess test cases us the mock by default).

At the moment I am using ashleym1972-cache-money-0.2.9 as ngmoco gives  
the following error

gem install ngmoco-cache-money  
ERROR: could not find gem ngmoco-cache-money locally or in a  
repository

I have the following sources.  
gem sources  
\*\*\* CURRENT SOURCES \*\*\*

[http://gems.rubyforge.org/](http://gems.rubyforge.org/)  
[http://gems.github.com](http://gems.github.com)

---

<div class="post-metadata">

**Author:** ![tnyplz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f17d59/32.png) [@tnyplz](https://www.ruby-forum.com/u/tnyplz)\
**Post date:** [November 2, 2009, 2:15pm UTC](https://www.ruby-forum.com/t/should-cache-money-work-with-rails-unit-tests/178282/6 "2009-11-02T14:15:19Z")

</div>

On Mon, Nov 2, 2009 at 4:58 AM, Tony [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks for the pointers Conrad. It seems this project has several  
> active forks on github. How does one go about choosing one? My hunch  
> is to go with a fork that is still active but my fear is that it may  
> diverge from what the original author intended and will become  
> deprecated.
> 
> Any recommendations here?

Hi, I would recommend getting the version from

[http://www.gemcutter.org/gems/ngmoco-cache-money](http://www.gemcutter.org/gems/ngmoco-cache-money)

-Conrad

---

<div class="post-metadata">

**Author:** ![tnyplz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f17d59/32.png) [@tnyplz](https://www.ruby-forum.com/u/tnyplz)\
**Post date:** [November 3, 2009, 5:54am UTC](https://www.ruby-forum.com/t/should-cache-money-work-with-rails-unit-tests/178282/7 "2009-11-03T05:54:05Z")

</div>

On Mon, Nov 2, 2009 at 7:16 AM, Tony [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > deprecated.  
> > > making the setup easier (i guess test cases us the mock by default).  
> > > \*\*\* CURRENT SOURCES \*\*\*
> 
> [http://gems.rubyforge.org/](http://gems.rubyforge.org/)  
> [http://gems.github.com](http://gems.github.com)

Hi, you’ll need to install gemcutter gem and update your primary gem  
host by  
doing the following:

sudo gem install gemcutter  
gem tumble

Good luck,

-Conrad
