forked from relevance/cache_test
-
Notifications
You must be signed in to change notification settings - Fork 1
/
README
139 lines (94 loc) · 3.75 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
= Cache Test Plugin
This plugin adds assertions in test cases to check the caching
logic of your application.
== 1. Installation
Unpack into the vendor/plugin and that should be it.
Add the following to your config/environments/test.rb:
config.cache_store = CacheTest::TestStore.new
== 2. Usage
=== Testing page caching
First create an integration test. Then, to test caching of the
"/pages/about" and "/pages/contact" pages, add a method like this:
def test_caching
assert_cache_pages("/pages/about", "/pages/contact")
end
The assert_cache_pages method will
- first make sure that the urls are not cached,
- execute a get on each request,
- assert that the corresponding cache files have been created.
You can also give a block to the assert_cache_pages method. Instead
of executing a get on each url, it will yield the urls. For example:
def test_caching
assert_cache_pages("/pages/about", "/pages/contact") do |url_about, url_contact|
post url_about
post url_contact
end
end
=== Testing expiration of pages
You will also certainly want (and that's really the most interesting
part) to check if your cached pages expires when the user is doing
some action. For that, here is the assert_expire method:
def test_expiring
assert_expire_pages("/news/list", "/news/show/1") do |*urls|
post "/news/delete/1"
end
end
Here the assert_expire_pages method will
- check that the urls are cached,
- execute the post request,
- and assert that the urls are no more cached.
This is great for testing your cache sweepers logic.
=== Testing action caching
To test caching of the "bar" action of the foo "controller"
in an integration test, do
assert_cache_actions(:controller => "foo", :action => "bar") do
get "/foo/bar"
end
The assert_cache_actions method will
- first make sure that the actions are not cached,
- yield the given block
- assert that the corresponding action fragment have been stored.
=== Testing expiration of actions
To check that some actions are expired, use the assert_expire_actions method:
assert_expire_actions(:controller => "foo", :action => "bar") do |*urls|
post "/foo/expire_cache"
end
Here the assert_expire_actions method will
- check that the actions fragments are cached,
- execute the post request,
- and assert that the fragments are no more cached.
In functional test, there can be only one controller, so you are
not required to give the :controller option and if they are no
parameters to the action, you can simply call
assert_cache_actions(:foo, :bar) do
get :bar
get :foo
end
== Testing fragments caching
To check that your fragments are cached when doing some action,
do
assert_cache_fragments(:controller => "foo", :action => "bar", :action_suffix => "baz") do
get "/foo/bar"
end
== Testing expiration of fragments
To check that your fragments are expired when doing some action,
do
assert_expire_fragments(:controller => "foo", :action => "bar", :action_suffix => "baz") do
get "/foo/expire"
end
== Testing interaction with Rails.cache
To avoid some of the key formatting applied to fragment and other ache keys, you
can also interact directly with the test cache store.
In the setup method for the test, add a:
cache_store.reset
Query the store with:
cache_store.deleted?( 'key' )
cache_store.written?( 'key' )
== 3. License
This plugin is licensed under the MIT license. Complete license text
is included in the MIT-LICENSE[link:files/MIT-LICENSE.html] file.
== 4. Author
This plugin was created by Damien Merenne <dam@cosinux.org> and first
released at http://blog.cosinux.org/pages/page-cache-test.
This plugin was updated to work with Rails 2.1.1 by Jason Rudolph. The
updated version is available at https://github.com/relevance/cache_test.