Skip to content

Instantly share code, notes, and snippets.

@dgilperez
Created May 6, 2012 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgilperez/2623725 to your computer and use it in GitHub Desktop.
Save dgilperez/2623725 to your computer and use it in GitHub Desktop.
My attempt at testing a cached Page model
# spec/requests/page_cache_spec.rb
require 'spec_helper'
describe "Page cache" do
# Testing with local cache instead of Memcached for speed
before(:each) do
ActionController::Base.perform_caching = true
ActionController::Base.cache_store = :file_store, "tmp/cache"
FileUtils.rm_rf(Dir['tmp/cache'])
@page = create :page, :title => 'The blank page', :body => 'full of text I am, though', :permalink => 'im-a-page'
@user = create :user
end
after(:each) do
ActionController::Base.perform_caching = false
FileUtils.rm_rf(Dir['tmp/cache'])
end
it "does not prime the page cache if the user is logged in" do
page_path(@page).is_action_cached?.should be_false
login @user
get page_path(@page)
response.status.should be(200)
response.body.should have_content 'The blank page'
response.body.should have_content @user.first_name
page_path(@page).is_action_cached?.should be_false
end
it "primes the page cache if visitor" do
page_path(@page).is_action_cached?.should be_false
get page_path(@page)
response.status.should be(200)
response.body.should have_content 'The blank page'
response.body.should_not have_content @user.first_name
page_path(@page).is_action_cached?.should be_true
request.path.is_action_cached?.should be_true
#response.should be_action_cached(request)
end
it "show page cache is primed and cleared under correct cicumstances" do
# 1. Start out with no cache
page_path(@page).is_action_cached?.should be_false
# 2. Check to make sure that we do prime the cache if we get the page
get page_path(@page)
response.body.should have_content("The blank page")
request.path.is_action_cached?.should be_true
# 3. When a page is updated, check to make sure the cache is cleared
@page.title = "The red page full of letters"
@page.save!
request.path.is_action_cached?.should be_false
# 4. get the page again, make sure we see updated content and that the cache is re-primed
get page_path(@page)
response.body.should have_content("The red page full of letters")
request.path.is_action_cached?.should be_true
end
def page_path(page)
"/paginas/#{page.to_param}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment