Skip to content

Instantly share code, notes, and snippets.

@avit
Created June 12, 2012 19:46
Show Gist options
  • Save avit/2919699 to your computer and use it in GitHub Desktop.
Save avit/2919699 to your computer and use it in GitHub Desktop.
Testing page caching
class PostsController < ApplicationController
caches_page :index
def index
render text: "html!"
end
end
require 'spec_helper'
describe PostsController do
describe "GET index", caching: true do
around do |example|
ActionController::Base.perform_caching = true
Rails.application.config.action_controller.perform_caching = true
example.run
ActionController::Base.perform_caching = false
Rails.application.config.action_controller.perform_caching = false
end
it "caches the index page" do
controller.class.perform_caching.should be_true # true.
controller.should_receive(:cache_page).and_return true
# ^ .with("html!", controller: "posts", action: "index")
get :index
end
end
end
@avit
Copy link
Author

avit commented Jun 12, 2012

Page Caching is implemented as an after_filter:

https://github.com/rails/rails/blob/3-2-stable/actionpack/lib/action_controller/caching/pages.rb#L122

I tested other after_filters, e.g.

after_filter :hit_me

def hit_me
  binding.pry
end

This gets called, but cache_page doesn't...

@avit
Copy link
Author

avit commented Jun 13, 2012

Turns out page caching only works when perform_caching is set in environments/test.rb.

Turning it on selectively the spec doesn't work: I suspect this is a reloading issue, even though checking perform_caching in the spec example returns true.

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