rspec-rails-caching 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm --create gemset use rspec-rails-caching
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rspec-rails-caching.gemspec
4
+ gemspec
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,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -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,4 @@
1
+ require 'rspec-rails-caching/matchers/cache'
2
+ require 'rspec-rails-caching/matchers/expire'
3
+ require 'rspec-rails-caching/matchers/cache_page'
4
+ require 'rspec-rails-caching/matchers/expire_page'
@@ -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,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
@@ -0,0 +1,15 @@
1
+ require 'rspec-rails-caching/matchers/base'
2
+
3
+ module RSpecRailsCaching::Matchers
4
+
5
+ caching_matcher :cache_page do
6
+ def cache_or_expire
7
+ "cache page"
8
+ end
9
+
10
+ def cache_results
11
+ cache_store.cached_pages
12
+ end
13
+ end
14
+
15
+ end
@@ -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
@@ -0,0 +1,15 @@
1
+ require 'rspec-rails-caching/matchers/base'
2
+
3
+ module RSpecRailsCaching::Matchers
4
+
5
+ caching_matcher :expire_page do
6
+ def cache_or_expire
7
+ "expire page"
8
+ end
9
+
10
+ def cache_results
11
+ cache_store.expired_pages
12
+ end
13
+ end
14
+
15
+ 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,3 @@
1
+ module RSpecRailsCaching
2
+ VERSION = "0.2.0"
3
+ 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: []