开发者

How to spec operations that rely on Memcached?

We have a Rails application that we test with RSpec. We want to spec operations that rely on Memcached. What is the best practice to do so?

I thought of doing this by stubbing all calls to Rails.cache. Is this a good idea?

As per @Pan Thomakos suggestion, I'm adding some additional details about one of the scenarios I'm trying to test:

We have the concept of accounts in our system, therefore on every request we retrieve the current user and the current account. Because there are not many accounts in the system, we keep them all in cache and retrieve them from there.

def self.find_by_slug(slug)  
  Rails.cache.fetch(Account.cache_key_for_slug(slug), :expires_in => 1.day) { super }  
end

For this reas开发者_StackOverflow中文版on, caching in this case isn't just a nice to have behavior, but the expected behavior and the thing I want to test. Therefore turning off caching won't do.


Test without stubbing IMHO!

The sequence would look like this:

  1. Cache.flush # or equivalent
  2. Cache.get(slug).shouldbe null # test cache is empty
  3. Method.find_by_slug(slug).should == 'some value' # test that method words
  4. Cache.get(slug).should == 'some value' # test that cache has value.

Personally, I believe if you have the resources on hand then stubbing SHOULD NOT be used. If you do not have the resources on hand (IE a 3rd party service) then stubbing SHOULD BE used.

The problem with stubbing, is that if you changed the code that you are stubbing, then you won't know if it breaks.

An example in this case would be if you switched from the standard memcache gem, to Dahli?, or some other memcache gem which handed cache misses by returning false, null or some other value differently. I mean really! Cache.set("my_key", false)! :)

A example for switching, would be to leave the ASCII protocol and move to the faster binary protocol.

Memcache is a cheap resource, you can set it up with 1 meg of ram to do this testing. I would even go as far as to say you can do the same for mysql. Anything bigger than mysql, then I would start leaning towards stubbing as the cost to "setup" those resources becomes significant. YMMV.

-daniel


It seems like if you're using Rails.cache.fetch directly your best option is to stub. But if you use the controller helpers (which are now in seperate gems in rails 4), I came across this gem which is helpful https://github.com/avit/rspec-rails-caching

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜