rspec-rails-caching 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rvmrc +1 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +46 -0
- data/Rakefile +2 -0
- data/lib/rspec-rails-caching.rb +32 -0
- data/lib/rspec-rails-caching/extensions.rb +1 -0
- data/lib/rspec-rails-caching/extensions/action_controller.rb +20 -0
- data/lib/rspec-rails-caching/matchers.rb +4 -0
- data/lib/rspec-rails-caching/matchers/base.rb +45 -0
- data/lib/rspec-rails-caching/matchers/cache.rb +13 -0
- data/lib/rspec-rails-caching/matchers/cache_page.rb +15 -0
- data/lib/rspec-rails-caching/matchers/expire.rb +11 -0
- data/lib/rspec-rails-caching/matchers/expire_page.rb +15 -0
- data/lib/rspec-rails-caching/test_store.rb +70 -0
- data/lib/rspec-rails-caching/version.rb +3 -0
- data/rspec-rails-caching.gemspec +21 -0
- metadata +117 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm --create gemset use rspec-rails-caching
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Andrew Vit
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# RSpec Rails Caching
|
2
|
+
|
3
|
+
Provides a cache store for recording and matchers for testing cache events in Rails controller tests.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'rspec-rails-caching'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install rspec-rails-caching
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Add `caching: true` as an option around a controller example group and use
|
22
|
+
a proc or lambda around the action for matching:
|
23
|
+
|
24
|
+
describe WidgetsController, caching: true do
|
25
|
+
it "should cache the show action" do
|
26
|
+
->{ get :show, id: 123 }.should cache_page('/widgets/123')
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should expire the update action" do
|
30
|
+
->{ put :update, id: 123 }.should expire_page('/widgets/123')
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
### Available matchers:
|
35
|
+
|
36
|
+
* `cache_page` / `expire_page`
|
37
|
+
* `cache_fragment` / `expire_fragment`
|
38
|
+
* `cache_action` / `expire_action`
|
39
|
+
|
40
|
+
## Contributing
|
41
|
+
|
42
|
+
1. Fork it
|
43
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
44
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
45
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
46
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'rspec-rails-caching/version'
|
2
|
+
require 'rspec-rails-caching/matchers'
|
3
|
+
require 'rspec-rails-caching/test_store'
|
4
|
+
require 'rspec-rails-caching/extensions/action_controller'
|
5
|
+
|
6
|
+
module RSpecRailsCaching
|
7
|
+
|
8
|
+
RSpec.configure do |config|
|
9
|
+
if ActionController::Base.perform_caching
|
10
|
+
silence_warnings do
|
11
|
+
Object.const_set :RAILS_CACHE, TestStore.new(true)
|
12
|
+
end
|
13
|
+
ActionController::Base.cache_store = RAILS_CACHE
|
14
|
+
ActionController::Base.class_eval do
|
15
|
+
extend Extensions::ActionController::ClassMethods
|
16
|
+
end
|
17
|
+
|
18
|
+
RSpec::Rails::ControllerExampleGroup.class_eval do
|
19
|
+
include Matchers
|
20
|
+
end
|
21
|
+
|
22
|
+
config.before :each do |example|
|
23
|
+
RAILS_CACHE.reset
|
24
|
+
end
|
25
|
+
|
26
|
+
config.after :each do |example|
|
27
|
+
RAILS_CACHE.reset
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'rspec-rails-caching/extensions/action_controller/base'
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module RSpecRailsCaching::Extensions
|
2
|
+
module ActionController
|
3
|
+
|
4
|
+
module ClassMethods
|
5
|
+
def cache_page(content, path, extension = nil, *)
|
6
|
+
instrument_page_cache :write_page, path do
|
7
|
+
cache_store.cached_pages << path
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def expire_page(path)
|
12
|
+
instrument_page_cache :expire_page, path do
|
13
|
+
cache_store.cached_pages.delete path
|
14
|
+
cache_store.expired_pages << path
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
@@ -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
|
@@ -0,0 +1,70 @@
|
|
1
|
+
module RSpecRailsCaching
|
2
|
+
class TestStore < ActiveSupport::Cache::Store
|
3
|
+
|
4
|
+
attr_reader :cached
|
5
|
+
attr_reader :expired
|
6
|
+
attr_reader :expiration_patterns
|
7
|
+
attr_reader :data
|
8
|
+
attr_accessor :read_cache
|
9
|
+
attr_reader :cached_pages
|
10
|
+
attr_reader :expired_pages
|
11
|
+
|
12
|
+
attr_accessor :context
|
13
|
+
|
14
|
+
def initialize(do_read_cache = false)
|
15
|
+
@data = {}
|
16
|
+
@cached = []
|
17
|
+
@expired = []
|
18
|
+
@expiration_patterns = []
|
19
|
+
@read_cache = do_read_cache
|
20
|
+
@cached_pages = []
|
21
|
+
@expired_pages = []
|
22
|
+
end
|
23
|
+
|
24
|
+
def reset
|
25
|
+
@data.clear
|
26
|
+
@cached.clear
|
27
|
+
@expired.clear
|
28
|
+
@expiration_patterns.clear
|
29
|
+
end
|
30
|
+
|
31
|
+
def read_entry(name, options = nil)
|
32
|
+
read_cache ? @data[name] : nil
|
33
|
+
end
|
34
|
+
|
35
|
+
def write_entry(name, value, options = nil)
|
36
|
+
@data[name] = value if read_cache
|
37
|
+
@cached << name
|
38
|
+
end
|
39
|
+
|
40
|
+
def delete_entry(name, options = nil)
|
41
|
+
@expired << name
|
42
|
+
end
|
43
|
+
|
44
|
+
def delete_matched(matcher, options = nil)
|
45
|
+
@expiration_patterns << matcher
|
46
|
+
end
|
47
|
+
|
48
|
+
def cached?(name)
|
49
|
+
@cached.include?(name)
|
50
|
+
end
|
51
|
+
|
52
|
+
def expired?(name)
|
53
|
+
@expired.include?(name) || @expiration_patterns.detect { |matcher| name =~ matcher }
|
54
|
+
end
|
55
|
+
|
56
|
+
def page_cached?(options = {})
|
57
|
+
@cached_pages.include?(test_cache_url(options))
|
58
|
+
end
|
59
|
+
|
60
|
+
def page_expired?(options = {})
|
61
|
+
@expired_pages.include?(test_cache_url(options))
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
def test_cache_url(options)
|
66
|
+
return options if options.is_a?(String)
|
67
|
+
url_for(options.merge(:only_path => true, :skip_relative_url_root => true))
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/rspec-rails-caching/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.name = "rspec-rails-caching"
|
6
|
+
gem.version = RSpecRailsCaching::VERSION
|
7
|
+
gem.authors = ["Andrew Vit"]
|
8
|
+
gem.email = ["andrew@avit.ca"]
|
9
|
+
gem.homepage = "https://github.com/avit/rspec-rails-caching"
|
10
|
+
gem.summary = %q{RSpec Rails Caching}
|
11
|
+
gem.description = %q{RSpec helper for testing page and action caching in Rails}
|
12
|
+
|
13
|
+
gem.add_dependency "rails", ">=3.0.0"
|
14
|
+
gem.add_dependency "rspec", ">=2.8.0"
|
15
|
+
gem.add_dependency "rspec-rails", ">=2.10.0"
|
16
|
+
|
17
|
+
gem.files = `git ls-files`.split($\)
|
18
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
20
|
+
gem.require_paths = ["lib"]
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rspec-rails-caching
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Andrew Vit
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-13 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.0.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 2.8.0
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 2.8.0
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec-rails
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 2.10.0
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.10.0
|
62
|
+
description: RSpec helper for testing page and action caching in Rails
|
63
|
+
email:
|
64
|
+
- andrew@avit.ca
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- .rvmrc
|
71
|
+
- Gemfile
|
72
|
+
- LICENSE
|
73
|
+
- README.md
|
74
|
+
- Rakefile
|
75
|
+
- lib/rspec-rails-caching.rb
|
76
|
+
- lib/rspec-rails-caching/extensions.rb
|
77
|
+
- lib/rspec-rails-caching/extensions/action_controller.rb
|
78
|
+
- lib/rspec-rails-caching/matchers.rb
|
79
|
+
- lib/rspec-rails-caching/matchers/base.rb
|
80
|
+
- lib/rspec-rails-caching/matchers/cache.rb
|
81
|
+
- lib/rspec-rails-caching/matchers/cache_page.rb
|
82
|
+
- lib/rspec-rails-caching/matchers/expire.rb
|
83
|
+
- lib/rspec-rails-caching/matchers/expire_page.rb
|
84
|
+
- lib/rspec-rails-caching/test_store.rb
|
85
|
+
- lib/rspec-rails-caching/version.rb
|
86
|
+
- rspec-rails-caching.gemspec
|
87
|
+
homepage: https://github.com/avit/rspec-rails-caching
|
88
|
+
licenses: []
|
89
|
+
post_install_message:
|
90
|
+
rdoc_options: []
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ! '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
segments:
|
100
|
+
- 0
|
101
|
+
hash: -591280762391301957
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
104
|
+
requirements:
|
105
|
+
- - ! '>='
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
segments:
|
109
|
+
- 0
|
110
|
+
hash: -591280762391301957
|
111
|
+
requirements: []
|
112
|
+
rubyforge_project:
|
113
|
+
rubygems_version: 1.8.25
|
114
|
+
signing_key:
|
115
|
+
specification_version: 3
|
116
|
+
summary: RSpec Rails Caching
|
117
|
+
test_files: []
|