public
Last active

TDD Action Caching in Rails 3

  • Download Gist
brand_sweeper.rb
Ruby
1 2 3 4 5 6 7
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
...
brands_controller.rb
Ruby
1 2 3 4 5 6 7 8 9 10
class BrandsController < ApplicationController
caches_action :preview
cache_sweeper :brand_sweeper
 
def preview
end
 
def update
...
@brand.save
brands_controller_spec.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
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

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)?

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.