public
Description: To provide a cleaner, more modular, heavier spec'd, more blackbox'd cache engine for Merb
Clone URL: git://github.com/benschwarz/merb-cache.git
merb-cache / burn / spec / controller_spec.rb
100644 199 lines (159 sloc) 6.642 kb
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
require File.dirname(__FILE__) + '/spec_helper'
 
describe Merb::Cache do
  describe CacheSpecController, "controller class methods" do
        
    before(:each) do
      Object.class_eval{ remove_const("CacheSpecControllerActionsController") if defined?(CacheSpecControllerActionsController)}
      Object.class_eval("class CacheSpecControllerActionsController < Merb::Controller; def show; 'In Show'; end; end")
      @klass = CacheSpecControllerActionsController
      @controller = @klass.new({})
    end
    
    it{ @klass.should respond_to(:cache_action) }
    
    it "should setup a cacheing options in the classes cache store" do
      @klass.class_eval <<-Ruby
cache_action :show
Ruby
      action_caches = @klass.send(:_action_caches)
      action_caches.should be_a_kind_of(Hash)
      action_caches[:show].should_not be_nil
      action_caches[:show][:proc].should be_nil
      action_caches[:show][:options].should be_empty
    end
    
    it "should add the proc to the action cache store" do
      @klass.class_eval <<-Ruby
cache_action :show do
"Stuff"
end
Ruby
      ac = @klass.send(:_action_caches)
      ac.should be_a_kind_of(Hash)
      ac[:show][:proc].call.should =="Stuff"
    end
      
    it "should add a before filter for the given action" do
      @klass._before_filters.should be_empty
      @klass.class_eval <<-Ruby
cache_action :show
Ruby
      @klass._before_filters.any?{|filter, opts| opts == {:only => ["show"]} }.should be_true
    end
    
    it "should call _fetch_action_cache in the before_filter" do
      @klass.send(:cache_action, :show)
      @controller = @klass.new({})
      @controller.should_receive(:_fetch_action_cache)
      @controller.should_receive(:_set_action_cache)
      @controller._dispatch("show")
      @controller.body.should == "In Show"
    end
            
    it{ @klass.should respond_to(:cache_actions)}
    
    it "should add all the actions to via cache_action with the options" do
      pending
      @klass.should_receive(:cache_setup).twice
      @klass.class_eval <<-Ruby
cache_actions :show, :index, :expire_in => 100 do
"Stuff"
end
Ruby
      ac = @klass.send(:_action_caches)
      ac.should be_a_kind_of(Hash)
      ac[:show][:options].should == {:expire_in => 100}
      ac[:index][:options].should == {:expire_in => 100}
      ac[:show][:proc].call.should == "Stuff"
    end
    
    describe "_fetch_action_cache" do
      it "should implement a _fetch_action_cache private method" do
        @klass.private_instance_methods.should include("_fetch_action_cache")
      end
      
      it "should call Merb.cache.get('key')" do
        @klass.send(:cache_action, :show)
      end
    end
    
    describe "_set_action_cache" do
      it "should implement a _set_action_cache private method" do
        @klass.private_instance_methods.should include("_set_action_cache")
      end
    end
    
  end
 
  describe CacheSpecController, "controller instance methods" do
    
    before(:all) do
      Merb::Cache.setup_default
      @controller = CacheSpecController.new({})
      
      @route_params = {:controller => "My::Controller", :action => "show", :id => 4}
      @get_params = {:search => "some search params"}
      @controller.stub!(:params).and_return(@route_params.merge(@get_params))
    end
    
    it{ @controller.should respond_to(:expire_action)}
    
    it{ @controller.should respond_to(:expire_actions)}
    
    it{ @controller.should respond_to(:cached_action?)}
    
    it "should call the call the base key path on the controller class" do
      pending
      @controller.class.should
      @controller.cache_key.should_not be_nil
      @controller.cache_key
    end
    
    it "should be cached"
    it "should not be cached"
  end
  
  describe Merb::Cache::ControllerInstanceMethods do
      
    before(:all) do
      Merb::Cache.setup_default
      Merb::Cache.setup(:custom_cache, :memcached)
      @controller = CacheSpecController.new({})
      @cache = Merb::Cache[:default]
      @custom_cache = Merb::Cache[:custom_cache]
    end
    
    after(:all) do
      @controller = nil
      Merb::Cache.remove_active_cache!(:custom_cache)
    end
    
    describe "get method" do
      it{ @controller.should respond_to(:get)}
      
      it "should pass the call through to te cache 'get'" do
        @cache.should_receive(:get).with("key").and_return "key"
        @controller.get("key")
      end
      
      it "should pass the call through to the custom cache 'get'" do
        @custom_cache.should_receive(:get).with("key").and_return "key"
        @cache.should_not_receive(:get)
        @controller.get("key", :custom_cache)
      end
    end
    
    describe "put method" do
      it{ @controller.should respond_to(:put)}
      
      it "should pass the put method on to the default cache" do
        @cache.should_receive(:put).with("key", "value", an_instance_of(Integer))
        @controller.put("key", "value", 1)
      end
      
      it "should pass the put method to the custom cache" do
        @custom_cache.should_receive(:put).with("key", "value", an_instance_of(Integer))
        @controller.put("key", "value", 1, :custom_cache)
      end
      
      it "should cahnge the expiry from minutes to seconds" do
        expiry = mock("expiry")
        expiry.should_receive(:*).with(60).and_return(120)
        @cache.should_receive(:put).with("key", "value", 120)
        @controller.put("key", "value", expiry)
      end
      
    end
    
    describe "cached? method" do
      it{ @controller.should respond_to(:cached?) }
      
      it "should pass the onto the cached? method of the default store" do
        @cache.should_receive(:cached?).with("key").and_return true
        @controller.cached?("key")
      end
      
      it "should pass the onto the cached? method of the custom store" do
        @custom_cache.should_receive(:cached?).with("key").and_return true
        @controller.cached?("key", :custom_cache)
      end
    end
    
    describe "expire! method" do
      it{ @controller.should respond_to(:expire!)}
      
      it "should pass the call the default stores expire! method" do
        @cache.should_receive(:expire!).with("key").and_return true
        @controller.expire!("key")
      end
      
      it "should pass the call the custom stores expire! method" do
        @custom_cache.should_receive(:expire!).with("key").and_return true
        @controller.expire!("key", :custom_cache)
      end
    end
    
  end
 
end