<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ninajansen.dk &#187; rspec</title>
	<atom:link href="http://ninajansen.dk/category/rspec/feed/" rel="self" type="application/rss+xml" />
	<link>http://ninajansen.dk</link>
	<description></description>
	<lastBuildDate>Thu, 23 Apr 2009 21:46:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Testing caching with memcache, cache_fu and rspec</title>
		<link>http://ninajansen.dk/2008/05/21/testing-caching-with-memcache-cache_fu-and-spec/</link>
		<comments>http://ninajansen.dk/2008/05/21/testing-caching-with-memcache-cache_fu-and-spec/#comments</comments>
		<pubDate>Wed, 21 May 2008 14:06:24 +0000</pubDate>
		<dc:creator>Nina Jansen</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[memcache]]></category>
		<category><![CDATA[rspec]]></category>

		<guid isPermaLink="false">http://ninajansen.dk/?p=37</guid>
		<description><![CDATA[I use cache_fu for caching my rails application. I use a &#8220;fat models, skinny controllers strategy&#8221;. This means that my Author model has code like this:

def cached_books
get_cache(:author_books) do
Book.find_by_author_id(self.id)
end
end

This method caches all books belonging to an author. Cache fu does not cache belongs_to or has_(one&#124;many) relationships, and that&#8217;s why we have this method. But this also [...]]]></description>
			<content:encoded><![CDATA[<p>I use cache_fu for caching my rails application. I use a &#8220;fat models, skinny controllers strategy&#8221;. This means that my Author model has code like this:</p>
<p><code><br />
def cached_books<br />
get_cache(:author_books) do<br />
Book.find_by_author_id(self.id)<br />
end<br />
end<br />
</code></p>
<p>This method caches all books belonging to an author. Cache fu does not cache belongs_to or has_(one|many) relationships, and that&#8217;s why we have this method. But this also means that if we add a book to an author, we must also expire the author records cache. This can all be done with cache_fu. The problem is that these relationships might get quite complicated and it would be nice to be able to write some specs that test that everything behaves as intended.</p>
<p>Chris Wanstrath, the author of cache_fu, suggests using mocha to stub everything about, but didn&#8217;t want to emulate the caching behavior and then test if the behavior is right. That is fine if you test the cache_fu plugin, but not fine if you need to test if your application uses the cache_fu plugin correctly. What I wanted was to turn on caching while running my specs and then query the cache to see if things are actually cached properly.</p>
<p>This first this I tried was turning on caching in the &#8220;test&#8221; environment. Turns out that it is not a good idea, as a lot of specs go into some kind of recursive error when you do that. Caching should be off by default in test. I needed a way to turn on the cache functions in a given spec.</p>
<p>Long story short: for unit testing you add the following function to your model:</p>
<p><code><br />
class &lt;&lt; self<br />
def reenable_cache_for_test<br />
class &lt;&lt; self<br />
alias_method :fetch_cache, :fetch_cache_without_disabled<br />
alias_method :set_cache, :set_cache_without_disabled<br />
alias_method :expire_cache, :expire_cache_without_disabled<br />
end<br />
end<br />
end<br />
</code></p>
<p>You call this method in your spec like this:</p>
<p><code><br />
it "should cache author" do<br />
Author.reenable_cache_for_test</code><br />
<code><br />
CACHE.flush_all<br />
CACHE.get('Author:1').should == nil<br />
author = Author.get_cache(1)<br />
author.should != nil<br />
CACHE.get('Author:1').should == author<br />
</code><code><br />
CACHE.get('Author:1:books').should == nil<br />
books = user.cached_books<br />
CACHE.get('Author:1:books).should == books<br />
end<br />
</code></p>
<p>Fragment caching is a bit different, in you controller spec you do this:</p>
<p><code><br />
it "fragment should be cached on get" do<br />
ActionController::Base.perform_caching = true<br />
ActsAsCached.config.clear<br />
config = YAML.load_file( File.dirname(__FILE__) + '/../../vendor/plugins/cache_fu/defaults/memcached.yml.default')<br />
config['test'] = config['development'].merge('benchmarking' =&gt; false, 'disabled' =&gt; false)<br />
ActsAsCached.config = config<br />
ActsAsCached::FragmentCache.setup!</code><br />
<code><br />
CACHE.flush_all<br />
</code><br />
&#8230; and then do your cache checking.</p>
<img src="http://ninajansen.dk/?ak_action=api_record_view&id=37&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://ninajansen.dk/2008/05/21/testing-caching-with-memcache-cache_fu-and-spec/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
