Skip to content

Instantly share code, notes, and snippets.

@cailinanne
Created November 28, 2011 03:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cailinanne/1398979 to your computer and use it in GitHub Desktop.
Save cailinanne/1398979 to your computer and use it in GitHub Desktop.
Testing cache clearing with Rails 3.1 and Rspec
#spec/requests/cache_book_spec.rb
require 'spec_helper'
describe "CacheBook" do
# Turn on caching
before do
ActionController::Base.perform_caching = true
ActionController::Base.cache_store = :file_store, "tmp/cache"
FileUtils.rm_rf(Dir['tmp/cache'])
end
it "show page cache is primed and cleared under correct cicumstances" do
book = FactoryGirl.create(:book,:title => "Fabulous Red Dog")
# 1. Start out with no cache
book_path.is_action_cached?.should be_false
# 2. Check to make sure that we do prime the cache if we visit the page
get book_path(book)
response.body.should have_content("Fabulous Red Dog")
response.should be_action_cached(request)
# 3. When a book is updated, check to make sure the cache is cleared
book.title = "Fabulous Blue Cat"
book.save!
book_path(book).is_action_cached?.should be_false
# 4. Visit the page again, make sure we see updated content and that the cache is re-primed
get book_path(book)
response.body.should have_content("Fabulous Blue Cat")
response.should be_action_cached(request)
end
# Turn off caching
after do
ActionController::Base.perform_caching = false
FileUtils.rm_rf(Dir['tmp/cache'])
end
end
@trappist
Copy link

trappist commented Apr 5, 2013

Rails.cache.clear > FileUtils.rm_rf

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