Is there a way to test caching in ur app?

TBH, I'm quite willing to trust that the rails guys have written their own thorough unit tests on their caching system, and not bother. Sounds like a lot of effort which will probably prove to be unnessecary... that is, unless you're having some kind of problem and think that caching is at fault....? -Nathan

TBH, I'm quite willing to trust that the rails guys have written their own thorough unit tests on their caching system, and not bother. Sounds like a lot of effort which will probably prove to be unnessecary... that is, unless you're having some kind of problem and think that caching is at fault....?

It's not to test that cached versions are being served properly; it's to test that your own code is expiring caches at the right time. Considering how many different pages you may need to flush the cache for when saving a single object, and how many you expect not to be flushed, I would say that being able to test your sweeper code is a good thing.

If it's possible to test it easily, I'd like to know how. Unit tests for the sweeper class? How do you assert that a page or action or fragment is cached?

-Nathan

Sincerely,

Tom Lieber http://AllTom.com/ http://GadgetLife.org/

That stuff is difficult to test unfortunately. I use integration testing for that in Mephisto:

http://svn.techno-weenie.net/projects/mephisto/trunk/test/integration/caching_test.rb http://svn.techno-weenie.net/projects/mephisto/trunk/test/referenced_caching_test_helper.rb

I imagine you could write up some nifty mock fragment cacher that would make that testing easier though. That would be nifty.

I wrote something like this a few months ago - I'll see if I can dig it up this evening.

Tom

I wrote something like this a few months ago - I'll see if I can dig it up this evening.

Any luck?

Tom

Sincerely,

Tom Lieber http://AllTom.com/ http://GadgetLife.org/

Yeah, I found what I'd written, but it's a nasty hack, so I'm going to clean it up a little and make it into a plugin. I'll post it by the end of the day.

Tom

Slightly later than promised, here's the code, packaged as a plugin. Unfortunately http access to my svn repository is down, so I've created an archive:

http://dev.popdog.net/caching_test_helper.tar.gz

Basic usage is something like:

assert_writes_fragment(:controller => :report, :action => :salary) do   # some code end

assert_expires_fragment(:controller => :report, :action => :salary) do   # some code end

There's no need to clear/populate the cache with fragments before the assertion. Also, the code should work as well in a unit test as a functional one (useful if, like me, you expire fragments in a different process to the web dispatcher).

If anyone finds it useful, let me know,

Tom

Slightly later than promised, here's the code, packaged as a plugin. Unfortunately http access to my svn repository is down, so I've created an archive:

http://dev.popdog.net/caching_test_helper.tar.gz

Basic usage is something like:

assert_writes_fragment(:controller => :report, :action => :salary) do   # some code end

assert_expires_fragment(:controller => :report, :action => :salary) do   # some code end

There's no need to clear/populate the cache with fragments before the assertion. Also, the code should work as well in a unit test as a functional one (useful if, like me, you expire fragments in a different process to the web dispatcher).

If anyone finds it useful, let me know,

Thanks, this looks handy! I'm going to try it soon, and let you know if I come across anything odd.

Tom

Sincerely,

Tom Lieber http://AllTom.com/ http://GadgetLife.org/

I now have a version in subversion if you'd prefer. No significant changes to the fragment caching assertions, but some work towards page caching assertions. Available from:

http://svn.popdog.net/public/rails/plugins/caching_test_helper/tags/REL-0.1/

Tom

I now have a version in subversion if you'd prefer. No significant changes to the fragment caching assertions, but some work towards page caching assertions. Available from:

http://svn.popdog.net/public/rails/plugins/caching_test_helper/tags/REL-0.1/

Mine only handled page caching. This plugin looks much better.