Is there a way to test caching in ur app?

Rails gives use page,action and fragment caching. Is there anyway to
write tests to make sure the cache is being expired properly? I’m
interested to know what other developers out there feel about this.

For example, I was thinkin maybe I could enable caching in test mode,
clear the cache on unittest setup and then after certain actions, load
other pages and test the contents (to make sure that stale fragments
aren’t being used, etc).

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

On 10/2/06, [email protected] [email protected] wrote:

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 L.
http://AllTom.com/
http://GadgetLife.org/

On 10/2/06, David B. [email protected] wrote:

Tom L. wrote:

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.

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.


Rick O.
http://weblog.techno-weenie.net
http://mephistoblog.com

Tom L. wrote:

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.

That’s right, that’s what I meant. If I cache a fragment that displays
the latest users in my app for example, I could forget to expire the
cached fragment when new users are added to the system. Although thats a
silly example (cos if u remember to put in the unit test (if there was a
way), you would also remember to put in the controller), BUT
expire_fragment takes regular expressions so if you later change your
routes or you have complicated caching rules, testing wouldn’t be silly
at all.

On 10/3/06, Tom W. [email protected] wrote:

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 L.
http://AllTom.com/
http://GadgetLife.org/

On 03/10/06, Rick O. [email protected] wrote:

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

On 04/10/06, Tom L. [email protected] wrote:

On 10/3/06, Tom W. [email protected] wrote:

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

Any luck?

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

On 10/7/06, Tom W. [email protected] wrote:

end
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 L.
http://AllTom.com/
http://GadgetLife.org/

On 05/10/06, Tom W. [email protected] wrote:

On 04/10/06, Tom L. [email protected] wrote:

Any luck?

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.

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

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.


Rick O.
http://weblog.techno-weenie.net
http://mephistoblog.com

On 07/10/06, Tom L. [email protected] wrote:

assert_writes_fragment(:controller => :report, :action => :salary) do
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.

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

On 10/2/06, David B. [email protected] wrote:

Rails gives use page,action and fragment caching. Is there anyway to
write tests to make sure the cache is being expired properly? I’m
interested to know what other developers out there feel about this.

For example, I was thinkin maybe I could enable caching in test mode,
clear the cache on unittest setup and then after certain actions, load
other pages and test the contents (to make sure that stale fragments
aren’t being used, etc).

You could easily do this with Selenium:

http://www.openqa.org/

With Selenium you can write a simple DSL to click through the app via
one or more browsers with tests in ruby code e.g.

def test_expire_name_div
a=users(:admin)
login_as a
open user_profile_url
wait_for_page_to_load
assert_equal a.name, get_text(‘name’)
a.name = “bob”
a.save! #should expire fragment
refresh_page
assert_equal a.name, get_text(‘name’)
end

This is a good way to test across browsers, to test ajax, and can
provide easier ways to test some aspects of your app from the
browser(s) to the db. Think of it as programmatic acceptance testing
(write the DSL with the client even). They’re also an impressive
client demo (i.e. shiny whilst in operation).

pt.

Posted via http://www.ruby-forum.com/.


Parker T.
http://www.parkert.com/
510.541.0125

On Oct 9, 2006, at 10:19 PM, Parker T. wrote:

other pages and test the contents (to make sure that stale fragments
aren’t being used, etc).

You could easily do this with Selenium:

http://www.openqa.org/

With Selenium you can write a simple DSL to click through the app via
one or more browsers with tests in ruby code e.g.

This might be helpful:
http://agilewebdevelopment.com/plugins/page_cache_test


Building an e-commerce site with Rails?
http://www.agilewebdevelopment.com/rails-ecommerce

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs