/
helper_spec.rb
324 lines (260 loc) · 9.39 KB
/
helper_spec.rb
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
require "spec_helper"
describe ActsAsAsync::Helper do
describe ".perform" do
context "when no ID is passed" do
let(:model) do
new_async_model
end
it "should call class methods" do
model.should_receive(:foobar)
model.perform "foobar"
end
it "should pass arguments" do
model.should_receive(:foobar).with("foo", "bar")
model.perform "foobar", "foo", "bar"
end
end
context "when an ID is passed" do
let(:instance) { Model.create }
it "should call instance methods" do
Model.perform instance.id, "smash!"
instance.reload.name.should == "SMASHED"
end
it "should pass arguments" do
Model.perform instance.id, "smash!", "LOL"
instance.reload.name.should == "LOL"
end
end
end
describe ".async" do
let(:model) { new_async_model }
it "should enqueue the current class" do
Resque.should_receive(:enqueue).with(model, anything)
model.async :foo
end
it "should enqueue the passed method" do
Resque.should_receive(:enqueue).with(anything, :foo)
model.async :foo
end
it "should append any additional arguments" do
Resque.should_receive(:enqueue).with(anything, anything, "foo", "bar")
model.async :foo, "foo", "bar"
end
end
describe ".async_at" do
let(:model) { new_async_model }
let(:time) { Time.now }
it "should enqueue at the passed time" do
Resque.should_receive(:enqueue_at).with(time, anything, anything)
model.async_at time, :foo
end
it "should enqueue the current class" do
Resque.should_receive(:enqueue_at).with(anything, model, anything)
model.async_at time, :foo
end
it "should enqueue the passed method" do
Resque.should_receive(:enqueue_at).with(anything, anything, :foo)
model.async_at time, :foo
end
it "should append any additional arguments" do
Resque.should_receive(:enqueue_at).with(anything, anything, anything, "bar", "baz")
model.async_at time, :foo, "bar", "baz"
end
end
describe ".async_in" do
let(:model) { new_async_model }
let(:interval) { 10.minutes }
it "should enqueue at the passed time interval" do
Resque.should_receive(:enqueue_in).with(interval, anything, anything)
model.async_in interval, :foo
end
it "should enqueue the current class" do
Resque.should_receive(:enqueue_in).with(anything, model, anything)
model.async_in interval, :foo
end
it "should enqueue the passed method" do
Resque.should_receive(:enqueue_in).with(anything, anything, :foo)
model.async_in interval, :foo
end
it "should append any additional arguments" do
Resque.should_receive(:enqueue_in).with(anything, anything, anything, "bar", "baz")
model.async_in interval, :foo, "bar", "baz"
end
end
describe ".inherited" do
context "when @queue isn't set" do
let(:model) { new_async_model }
let(:inherited) { Class.new model }
it "should set @queue to the value of the parent" do
inherited.instance_variable_get(:@queue).should == :default
end
end
end
describe "#async" do
let (:instance) { Model.create }
it "should enqueue the current class" do
Resque.should_receive(:enqueue).with(instance.class, anything, anything)
instance.async :foo
end
it "should enqueue the current instance" do
Resque.should_receive(:enqueue).with(anything, instance.id, anything)
instance.async :foo
end
it "should enqueue the passed method" do
Resque.should_receive(:enqueue).with(anything, anything, :foo)
instance.async :foo
end
it "should append any additional arguments" do
Resque.should_receive(:enqueue).with(anything, anything, anything, "bar", "baz")
instance.async :foo, "bar", "baz"
end
end
describe "#async_at" do
let (:instance) { Model.create }
let (:time) { Time.now }
it "should enqueue at the passed time" do
Resque.should_receive(:enqueue_at).with(time, anything, anything, anything)
instance.async_at time, :foo
end
it "should enqueue the current class" do
Resque.should_receive(:enqueue_at).with(anything, instance.class, anything, anything)
instance.async_at time, :foo
end
it "should enqueue the current instance" do
Resque.should_receive(:enqueue_at).with(anything, anything, instance.id, anything)
instance.async_at time, :foo
end
it "should enqueue the passed method" do
Resque.should_receive(:enqueue_at).with(anything, anything, anything, :foo)
instance.async_at time, :foo
end
it "should append any additional arguments" do
Resque.should_receive(:enqueue_at).with(anything, anything, anything, anything, "bar", "baz")
instance.async_at time, :foo, "bar", "baz"
end
end
describe "#async_in" do
let (:instance) { Model.create }
let (:interval) { 10.minutes }
it "should enqueue at the passed time interval" do
Resque.should_receive(:enqueue_in).with(interval, anything, anything, anything)
instance.async_in interval, :foo
end
it "should enqueue the current class" do
Resque.should_receive(:enqueue_in).with(anything, instance.class, anything, anything)
instance.async_in interval, :foo
end
it "should enqueue the current instance" do
Resque.should_receive(:enqueue_in).with(anything, anything, instance.id, anything)
instance.async_in interval, :foo
end
it "should enqueue the passed method" do
Resque.should_receive(:enqueue_in).with(anything, anything, anything, :foo)
instance.async_in interval, :foo
end
it "should append any additional arguments" do
Resque.should_receive(:enqueue_in).with(anything, anything, anything, anything, "bar", "baz")
instance.async_in interval, :foo, "bar", "baz"
end
end
describe "::METHOD_REGEXP" do
let(:regexp) { ActsAsAsync::Helper::SharedMethods::METHOD_REGEXP }
it "should match async_foobar" do
"async_foobar".should match(regexp)
end
it "should match async_foobar!" do
"async_foobar!".should match(regexp)
end
it "should match async_foobar_at" do
"async_foobar_at".should match(regexp)
end
it "should match async_foobar_at!" do
"async_foobar_at!".should match(regexp)
end
it "should match async_foobar_in" do
"async_foobarin".should match(regexp)
end
it "should match async_foobar_in!" do
"async_foobar_in!".should match(regexp)
end
it "should not match async_123" do
"async_123".should_not match(regexp)
end
it "should capture the method name" do
match = "async_foobar".match regexp
match[1].should == "foobar"
end
it "should capture a trailing '_at'" do
match = "async_foobar_at".match regexp
match[2].should == "_at"
end
it "should capture a trailing '_in'" do
match = "async_foobar_in".match regexp
match[2].should == "_in"
end
it "should capture a trailing '!'" do
match = "async_foobar!".match regexp
match[3].should == "!"
end
end
describe "method_missing" do
context "when the missing method doesn't match" do
it "should call super" do
lambda { Model.nonexistant_method }.should raise_error(NoMethodError)
end
end
context "when the extracted method exists" do
context "when the method matches async" do
it "should call async with the extracted method" do
Model.should_receive(:async).with("lol")
Model.async_lol
end
it "should order the arguments properly" do
Model.should_receive(:async).with("lol", "foo", "bar")
Model.async_lol("foo", "bar")
end
end
context "when the method matches async_at" do
let (:time) { Time.now }
it "should call async_at with the extracted method" do
Model.should_receive(:async_at).with(time, "lol")
Model.async_lol_at time
end
it "should order the arguments properly" do
Model.should_receive(:async_at).with(time, "lol", "foo", "bar")
Model.async_lol_at time, "foo", "bar"
end
end
context "when the method matches async_in" do
let (:interval) { 10.minutes }
it "should call async_at with the extracted method" do
Model.should_receive(:async_in).with(interval, "lol")
Model.async_lol_in interval
end
it "should order the arguments properly" do
Model.should_receive(:async_in).with(interval, "lol", "foo", "bar")
Model.async_lol_in interval, "foo", "bar"
end
end
context "when the matched method is private" do
let (:model) { Model.create }
it "should still work" do
lambda { model.async_a_private_method }.should_not raise_error
end
end
end
context "when the extracted method doesn't exist" do
it "should raise NoMethodError" do
lambda { Model.async_nonexistant_method }.should raise_error(NoMethodError)
end
end
end
describe "respond_to?" do
it "should return true if the passed method matches" do
Model.respond_to?(:async_something).should == true
end
it "should call super if the passed method doesn't match" do
Model.respond_to?(:something_totally_whack).should == false
end
end
end