Skip to content

Instantly share code, notes, and snippets.

@kmayer
Created March 31, 2012 18:53
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmayer/2267519 to your computer and use it in GitHub Desktop.
Save kmayer/2267519 to your computer and use it in GitHub Desktop.
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
end
...
class BrandsController < ApplicationController
caches_action :preview
cache_sweeper :brand_sweeper
def preview
end
def update
...
@brand.save
describe BrandsController do
describe "caching" do
let(:brand) { Factory.create(:brand) }
let(:preview_cache_path) {'views/test.host' + 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 }
example.run
silence_warnings { Object.const_set "RAILS_CACHE", store }
ActionController::Base.cache_store = store
ActionController::Base.perform_caching = caching
end
it "should action cache #preview" do
Rails.cache.clear
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
end
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
end
end
end
@JayTeeSF
Copy link

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