active_remote-cached 1.0.0 → 1.1.1

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.
@@ -1,54 +1,65 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  class FindMethodClass
4
6
  include ::ActiveRemote::Cached
5
7
 
6
- def self.find; nil; end
7
- def self.search; nil; end
8
+ def self.find
9
+ nil
10
+ end
11
+
12
+ def self.search
13
+ nil
14
+ end
8
15
 
9
16
  cached_finders_for :foo, :expires_in => 500
10
17
  cached_finders_for :guid
11
18
  cached_finders_for :guid, :user_guid
12
- cached_finders_for [:user_guid, :client_guid]
13
- cached_finders_for [:derp, :user_guid, :client_guid]
19
+ cached_finders_for %i[user_guid client_guid]
20
+ cached_finders_for %i[derp user_guid client_guid]
14
21
  end
15
22
 
16
23
  describe FindMethodClass do
17
- describe "API" do
24
+ let(:versioned_prefix) do
25
+ "#{RUBY_ENGINE_VERSION}:#{ActiveSupport::VERSION::STRING}"
26
+ end
27
+
28
+ describe 'API' do
18
29
  it "creates 'cached_find_by_foo'" do
19
- FindMethodClass.must_respond_to("cached_find_by_foo")
30
+ expect(FindMethodClass).to respond_to(:cached_find_by_foo)
20
31
  end
21
32
 
22
33
  it "creates 'cached_find_by_guid'" do
23
- FindMethodClass.must_respond_to("cached_find_by_guid")
34
+ expect(FindMethodClass).to respond_to(:cached_find_by_guid)
24
35
  end
25
36
 
26
37
  it "creates 'cached_find_by_user_guid'" do
27
- FindMethodClass.must_respond_to("cached_find_by_user_guid")
38
+ expect(FindMethodClass).to respond_to(:cached_find_by_user_guid)
28
39
  end
29
40
 
30
41
  it "creates 'cached_find_by_user_guid_and_client_guid'" do
31
- FindMethodClass.must_respond_to("cached_find_by_user_guid_and_client_guid")
42
+ expect(FindMethodClass).to respond_to(:cached_find_by_user_guid_and_client_guid)
32
43
  end
33
44
 
34
45
  it "creates 'cached_find_by_client_guid_and_user_guid'" do
35
- FindMethodClass.must_respond_to("cached_find_by_client_guid_and_user_guid")
46
+ expect(FindMethodClass).to respond_to(:cached_find_by_client_guid_and_user_guid)
36
47
  end
37
48
 
38
49
  it "creates 'cached_find_by_derp_and_user_guid_and_client_guid'" do
39
- FindMethodClass.must_respond_to("cached_find_by_derp_and_user_guid_and_client_guid")
50
+ expect(FindMethodClass).to respond_to(:cached_find_by_derp_and_user_guid_and_client_guid)
40
51
  end
41
52
 
42
53
  it "creates 'cached_find_by_client_guid_and_derp_and_user_guid'" do
43
- FindMethodClass.must_respond_to("cached_find_by_client_guid_and_derp_and_user_guid")
54
+ expect(FindMethodClass).to respond_to(:cached_find_by_client_guid_and_derp_and_user_guid)
44
55
  end
45
56
 
46
57
  it "creates 'cached_find_by_client_guid_and_user_guid_and_derp'" do
47
- FindMethodClass.must_respond_to("cached_find_by_client_guid_and_user_guid_and_derp")
58
+ expect(FindMethodClass).to respond_to(:cached_find_by_client_guid_and_user_guid_and_derp)
48
59
  end
49
60
  end
50
61
 
51
- describe "#cached_find_by_guid" do
62
+ describe '#cached_find_by_guid' do
52
63
  before do
53
64
  ::ActiveRemote::Cached.cache(HashCache.new)
54
65
  ::ActiveRemote::Cached.default_options(:expires_in => 100)
@@ -58,50 +69,50 @@ describe FindMethodClass do
58
69
  ::ActiveRemote::Cached.default_options({})
59
70
  end
60
71
 
61
- it "executes find_by_guid when cached_find with guid called" do
62
- FindMethodClass.stub(:find, :hello) do
63
- FindMethodClass.cached_find(:guid => :guid).must_equal(:hello)
64
- end
72
+ it 'executes find_by_guid when cached_find with guid called' do
73
+ expect(FindMethodClass).to receive(:find).and_return(:hello)
74
+ expect(FindMethodClass.cached_find(:guid => :guid)).to eq(:hello)
65
75
  end
66
76
 
67
- it "executes the fetch block if not present in cache" do
68
- FindMethodClass.stub(:find, :hello) do
69
- FindMethodClass.cached_find_by_guid(:guid).must_equal(:hello)
70
- end
77
+ it 'executes the fetch block if not present in cache' do
78
+ expect(FindMethodClass).to receive(:find).and_return(:hello)
79
+ expect(FindMethodClass.cached_find_by_guid(:guid)).to eq(:hello)
71
80
  end
72
81
 
73
- it "merges the default options in for the fetch call" do
74
- ::ActiveRemote::Cached.cache.expects(:fetch).with([FindMethodClass.name, "#find", "guid"], :expires_in => 100).returns(:hello)
75
-
76
- FindMethodClass.stub(:find, :hello) do
77
- FindMethodClass.cached_find_by_guid(:guid).must_equal(:hello)
78
- end
82
+ it 'merges the default options in for the fetch call' do
83
+ expect(::ActiveRemote::Cached.cache).to receive(:fetch).with(
84
+ [versioned_prefix, FindMethodClass.name, '#find', 'guid'], { :expires_in => 100 }
85
+ ).and_return(:hello)
86
+ expect(FindMethodClass).not_to receive(:find)
87
+ expect(FindMethodClass.cached_find_by_guid(:guid)).to eq(:hello)
79
88
  end
80
89
 
81
- it "overrides the default options with local options for the fetch call" do
82
- ::ActiveRemote::Cached.cache.expects(:fetch).with([FindMethodClass.name, "#find", "guid"], :expires_in => 200).returns(:hello)
90
+ it 'overrides the default options with local options for the fetch call' do
91
+ expect(::ActiveRemote::Cached.cache).to receive(:fetch).with(
92
+ [versioned_prefix, FindMethodClass.name, '#find', 'guid'], { :expires_in => 200 }
93
+ ).and_return(:hello)
94
+ expect(FindMethodClass).not_to receive(:find)
83
95
 
84
- FindMethodClass.stub(:find, :hello) do
85
- FindMethodClass.cached_find_by_guid(:guid, :expires_in => 200).must_equal(:hello)
86
- end
96
+ expect(FindMethodClass.cached_find_by_guid(:guid, :expires_in => 200)).to eq(:hello)
87
97
  end
88
98
 
89
- describe "namespaced cache" do
99
+ describe 'namespaced cache' do
90
100
  before do
91
- ::ActiveRemote::Cached.default_options(:expires_in => 100, :namespace => "MyApp")
101
+ ::ActiveRemote::Cached.default_options(:expires_in => 100, :namespace => 'MyApp')
92
102
  end
93
103
 
94
- it "uses the namespace as a prefix to the cache key" do
95
- ::ActiveRemote::Cached.cache.expects(:fetch).with(["MyApp", FindMethodClass.name, "#find", "guid"], :expires_in => 100).returns(:hello)
104
+ it 'uses the namespace as a prefix to the cache key' do
105
+ expect(::ActiveRemote::Cached.cache).to receive(:fetch).with(
106
+ [versioned_prefix, 'MyApp', FindMethodClass.name, '#find', 'guid'], { :expires_in => 100 }
107
+ ).and_return(:hello)
96
108
 
97
- FindMethodClass.stub(:find, :hello) do
98
- FindMethodClass.cached_find_by_guid(:guid)
99
- end
109
+ expect(FindMethodClass).not_to receive(:find)
110
+ FindMethodClass.cached_find_by_guid(:guid)
100
111
  end
101
112
  end
102
113
  end
103
114
 
104
- describe "#cached_find_by_foo" do
115
+ describe '#cached_find_by_foo' do
105
116
  before do
106
117
  ::ActiveRemote::Cached.cache(HashCache.new)
107
118
  ::ActiveRemote::Cached.default_options(:expires_in => 100)
@@ -111,20 +122,22 @@ describe FindMethodClass do
111
122
  ::ActiveRemote::Cached.default_options({})
112
123
  end
113
124
 
114
- it "overrides the default options with cached_finder options for the fetch call" do
115
- ::ActiveRemote::Cached.cache.expects(:fetch).with([FindMethodClass.name, "#find", "foo"], :expires_in => 500).returns(:hello)
125
+ it 'overrides the default options with cached_finder options for the fetch call' do
126
+ expect(::ActiveRemote::Cached.cache).to receive(:fetch).with(
127
+ [versioned_prefix, FindMethodClass.name, '#find', 'foo'], { :expires_in => 500 }
128
+ ).and_return(:hello)
116
129
 
117
- FindMethodClass.stub(:find, :hello) do
118
- FindMethodClass.cached_find_by_foo(:foo).must_equal(:hello)
119
- end
130
+ expect(FindMethodClass).not_to receive(:find)
131
+ expect(FindMethodClass.cached_find_by_foo(:foo)).to eq(:hello)
120
132
  end
121
133
 
122
- it "overrides the cached_finder options with local options for the fetch call" do
123
- ::ActiveRemote::Cached.cache.expects(:fetch).with([FindMethodClass.name, "#find", "foo"], :expires_in => 200).returns(:hello)
134
+ it 'overrides the cached_finder options with local options for the fetch call' do
135
+ expect(::ActiveRemote::Cached.cache).to receive(:fetch).with(
136
+ [versioned_prefix, FindMethodClass.name, '#find', 'foo'], { :expires_in => 200 }
137
+ ).and_return(:hello)
124
138
 
125
- FindMethodClass.stub(:find, :hello) do
126
- FindMethodClass.cached_find_by_foo(:foo, :expires_in => 200).must_equal(:hello)
127
- end
139
+ expect(FindMethodClass).not_to receive(:find)
140
+ expect(FindMethodClass.cached_find_by_foo(:foo, :expires_in => 200)).to eq(:hello)
128
141
  end
129
142
  end
130
143
  end
@@ -1,63 +1,96 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  class SearchMethodClass
4
6
  include ::ActiveRemote::Cached
5
7
 
6
- def self.derp; nil; end
7
- def self.find; nil; end
8
- def self.search; nil; end
8
+ def self.derp
9
+ nil
10
+ end
11
+
12
+ def self.find
13
+ nil
14
+ end
15
+
16
+ def self.search
17
+ nil
18
+ end
9
19
 
10
20
  cached_finders_for :foo, :expires_in => 500
11
21
  cached_finders_for :guid
12
22
  cached_finders_for :guid, :user_guid
13
- cached_finders_for [:user_guid, :client_guid]
14
- cached_finders_for [:derp, :user_guid, :client_guid]
23
+ cached_finders_for %i[user_guid client_guid]
24
+ cached_finders_for %i[derp user_guid client_guid]
25
+ cached_finders_for %i[zippy123 alpha]
15
26
  end
16
27
 
17
28
  describe SearchMethodClass do
18
- describe "API" do
29
+ let(:versioned_prefix) do
30
+ "#{RUBY_ENGINE_VERSION}:#{ActiveSupport::VERSION::STRING}"
31
+ end
32
+
33
+ describe 'API' do
19
34
  it "creates 'cached_search_by_foo'" do
20
- SearchMethodClass.must_respond_to("cached_search_by_foo")
35
+ expect(SearchMethodClass).to respond_to(:cached_search_by_foo)
21
36
  end
22
37
 
23
38
  it "creates 'cached_search_by_foo!'" do
24
- SearchMethodClass.must_respond_to("cached_search_by_foo!")
39
+ expect(SearchMethodClass).to respond_to(:cached_search_by_foo!)
25
40
  end
26
41
 
27
42
  it "creates 'cached_search_by_guid'" do
28
- SearchMethodClass.must_respond_to("cached_search_by_guid")
43
+ expect(SearchMethodClass).to respond_to(:cached_search_by_guid)
29
44
  end
30
45
 
31
46
  it "creates 'cached_search_by_user_guid'" do
32
- SearchMethodClass.must_respond_to("cached_search_by_user_guid")
47
+ expect(SearchMethodClass).to respond_to(:cached_search_by_user_guid)
33
48
  end
34
49
 
35
50
  it "creates 'cached_search_by_user_guid_and_client_guid'" do
36
- SearchMethodClass.must_respond_to("cached_search_by_user_guid_and_client_guid")
51
+ expect(SearchMethodClass).to respond_to(:cached_search_by_user_guid_and_client_guid)
37
52
  end
38
53
 
39
54
  it "creates 'cached_search_by_client_guid_and_user_guid'" do
40
- SearchMethodClass.must_respond_to("cached_search_by_client_guid_and_user_guid")
55
+ expect(SearchMethodClass).to respond_to(:cached_search_by_client_guid_and_user_guid)
41
56
  end
42
57
 
43
58
  it "creates 'cached_search_by_derp_and_user_guid_and_client_guid'" do
44
- SearchMethodClass.must_respond_to("cached_search_by_derp_and_user_guid_and_client_guid")
59
+ expect(SearchMethodClass).to respond_to(:cached_search_by_derp_and_user_guid_and_client_guid)
45
60
  end
46
61
 
47
62
  it "creates 'cached_search_by_client_guid_and_derp_and_user_guid'" do
48
- SearchMethodClass.must_respond_to("cached_search_by_client_guid_and_derp_and_user_guid")
63
+ expect(SearchMethodClass).to respond_to(:cached_search_by_client_guid_and_derp_and_user_guid)
49
64
  end
50
65
 
51
66
  it "creates 'cached_search_by_client_guid_and_user_guid_and_derp'" do
52
- SearchMethodClass.must_respond_to("cached_search_by_client_guid_and_user_guid_and_derp")
67
+ expect(SearchMethodClass).to respond_to(:cached_search_by_client_guid_and_user_guid_and_derp)
53
68
  end
54
69
 
55
70
  it "creates 'cached_search_by_client_guid_and_user_guid_and_derp!'" do
56
- SearchMethodClass.must_respond_to("cached_search_by_client_guid_and_user_guid_and_derp!")
71
+ expect(SearchMethodClass).to respond_to(:cached_search_by_client_guid_and_user_guid_and_derp!)
72
+ end
73
+
74
+ # Ensure it works with numbers in method name
75
+ it "creates 'cached_search_by_alpha_and_zippy123'" do
76
+ expect(SearchMethodClass).to respond_to(:cached_search_by_alpha_and_zippy123)
77
+ end
78
+
79
+ # Ensure it responds to methods where params are not sorted
80
+ # i.e. the method missing override is used.
81
+ it "responds to 'cached_search_by_zippy123_and_alpha'" do
82
+ expect(SearchMethodClass).to respond_to(:cached_search_by_zippy123_and_alpha)
83
+ end
84
+
85
+ # Ensure the right method actually gets called when
86
+ # method_missing override is used.
87
+ it "can call 'cached_search_by_zippy123_and_alpha' when not defined" do
88
+ expect(SearchMethodClass).to receive(:search)
89
+ SearchMethodClass.cached_search_by_zippy123_and_alpha('abc', '123')
57
90
  end
58
91
  end
59
92
 
60
- describe "#cached_search_by_guid" do
93
+ describe '#cached_search_by_guid' do
61
94
  before do
62
95
  ::ActiveRemote::Cached.cache(HashCache.new)
63
96
  ::ActiveRemote::Cached.default_options(:expires_in => 100)
@@ -67,108 +100,105 @@ describe SearchMethodClass do
67
100
  ::ActiveRemote::Cached.default_options({})
68
101
  end
69
102
 
70
- it "executes the search block when a block is passed" do
71
- SearchMethodClass.stub(:derp, :derp) do
72
- SearchMethodClass.cached_search(:guid => :guid) do
73
- SearchMethodClass.derp
74
- end.must_equal(:derp)
75
- end
103
+ it 'executes the search block when a block is passed' do
104
+ expect(SearchMethodClass).to receive(:derp).and_return(:derp)
105
+ expect(SearchMethodClass.cached_search(:guid => :guid) do
106
+ SearchMethodClass.derp
107
+ end).to eq(:derp)
76
108
  end
77
109
 
78
- it "does not persist empty values by default" do
79
- SearchMethodClass.stub(:derp, []) do
80
- SearchMethodClass.cached_search(:guid => :guid) do
81
- SearchMethodClass.derp
82
- end
110
+ it 'does not persist empty values by default' do
111
+ expect(SearchMethodClass).to receive(:derp).and_return([])
83
112
 
84
- SearchMethodClass.cached_exist_search_by_guid?(:guid).must_equal(false)
113
+ SearchMethodClass.cached_search(:guid => :guid) do
114
+ SearchMethodClass.derp
85
115
  end
86
- end
87
116
 
88
- it "persists empty values when allow_empty sent" do
89
- SearchMethodClass.stub(:derp, []) do
90
- SearchMethodClass.cached_search({:guid => :guid}, :allow_empty => true) do
91
- SearchMethodClass.derp
92
- end
117
+ expect(SearchMethodClass.cached_exist_search_by_guid?(:guid)).to eq(false)
118
+ end
93
119
 
94
- SearchMethodClass.cached_exist_search_by_guid?(:guid).must_equal(true)
120
+ it 'persists empty values when allow_empty sent' do
121
+ expect(SearchMethodClass).to receive(:derp).and_return([])
122
+ SearchMethodClass.cached_search({ :guid => :guid }, :allow_empty => true) do
123
+ SearchMethodClass.derp
95
124
  end
96
- end
97
125
 
98
- it "does not persist nil values by default" do
99
- SearchMethodClass.stub(:derp, nil) do
100
- SearchMethodClass.cached_search(:guid => :guid) do
101
- SearchMethodClass.derp
102
- end
126
+ expect(SearchMethodClass.cached_exist_search_by_guid?(:guid)).to eq(true)
127
+ end
103
128
 
104
- SearchMethodClass.cached_exist_search_by_guid?(:guid).must_equal(false)
129
+ it 'does not persist nil values by default' do
130
+ expect(SearchMethodClass).to receive(:derp).and_return(nil)
131
+ SearchMethodClass.cached_search(:guid => :guid) do
132
+ SearchMethodClass.derp
105
133
  end
106
- end
107
134
 
108
- it "persists nil values when allow_nil sent" do
109
- SearchMethodClass.stub(:derp, nil) do
110
- SearchMethodClass.cached_search({:guid => :guid}, :allow_nil => true) do
111
- SearchMethodClass.derp
112
- end
135
+ expect(SearchMethodClass.cached_exist_search_by_guid?(:guid)).to eq(false)
136
+ end
113
137
 
114
- SearchMethodClass.cached_exist_search_by_guid?(:guid).must_equal(true)
138
+ it 'persists nil values when allow_nil sent' do
139
+ expect(SearchMethodClass).to receive(:derp).and_return(nil)
140
+ SearchMethodClass.cached_search({ :guid => :guid }, :allow_nil => true) do
141
+ SearchMethodClass.derp
115
142
  end
116
- end
117
143
 
118
- it "does persist non nil values" do
119
- SearchMethodClass.stub(:derp, :derp) do
120
- SearchMethodClass.cached_search(:guid => :guid) do
121
- SearchMethodClass.derp
122
- end
144
+ expect(SearchMethodClass.cached_exist_search_by_guid?(:guid)).to eq(true)
145
+ end
123
146
 
124
- SearchMethodClass.cached_exist_search_by_guid?(:guid).must_equal(true)
147
+ it 'does persist non nil values' do
148
+ expect(SearchMethodClass).to receive(:derp).and_return(:derp)
149
+ SearchMethodClass.cached_search(:guid => :guid) do
150
+ SearchMethodClass.derp
125
151
  end
152
+
153
+ expect(SearchMethodClass.cached_exist_search_by_guid?(:guid)).to eq(true)
126
154
  end
127
155
 
128
- it "executes search_by_guid when cached_search with guid called" do
129
- FindMethodClass.stub(:search, :hello) do
130
- FindMethodClass.cached_search(:guid => :guid).must_equal(:hello)
131
- end
156
+ it 'executes search_by_guid when cached_search with guid called' do
157
+ expect(SearchMethodClass).to receive(:search).and_return(:hello)
158
+
159
+ expect(SearchMethodClass.cached_search(:guid => :guid)).to eq(:hello)
132
160
  end
133
161
 
134
- it "executes the fetch block if not present in cache" do
135
- SearchMethodClass.stub(:search, :hello) do
136
- SearchMethodClass.cached_search_by_guid(:guid).must_equal(:hello)
137
- end
162
+ it 'executes the fetch block if not present in cache' do
163
+ expect(SearchMethodClass).to receive(:search).and_return(:hello)
164
+ expect(SearchMethodClass.cached_search_by_guid(:guid)).to eq(:hello)
138
165
  end
139
166
 
140
- it "merges the default options in for the fetch call" do
141
- ::ActiveRemote::Cached.cache.expects(:fetch).with([SearchMethodClass.name, "#search", "guid"], :expires_in => 100).returns(:hello)
167
+ it 'merges the default options in for the fetch call' do
168
+ expect(::ActiveRemote::Cached.cache).to receive(:fetch).with(
169
+ [versioned_prefix, SearchMethodClass.name, '#search', 'guid'], { :expires_in => 100 }
170
+ ).and_return(:hello)
142
171
 
143
- SearchMethodClass.stub(:search, :hello) do
144
- SearchMethodClass.cached_search_by_guid(:guid).must_equal(:hello)
145
- end
172
+ expect(SearchMethodClass).not_to receive(:search)
173
+ expect(SearchMethodClass.cached_search_by_guid(:guid)).to eq(:hello)
146
174
  end
147
175
 
148
- it "overrides the default options with local options for the fetch call" do
149
- ::ActiveRemote::Cached.cache.expects(:fetch).with([SearchMethodClass.name, "#search", "guid"], :expires_in => 200).returns(:hello)
176
+ it 'overrides the default options with local options for the fetch call' do
177
+ expect(::ActiveRemote::Cached.cache).to receive(:fetch).with(
178
+ [versioned_prefix, SearchMethodClass.name, '#search', 'guid'], { :expires_in => 200 }
179
+ ).and_return(:hello)
150
180
 
151
- SearchMethodClass.stub(:search, :hello) do
152
- SearchMethodClass.cached_search_by_guid(:guid, :expires_in => 200).must_equal(:hello)
153
- end
181
+ expect(SearchMethodClass).not_to receive(:search)
182
+ expect(SearchMethodClass.cached_search_by_guid(:guid, { :expires_in => 200 })).to eq(:hello)
154
183
  end
155
184
 
156
- describe "namespaced cache" do
185
+ describe 'namespaced cache' do
157
186
  before do
158
- ::ActiveRemote::Cached.default_options(:expires_in => 100, :namespace => "MyApp")
187
+ ::ActiveRemote::Cached.default_options(:expires_in => 100, :namespace => 'MyApp')
159
188
  end
160
189
 
161
- it "uses the namespace as a prefix to the cache key" do
162
- ::ActiveRemote::Cached.cache.expects(:fetch).with(["MyApp", SearchMethodClass.name, "#search", "guid"], :expires_in => 100).returns(:hello)
190
+ it 'uses the namespace as a prefix to the cache key' do
191
+ expect(::ActiveRemote::Cached.cache).to receive(:fetch).with(
192
+ [versioned_prefix, 'MyApp', SearchMethodClass.name, '#search', 'guid'], { :expires_in => 100 }
193
+ ).and_return(:hello)
163
194
 
164
- SearchMethodClass.stub(:search, :hello) do
165
- SearchMethodClass.cached_search_by_guid(:guid)
166
- end
195
+ expect(SearchMethodClass).not_to receive(:search)
196
+ SearchMethodClass.cached_search_by_guid(:guid)
167
197
  end
168
198
  end
169
199
  end
170
200
 
171
- describe "#cached_search_by_foo" do
201
+ describe '#cached_search_by_foo' do
172
202
  before do
173
203
  ::ActiveRemote::Cached.cache(HashCache.new)
174
204
  ::ActiveRemote::Cached.default_options(:expires_in => 100)
@@ -178,24 +208,26 @@ describe SearchMethodClass do
178
208
  ::ActiveRemote::Cached.default_options({})
179
209
  end
180
210
 
181
- it "overrides the default options with cached_finder options for the fetch call" do
182
- ::ActiveRemote::Cached.cache.expects(:fetch).with([SearchMethodClass.name, "#search", "foo"], :expires_in => 500).returns(:hello)
211
+ it 'overrides the default options with cached_finder options for the fetch call' do
212
+ expect(::ActiveRemote::Cached.cache).to receive(:fetch).with(
213
+ [versioned_prefix, SearchMethodClass.name, '#search', 'foo'], { :expires_in => 500 }
214
+ ).and_return(:hello)
183
215
 
184
- SearchMethodClass.stub(:find, :hello) do
185
- SearchMethodClass.cached_search_by_foo(:foo).must_equal(:hello)
186
- end
216
+ expect(SearchMethodClass).not_to receive(:find)
217
+ expect(SearchMethodClass.cached_search_by_foo(:foo)).to eq(:hello)
187
218
  end
188
219
 
189
- it "overrides the cached_finder options with local options for the fetch call" do
190
- ::ActiveRemote::Cached.cache.expects(:fetch).with([SearchMethodClass.name, "#search", "foo"], :expires_in => 200).returns(:hello)
220
+ it 'overrides the cached_finder options with local options for the fetch call' do
221
+ expect(::ActiveRemote::Cached.cache).to receive(:fetch).with(
222
+ [versioned_prefix, SearchMethodClass.name, '#search', 'foo'], { :expires_in => 200 }
223
+ ).and_return(:hello)
191
224
 
192
- SearchMethodClass.stub(:find, :hello) do
193
- SearchMethodClass.cached_search_by_foo(:foo, :expires_in => 200).must_equal(:hello)
194
- end
225
+ expect(SearchMethodClass).not_to receive(:find)
226
+ expect(SearchMethodClass.cached_search_by_foo(:foo, :expires_in => 200)).to eq(:hello)
195
227
  end
196
228
  end
197
229
 
198
- describe "#cached_search_by_foo!" do
230
+ describe '#cached_search_by_foo!' do
199
231
  before do
200
232
  ::ActiveRemote::Cached.cache(HashCache.new)
201
233
  ::ActiveRemote::Cached.default_options(:expires_in => 100)
@@ -205,16 +237,35 @@ describe SearchMethodClass do
205
237
  ::ActiveRemote::Cached.default_options({})
206
238
  end
207
239
 
208
- it "returns results when present" do
209
- SearchMethodClass.stub(:search, [:hello]) do
210
- SearchMethodClass.cached_search_by_foo!(:foo, :expires_in => 200).must_equal([:hello])
211
- end
240
+ it 'and_return results when present' do
241
+ expect(SearchMethodClass).to receive(:search).and_return([:hello])
242
+ expect(SearchMethodClass.cached_search_by_foo!(:foo, :expires_in => 200)).to eq([:hello])
212
243
  end
213
244
 
214
- it "raises ActiveRemote::RemoteRecordNotFound when not found" do
215
- SearchMethodClass.stub(:search, []) do
216
- -> { SearchMethodClass.cached_search_by_foo!(:foo, :expires_in => 200) }.must_raise ::ActiveRemote::RemoteRecordNotFound
217
- end
245
+ it 'raises ActiveRemote::RemoteRecordNotFound when not found' do
246
+ expect(SearchMethodClass).to receive(:search).and_return([])
247
+ expect do
248
+ SearchMethodClass.cached_search_by_foo!(:foo, :expires_in => 200)
249
+ end.to raise_error ::ActiveRemote::RemoteRecordNotFound
250
+ end
251
+ end
252
+
253
+ # This tests the method_missing override
254
+ describe '#cached_search_by_zippy123_and_alpha' do
255
+ before do
256
+ ::ActiveRemote::Cached.cache(HashCache.new)
257
+ ::ActiveRemote::Cached.default_options(:expires_in => 100)
258
+ end
259
+
260
+ after do
261
+ ::ActiveRemote::Cached.default_options({})
262
+ end
263
+
264
+ it 'calls the underlying method with params in correct order' do
265
+ expect(SearchMethodClass).to receive(:cached_search_by_alpha_and_zippy123).with('123', 'abc').and_return(:hello)
266
+
267
+ # Calls method that does not exist (different param order)
268
+ expect(SearchMethodClass.cached_search_by_zippy123_and_alpha('abc', '123')).to eq(:hello)
218
269
  end
219
270
  end
220
271
  end
data/spec/spec_helper.rb CHANGED
@@ -1,21 +1,16 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rubygems'
2
4
  require 'bundler'
3
5
  Bundler.require(:default, :development, :test)
4
6
 
5
- require 'minitest/mock'
6
- require 'minitest/spec'
7
- require 'minitest/autorun'
8
- require 'minitest/pride'
9
-
10
7
  class HashCache < Hash
11
8
  def exist?(key)
12
- self.has_key?(key)
9
+ key?(key)
13
10
  end
14
11
 
15
- def fetch(key, options = {}, &blk)
16
- if self.has_key?(key)
17
- return self[key]
18
- end
12
+ def fetch(key, _options = {})
13
+ return self[key] if key?(key)
19
14
 
20
15
  self[key] = yield
21
16
  end