# should cache-money work with rails unit tests?

**URL:** <https://discuss.rubyonrails.org/t/should-cache-money-work-with-rails-unit-tests/47251>\
**Category:** rubyonrails-talk\
**Created:** [November 2, 2009, 3:51am UTC](https://discuss.rubyonrails.org/t/should-cache-money-work-with-rails-unit-tests/47251 "2009-11-02T03:51:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Con](https://avatars.discourse-cdn.com/v4/letter/c/9fc29f/32.png) [@Con](https://discuss.rubyonrails.org/u/Con)\
**Post date:** [November 2, 2009, 3:51am UTC](https://discuss.rubyonrails.org/t/should-cache-money-work-with-rails-unit-tests/47251/1 "2009-11-02T03:51:13Z")

</div>

> The Cache Money page ([http://github.com/nkallen/cache-money](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

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:** ![Con](https://avatars.discourse-cdn.com/v4/letter/c/9fc29f/32.png) [@Con](https://discuss.rubyonrails.org/u/Con)\
**Post date:** [November 2, 2009, 6:05am UTC](https://discuss.rubyonrails.org/t/should-cache-money-work-with-rails-unit-tests/47251/2 "2009-11-02T06:05:52Z")

</div>

> > The Cache Money page ([http://github.com/nkallen/cache-money](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.

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:** ![Tony5](https://avatars.discourse-cdn.com/v4/letter/t/e68b1a/32.png) [@Tony5](https://discuss.rubyonrails.org/u/Tony5)\
**Post date:** [November 2, 2009, 12:58pm UTC](https://discuss.rubyonrails.org/t/should-cache-money-work-with-rails-unit-tests/47251/3 "2009-11-02T12:58:10Z")

</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:** ![Con](https://avatars.discourse-cdn.com/v4/letter/c/9fc29f/32.png) [@Con](https://discuss.rubyonrails.org/u/Con)\
**Post date:** [November 2, 2009, 1:13pm UTC](https://discuss.rubyonrails.org/t/should-cache-money-work-with-rails-unit-tests/47251/4 "2009-11-02T13:13:17Z")

</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?

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:** ![Tony5](https://avatars.discourse-cdn.com/v4/letter/t/e68b1a/32.png) [@Tony5](https://discuss.rubyonrails.org/u/Tony5)\
**Post date:** [November 2, 2009, 3:16pm UTC](https://discuss.rubyonrails.org/t/should-cache-money-work-with-rails-unit-tests/47251/5 "2009-11-02T15:16:53Z")

</div>

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:** ![Con](https://avatars.discourse-cdn.com/v4/letter/c/9fc29f/32.png) [@Con](https://discuss.rubyonrails.org/u/Con)\
**Post date:** [November 3, 2009, 4:52am UTC](https://discuss.rubyonrails.org/t/should-cache-money-work-with-rails-unit-tests/47251/6 "2009-11-03T04:52:43Z")

</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?
> 
> > 
> 
> > 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)

> 

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
