From 3b9b3cbcd46fe2d0a6055345e219195bbcaa4ead Mon Sep 17 00:00:00 2001 From: Andrew Vit Date: Sun, 9 Mar 2014 20:42:07 -0700 Subject: [PATCH] Add specs for page caching matchers --- spec/controllers/page_caching_spec.rb | 42 +++++++++++++++++++ .../controllers/page_cache_spec_controller.rb | 32 ++++++++++++++ spec/spec_helper.rb | 27 ++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 spec/controllers/page_caching_spec.rb create mode 100644 spec/resources/controllers/page_cache_spec_controller.rb create mode 100644 spec/spec_helper.rb diff --git a/spec/controllers/page_caching_spec.rb b/spec/controllers/page_caching_spec.rb new file mode 100644 index 0000000..3278a95 --- /dev/null +++ b/spec/controllers/page_caching_spec.rb @@ -0,0 +1,42 @@ +require_relative '../spec_helper' +require_relative '../resources/controllers/page_cache_spec_controller' + +describe PageCacheSpecController, :type => :controller do + describe "response.should cache_page" do + + it "matches an action for a cached page" do + expect { + expect { get "action_with_page_caching" } + .to cache_page '/action_with_page_caching' + }.to_not raise_error + end + + it "does not match an action for an uncached page" do + expect { + expect { get "action_without_page_caching" } + .to cache_page '/action_without_page_caching' + }.to raise_error(RSpec::Expectations::ExpectationNotMetError) + end + + end + + describe "response.should expire_page" do + + it "matches an action for an expired page" do + expect { + get "action_with_page_caching" + expect { get "action_with_page_expiry" } + .to expire_page '/action_with_page_caching' + }.to_not raise_error + end + + it "does not match an action without expiry" do + expect { + expect { get "action_with_page_caching" } + .to expire_page '/action_with_page_caching' + }.to raise_error(RSpec::Expectations::ExpectationNotMetError) + end + + end + +end diff --git a/spec/resources/controllers/page_cache_spec_controller.rb b/spec/resources/controllers/page_cache_spec_controller.rb new file mode 100644 index 0000000..dd70861 --- /dev/null +++ b/spec/resources/controllers/page_cache_spec_controller.rb @@ -0,0 +1,32 @@ +class PageCacheSpecController < ActionController::Base + include Rails.application.routes.url_helpers + + caches_page :action_with_page_caching + + def action_without_page_caching + render :text => "" + end + + def action_with_page_caching + render :text => "" + end + + def action_with_page_expiry + expire_page :action => 'action_with_page_caching' + render :text => "" + end +end + +RSpec::Rails::Application.routes.draw do + get "action_without_page_caching", + :controller => "page_cache_spec", + :action => "action_without_page_caching" + + get "action_with_page_caching", + :controller => "page_cache_spec", + :action => "action_with_page_caching" + + get "action_with_page_expiry", + :controller => "page_cache_spec", + :action => "action_with_page_expiry" +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..69dcdee --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,27 @@ +require 'rails' +require 'action_controller/railtie' +unless Rails::VERSION::MAJOR < 4 + require 'action_controller/action_caching' + require 'action_controller/page_caching' +end +require 'rspec/rails' +require 'rspec-rails-caching' + +module RSpec::Rails + class Application < ::Rails::Application + config.secret_key_base = 'test' + config.action_controller.page_cache_directory = + File.dirname(__FILE__) + end +end + +RSpec.configure do |config| + config.before(:each) do + @real_world = RSpec.world + RSpec.instance_variable_set(:@world, RSpec::Core::World.new) + end + config.after(:each) do + RSpec.instance_variable_set(:@world, @real_world) + end + config.order = :random +end