Created
March 31, 2012 18:53
-
-
Save kmayer/2267519 to your computer and use it in GitHub Desktop.
TDD Action Caching in Rails 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BrandsController < ApplicationController | |
caches_action :preview | |
cache_sweeper :brand_sweeper | |
def preview | |
end | |
def update | |
... | |
@brand.save |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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)?