Skip to content

Commit 9433078

Browse files
committed
Extract matchers to base class and subclasses
1 parent 68dd4e1 commit 9433078

File tree

7 files changed

+88
-71
lines changed

7 files changed

+88
-71
lines changed

lib/rspec-rails-caching.rb

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ module RSpecRailsCaching
99
end
1010

1111
RSpec.configure do |config|
12-
config.alias_it_should_behave_like_to :with_configuration, "with"
1312
config.before(:all, :type => :controller, :caching => true) do |example|
1413
silence_warnings do
1514
@_orig_cache_store = RAILS_CACHE
@@ -22,10 +21,13 @@ module RSpecRailsCaching
2221
# The controller needs to be reloaded to metaprogram the caches_page
2322
# callback with the perform_caching option turned on. There is no
2423
# reloading if the example controller isn't in the load paths, likely
25-
# because it was defined inline in the spec.
24+
# because it was defined inline in the spec or an AnonymousController.
2625
if ctrl_class_file =
2726
ActiveSupport::Dependencies.search_for_file(example.class.controller_class.to_s.underscore)
2827
then
28+
# FIXME: the controller's cache_page callbacks don't get triggered
29+
# when the test suite runs an outer example group (without caching:
30+
# true), but individual examples will pass.
2931
ActiveSupport::Dependencies.load(ctrl_class_file)
3032
end
3133

@@ -35,18 +37,13 @@ module RSpecRailsCaching
3537
end
3638
end
3739

38-
config.after(:all) do |example|
40+
config.after(:all, :type => :controller, :caching => true) do |example|
3941
silence_warnings do
4042
Object.const_set "RAILS_CACHE", @_orig_cache_store
4143
end
4244
ActionController::Base.cache_store = RAILS_CACHE
4345
ActionController::Base.perform_caching = false
4446
end
45-
46-
config.around(:each, :type => :controller, :caching => true) do |example|
47-
# This block does some voodoo to ensure the controller class gets
48-
# reloaded in the right context. I don't understand why it's necessary.
49-
end
5047
end
5148

5249
end

lib/rspec-rails-caching/matchers.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
module RSpecRailsCaching
2-
module Matchers
3-
end
4-
end
5-
1+
require 'rspec-rails-caching/matchers/cache'
2+
require 'rspec-rails-caching/matchers/expire'
63
require 'rspec-rails-caching/matchers/cache_page'
74
require 'rspec-rails-caching/matchers/expire_page'
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
module RSpecRailsCaching
2+
module Matchers
3+
extend RSpec::Matchers::DSL
4+
5+
def self.caching_matcher name, &block
6+
matcher name do
7+
match do |actual|
8+
actual = actual.call if actual.respond_to?(:call)
9+
Array(expected).all? { |e| cache_results.include?(e) }
10+
end
11+
12+
description do
13+
"#{cache_or_expire} #{expected.inspect}"
14+
end
15+
16+
failure_message_for_should_not do |actual|
17+
"Expected #{controller.class} not to #{cache_or_expire} #{expected.inspect} but got #{cache_results.inspect}"
18+
end
19+
20+
failure_message_for_should do |actual|
21+
"Expected #{controller.class} to #{cache_or_expire} #{expected.inspect} but got #{cache_results.inspect}"
22+
end
23+
24+
def controller
25+
matcher_execution_context.controller
26+
end
27+
28+
def cache_store
29+
controller.cache_store
30+
end
31+
32+
def cache_results
33+
fail NoMethodError, "Abstract method 'cache_results' to be defined in matcher"
34+
end
35+
36+
def cache_or_expire
37+
fail NoMethodError, "Abstract method 'cache_or_expire' to be defined in matcher"
38+
end
39+
40+
instance_eval &block
41+
42+
end
43+
end
44+
end
45+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require 'rspec-rails-caching/matchers/base'
2+
3+
module RSpecRailsCaching::Matchers
4+
5+
caching_matcher :cache do
6+
def cache_results
7+
cache_store.cached
8+
end
9+
end
10+
alias_method :cache_fragment, :cache
11+
alias_method :cache_action, :cache
12+
13+
end
Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,15 @@
1-
module RSpecRailsCaching::Matchers
2-
extend RSpec::Matchers::DSL
3-
4-
matcher :cache_page do |*expected|
5-
match do |actual|
6-
actual = actual.call if actual.respond_to?(:call)
7-
unless actual.is_a? ActionController::TestResponse
8-
raise ArgumentError("cache_page matcher expects a callable Proc or a TestResponse")
9-
end
10-
11-
Array(expected).all? { |e| cache_store.page_cached?(e) }
12-
end
1+
require 'rspec-rails-caching/matchers/base'
132

14-
failure_message_for_should do |actual|
15-
"expected #{controller.class} to cache: #{expected.inspect} but got #{cache_results.inspect}"
16-
end
17-
18-
failure_message_for_should_not do |actual|
19-
"expected #{controller.class} not to cache: #{expected.inspect} but got #{cache_results.inspect}"
20-
end
21-
22-
description do
23-
"cache page #{expected.inspect}"
24-
end
25-
26-
def controller
27-
matcher_execution_context.controller
28-
end
3+
module RSpecRailsCaching::Matchers
294

30-
def cache_store
31-
controller.cache_store
5+
caching_matcher :cache_page do
6+
def cache_or_expire
7+
"cache page"
328
end
339

3410
def cache_results
3511
cache_store.cached_pages
3612
end
3713
end
14+
3815
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require 'rspec-rails-caching/matchers/base'
2+
3+
module RSpecRailsCaching::Matchers
4+
5+
caching_matcher :expire do
6+
def cache_results
7+
cache_store.expired
8+
end
9+
end
10+
11+
end
Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,15 @@
1-
module RSpecRailsCaching::Matchers
2-
extend RSpec::Matchers::DSL
3-
4-
matcher :expire_page do |*expected|
5-
match do |actual|
6-
actual = actual.call if actual.respond_to?(:call)
7-
unless actual.is_a? ActionController::TestResponse
8-
raise ArgumentError("cache_page matcher expects a callable Proc or a TestResponse")
9-
end
10-
11-
Array(expected).all? { |e| cache_store.page_expired?(e) }
12-
end
1+
require 'rspec-rails-caching/matchers/base'
132

14-
failure_message_for_should do |actual|
15-
"expected #{controller.class} to expire: #{expected.inspect} but got #{cache_results.inspect}"
16-
end
17-
18-
failure_message_for_should_not do |actual|
19-
"expected #{controller.class} not to expire: #{expected.inspect} but got #{cache_results.inspect}"
20-
end
21-
22-
description do
23-
"expire page #{expected.inspect}"
24-
end
25-
26-
def controller
27-
matcher_execution_context.controller
28-
end
3+
module RSpecRailsCaching::Matchers
294

30-
def cache_store
31-
controller.cache_store
5+
caching_matcher :expire_page do
6+
def cache_or_expire
7+
"expire page"
328
end
339

3410
def cache_results
3511
cache_store.expired_pages
3612
end
3713
end
14+
3815
end

0 commit comments

Comments
 (0)