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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
|
require 'spec_helper'
require 'mspec/mocks/proxy'
describe MockObject, ".new" do
it "creates a new mock object" do
m = MockObject.new('not a null object')
lambda { m.not_a_method }.should raise_error(NoMethodError)
end
it "creates a new mock object that follows the NullObject pattern" do
m = MockObject.new('null object', :null_object => true)
m.not_really_a_method.should equal(m)
end
end
describe MockProxy, ".new" do
it "creates a mock proxy by default" do
MockProxy.new.mock?.should be_true
end
it "creates a stub proxy by request" do
MockProxy.new(:stub).stub?.should be_true
end
it "sets the call expectation to 1 call for a mock" do
MockProxy.new.count.should == [:exactly, 1]
end
it "sets the call expectation to any number of times for a stub" do
MockProxy.new(:stub).count.should == [:any_number_of_times, 0]
end
end
describe MockProxy, "#count" do
before :each do
@proxy = MockProxy.new
end
it "returns the expected number of calls the mock should receive" do
@proxy.count.should == [:exactly, 1]
@proxy.at_least(3).count.should == [:at_least, 3]
end
end
describe MockProxy, "#arguments" do
before :each do
@proxy = MockProxy.new
end
it "returns the expected arguments" do
@proxy.arguments.should == :any_args
end
end
describe MockProxy, "#with" do
before :each do
@proxy = MockProxy.new
end
it "returns self" do
@proxy.with(:a).should be_equal(@proxy)
end
it "raises an ArgumentError if no arguments are given" do
lambda { @proxy.with }.should raise_error(ArgumentError)
end
it "accepts any number of arguments" do
@proxy.with(1, 2, 3).should be_an_instance_of(MockProxy)
@proxy.arguments.should == [1,2,3]
end
end
describe MockProxy, "#once" do
before :each do
@proxy = MockProxy.new
end
it "returns self" do
@proxy.once.should be_equal(@proxy)
end
it "sets the expected calls to 1" do
@proxy.once
@proxy.count.should == [:exactly, 1]
end
it "accepts no arguments" do
lambda { @proxy.once(:a) }.should raise_error
end
end
describe MockProxy, "#twice" do
before :each do
@proxy = MockProxy.new
end
it "returns self" do
@proxy.twice.should be_equal(@proxy)
end
it "sets the expected calls to 2" do
@proxy.twice
@proxy.count.should == [:exactly, 2]
end
it "accepts no arguments" do
lambda { @proxy.twice(:b) }.should raise_error
end
end
describe MockProxy, "#exactly" do
before :each do
@proxy = MockProxy.new
end
it "returns self" do
@proxy.exactly(2).should be_equal(@proxy)
end
it "sets the expected calls to exactly n" do
@proxy.exactly(5)
@proxy.count.should == [:exactly, 5]
end
it "does not accept an argument that Integer() cannot convert" do
lambda { @proxy.exactly('x') }.should raise_error
end
end
describe MockProxy, "#at_least" do
before :each do
@proxy = MockProxy.new
end
it "returns self" do
@proxy.at_least(3).should be_equal(@proxy)
end
it "sets the expected calls to at least n" do
@proxy.at_least(3)
@proxy.count.should == [:at_least, 3]
end
it "accepts :once :twice" do
@proxy.at_least(:once)
@proxy.count.should == [:at_least, 1]
@proxy.at_least(:twice)
@proxy.count.should == [:at_least, 2]
end
it "does not accept an argument that Integer() cannot convert" do
lambda { @proxy.at_least('x') }.should raise_error
end
end
describe MockProxy, "#at_most" do
before :each do
@proxy = MockProxy.new
end
it "returns self" do
@proxy.at_most(2).should be_equal(@proxy)
end
it "sets the expected calls to at most n" do
@proxy.at_most(2)
@proxy.count.should == [:at_most, 2]
end
it "accepts :once, :twice" do
@proxy.at_most(:once)
@proxy.count.should == [:at_most, 1]
@proxy.at_most(:twice)
@proxy.count.should == [:at_most, 2]
end
it "does not accept an argument that Integer() cannot convert" do
lambda { @proxy.at_most('x') }.should raise_error
end
end
describe MockProxy, "#any_number_of_times" do
before :each do
@proxy = MockProxy.new
end
it "returns self" do
@proxy.any_number_of_times.should be_equal(@proxy)
end
it "sets the expected calls to any number of times" do
@proxy.any_number_of_times
@proxy.count.should == [:any_number_of_times, 0]
end
it "does not accept an argument" do
lambda { @proxy.any_number_of_times(2) }.should raise_error
end
end
describe MockProxy, "#and_return" do
before :each do
@proxy = MockProxy.new
end
it "returns self" do
@proxy.and_return(false).should equal(@proxy)
end
it "sets the expected return value" do
@proxy.and_return(false)
@proxy.returning.should == false
end
it "accepts any number of return values" do
@proxy.and_return(1, 2, 3)
@proxy.returning.should == 1
@proxy.returning.should == 2
@proxy.returning.should == 3
end
it "implicitly sets the expected number of calls" do
@proxy.and_return(1, 2, 3)
@proxy.count.should == [:exactly, 3]
end
it "only sets the expected number of calls if it is higher than what is already set" do
@proxy.at_least(5).times.and_return(1, 2, 3)
@proxy.count.should == [:at_least, 5]
@proxy.at_least(2).times.and_return(1, 2, 3)
@proxy.count.should == [:at_least, 3]
end
end
describe MockProxy, "#returning" do
before :each do
@proxy = MockProxy.new
end
it "returns nil by default" do
@proxy.returning.should be_nil
end
it "returns the value set by #and_return" do
@proxy.and_return(2)
@proxy.returning.should == 2
@proxy.returning.should == 2
end
it "returns a sequence of values set by #and_return" do
@proxy.and_return(1,2,3,4)
@proxy.returning.should == 1
@proxy.returning.should == 2
@proxy.returning.should == 3
@proxy.returning.should == 4
@proxy.returning.should == 4
@proxy.returning.should == 4
end
end
describe MockProxy, "#calls" do
before :each do
@proxy = MockProxy.new
end
it "returns the number of times the proxy is called" do
@proxy.calls.should == 0
end
end
describe MockProxy, "#called" do
before :each do
@proxy = MockProxy.new
end
it "increments the number of times the proxy is called" do
@proxy.called
@proxy.called
@proxy.calls.should == 2
end
end
describe MockProxy, "#times" do
before :each do
@proxy = MockProxy.new
end
it "is a no-op" do
@proxy.times.should == @proxy
end
end
describe MockProxy, "#stub?" do
it "returns true if the proxy is created as a stub" do
MockProxy.new(:stub).stub?.should be_true
end
it "returns false if the proxy is created as a mock" do
MockProxy.new(:mock).stub?.should be_false
end
end
describe MockProxy, "#mock?" do
it "returns true if the proxy is created as a mock" do
MockProxy.new(:mock).mock?.should be_true
end
it "returns false if the proxy is created as a stub" do
MockProxy.new(:stub).mock?.should be_false
end
end
describe MockProxy, "#and_yield" do
before :each do
@proxy = MockProxy.new
end
it "returns self" do
@proxy.and_yield(false).should equal(@proxy)
end
it "sets the expected values to yield" do
@proxy.and_yield(1).yielding.should == [[1]]
end
it "accepts multiple values to yield" do
@proxy.and_yield(1, 2, 3).yielding.should == [[1, 2, 3]]
end
end
describe MockProxy, "#raising" do
before :each do
@proxy = MockProxy.new
end
it "returns nil by default" do
@proxy.raising.should be_nil
end
it "returns the exception object passed to #and_raise" do
exc = double("exception")
@proxy.and_raise(exc)
@proxy.raising.should equal(exc)
end
it "returns an instance of RuntimeError when a String is passed to #and_raise" do
@proxy.and_raise("an error")
exc = @proxy.raising
exc.should be_an_instance_of(RuntimeError)
exc.message.should == "an error"
end
end
describe MockProxy, "#yielding" do
before :each do
@proxy = MockProxy.new
end
it "returns an empty array by default" do
@proxy.yielding.should == []
end
it "returns an array of arrays of values the proxy should yield" do
@proxy.and_yield(3)
@proxy.yielding.should == [[3]]
end
it "returns an accumulation of arrays of values the proxy should yield" do
@proxy.and_yield(1).and_yield(2, 3)
@proxy.yielding.should == [[1], [2, 3]]
end
end
describe MockProxy, "#yielding?" do
before :each do
@proxy = MockProxy.new
end
it "returns false if the proxy is not yielding" do
@proxy.yielding?.should be_false
end
it "returns true if the proxy is yielding" do
@proxy.and_yield(1)
@proxy.yielding?.should be_true
end
end
describe MockIntObject, "#to_int" do
before :each do
@int = MockIntObject.new(10)
end
it "returns the number if to_int is called" do
@int.to_int.should == 10
@int.count.should == [:at_least, 1]
end
it "tries to convert the target to int if to_int is called" do
MockIntObject.new(@int).to_int.should == 10
@int.count.should == [:at_least, 1]
end
end
|