Skip to content

Commit 3b9b3cb

Browse files
committed
Add specs for page caching matchers
1 parent 409cc67 commit 3b9b3cb

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

spec/controllers/page_caching_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
require_relative '../spec_helper'
2+
require_relative '../resources/controllers/page_cache_spec_controller'
3+
4+
describe PageCacheSpecController, :type => :controller do
5+
describe "response.should cache_page" do
6+
7+
it "matches an action for a cached page" do
8+
expect {
9+
expect { get "action_with_page_caching" }
10+
.to cache_page '/action_with_page_caching'
11+
}.to_not raise_error
12+
end
13+
14+
it "does not match an action for an uncached page" do
15+
expect {
16+
expect { get "action_without_page_caching" }
17+
.to cache_page '/action_without_page_caching'
18+
}.to raise_error(RSpec::Expectations::ExpectationNotMetError)
19+
end
20+
21+
end
22+
23+
describe "response.should expire_page" do
24+
25+
it "matches an action for an expired page" do
26+
expect {
27+
get "action_with_page_caching"
28+
expect { get "action_with_page_expiry" }
29+
.to expire_page '/action_with_page_caching'
30+
}.to_not raise_error
31+
end
32+
33+
it "does not match an action without expiry" do
34+
expect {
35+
expect { get "action_with_page_caching" }
36+
.to expire_page '/action_with_page_caching'
37+
}.to raise_error(RSpec::Expectations::ExpectationNotMetError)
38+
end
39+
40+
end
41+
42+
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class PageCacheSpecController < ActionController::Base
2+
include Rails.application.routes.url_helpers
3+
4+
caches_page :action_with_page_caching
5+
6+
def action_without_page_caching
7+
render :text => ""
8+
end
9+
10+
def action_with_page_caching
11+
render :text => ""
12+
end
13+
14+
def action_with_page_expiry
15+
expire_page :action => 'action_with_page_caching'
16+
render :text => ""
17+
end
18+
end
19+
20+
RSpec::Rails::Application.routes.draw do
21+
get "action_without_page_caching",
22+
:controller => "page_cache_spec",
23+
:action => "action_without_page_caching"
24+
25+
get "action_with_page_caching",
26+
:controller => "page_cache_spec",
27+
:action => "action_with_page_caching"
28+
29+
get "action_with_page_expiry",
30+
:controller => "page_cache_spec",
31+
:action => "action_with_page_expiry"
32+
end

spec/spec_helper.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require 'rails'
2+
require 'action_controller/railtie'
3+
unless Rails::VERSION::MAJOR < 4
4+
require 'action_controller/action_caching'
5+
require 'action_controller/page_caching'
6+
end
7+
require 'rspec/rails'
8+
require 'rspec-rails-caching'
9+
10+
module RSpec::Rails
11+
class Application < ::Rails::Application
12+
config.secret_key_base = 'test'
13+
config.action_controller.page_cache_directory =
14+
File.dirname(__FILE__)
15+
end
16+
end
17+
18+
RSpec.configure do |config|
19+
config.before(:each) do
20+
@real_world = RSpec.world
21+
RSpec.instance_variable_set(:@world, RSpec::Core::World.new)
22+
end
23+
config.after(:each) do
24+
RSpec.instance_variable_set(:@world, @real_world)
25+
end
26+
config.order = :random
27+
end

0 commit comments

Comments
 (0)