Created: 2008-04-17 16:46
Updated: 2017-07-27 04:49
License: mit


NOTE: This project is outdated. I have not used this plugin for a long time, nor has it been tested in the latest version of Rails and Rspec. Its use is not recommended. I keep the code online as a reminder of the code I wrote years ago, making the stuff I do today look good.

Rspec caching test plugin

This plugin helps you test your caching with rspec. It provides you with matchers to test if caching and expiring does what you want it to do. It is currently only aiming at functional (controller) specifications.

This is basic but useful stuff, extracted from one of my projects. These are the matchers you get:

  • lambda { ... }.should cache_page(url)
  • ... expire_page(url)
  • ... cache_action(action)
  • ... cache_fragment(name)
  • ... expire_action(action)
  • ... expire_fragment(name)

Note that cache_action and cache_fragment are the same thing, only cache_action turns your action argument into the right name using fragment_cache_key. See the Rails docs for more info.


This is almost a drop-in solution. You only need to set up this plugin's test hooks by calling its setup method, preferably in your spec_helper.rb file like so:



Consider the following example specification:

describe PostsController do
  describe "handling GET /posts" do
    it "should cache the page" do
      lambda { get :index }.should cache_page('/posts')

    it "should cache the RSS feed" do
      lambda { 
        get :index, :format => 'rss' 
      }.should cache_page('/posts.rss')

The cache_page matcher tests if your lambda actually triggers the caching.

describe "handling GET /users/1" do
  it "should cache the action" do
    lambda { 
      get :show, :id => 1, :user_id => 
    }.should cache_action(:show)

The cache_action takes a symbol for the action of the current controller to test for, or an entire Hash to be used with url_for.


  1. version 1.0 (I don't like beta)

    The plugin works but remains without a test suite. A bit rough around the edges, maybe.


  • Build full test suite.
  • Adapt for integration testing


Copyright (c) 2008 Arjan van der Gaag, released under the MIT license

Cookies help us deliver our services. By using our services, you agree to our use of cookies Learn more