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.
Presently, there is one new method added to ActionView::Helpers::CacheHelper.
-
conditionally_cache(expression, name, &block)
: works much likecache
, exceptexpression
is evaluated first. When true, control is passed tocache
as usual, but when false, the block is captured and output without caching anything.
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'
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
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
These take a url_for
-style Hash argument.
-
assert_fragment_cached
-
assert_fragment_not_cached
-
assert_fragment_expired
-
assert_fragment_not_expired
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.
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.
-
Unit tests
Steve Madsen <[email protected]>
Copyright © 2009 Light Year Software, LLC
Released under the MIT license, see MIT-LICENSE for details.