Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
TDD Action Caching in Rails 3
class BrandSweeper < ActionController::Caching::Sweeper
observe Brand # Observers will introspect on the class, but Sweepers don't
def after_update(brand)
expire_action :controller => "brand", :action => :preview, :brand_id => brand.to_param
class BrandsController < ApplicationController
caches_action :preview
cache_sweeper :brand_sweeper
def preview
def update
describe BrandsController do
describe "caching" do
let(:brand) { Factory.create(:brand) }
let(:preview_cache_path) {'views/' + preview_brand_path(brand)}
around do |example|
caching, ActionController::Base.perform_caching = ActionController::Base.perform_caching, true
store, ActionController::Base.cache_store = ActionController::Base.cache_store, :memory_store
silence_warnings { Object.const_set "RAILS_CACHE", ActionController::Base.cache_store }
silence_warnings { Object.const_set "RAILS_CACHE", store }
ActionController::Base.cache_store = store
ActionController::Base.perform_caching = caching
it "should action cache #preview" do
BrandsController.caches_action :preview # must be recapitulated to get around load time weirdfullness
get :preview, :brand_id => brand.to_param
ActionController::Base.cache_store.exist?(preview_cache_path).should be_true
it "should clear the cache on #update" do
ActionController::Base.cache_store.write(preview_cache_path, 'CACHED ACTION')
put :update, id: brand.to_param, brand: {one: 'attribute', after: 'another'}
ActionController::Base.cache_store.exist?(sign_up_cache_path).should be_false

sadly, when I tried this the spec only worked in isolation.
Any new insights as to how-to test the key generated for an overriden :cache_path (re: caches_action)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment