A Rails plug-in for testing page, action and fragment caching.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



Banker is a Rails plug-in to make it easy to test your application's page, action and fragment caching, and add more functionality to ActionView::Helpers::CacheHelper.

ActionView::Helpers::CacheHelper Additions

Presently, there is one new method added to ActionView::Helpers::CacheHelper.

  • conditionally_cache(expression, name, &block): works much like cache, except expression is evaluated first. When true, control is passed to cache as usual, but when false, the block is captured and output without caching anything.

Test Usage

In your config/environments/test.rb:

config.action_controller.perform_caching = true

In your functional tests:

assert_page_cached '/some-url'
assert_action_cached :index
assert_fragment_cached :controller => 'pages', :action => 'index', :part => 'fragment'

Page Caching/Expiration Assertions

These take a url_for-style Hash argument, a named route or a plain string.

  • assert_page_cached

  • assert_page_not_cached

  • assert_page_expired

  • assert_page_not_expired

Action Caching/Expiration Assertions

These take an action name (String or Symbol) as an argument.

  • assert_action_cached

  • assert_action_not_cached

  • assert_action_expired

  • assert_action_not_expired

Fragment Caching/Expiration Assertions

These take a url_for-style Hash argument.

  • assert_fragment_cached

  • assert_fragment_not_cached

  • assert_fragment_expired

  • assert_fragment_not_expired

Shoulda Macros

Should macros are provided for the most common assertions:

  • should_cache_page(description, &block)

  • should_not_cache_page(description, &block)

  • should_expire_page(description, &block)

  • should_not_expire_page(description, &block)

  • should_cache_action(description, action)

  • should_not_cache_action(description, action)

  • should_expire_action(description, action)

  • should_not_expire_action(description, action)

  • should_cache_fragment(description, key, &block)

  • should_not_cache_fragment(description, key, &block)

  • should_expire_fragment(description, key, &block)

  • should_not_expire_fragment(description, key, &block)

The action macros take an action argument in the same form as the action and fragment assertions. The page macros, like should_redirect_to, require that a named route be passed in a block, so that it is evaluated in the context of the test instance:

should_cache_page('home') { root_path }

The fragment macros will work either way: pass key if the name of your fragment is a literal or supply it with a block if you need to construct the key using variables only available in the context of the test instance.

Expiring a Cache from a Model or Script

It is sometimes useful to expire a cache from outside the context of a normal HTTP request, but Rails does not make this straightforward. See lightyearsoftware.com/blog/2009/09/running-sweepers-from-a-model for details.

To Do

  • Unit tests


Steve Madsen <steve@lightyearsoftware.com>

Copyright © 2009 Light Year Software, LLC

Released under the MIT license, see MIT-LICENSE for details.