caricature 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -13,12 +13,12 @@ An Isolation will create what in Rhino.Mocks would be called a DynamicMock (but
13
13
  In Moq it would be the Loose mocking strategy.
14
14
 
15
15
  The naming of the methods for creating mocks is the one that JP Boodhoo proposed WhenToldTo and WasToldTo.
16
- To specify a stub/expectation on an isolation you have one and only one way of doing that with the method called when_told_to.
17
- Then only if you're interested in asserting if a method has been called you can use the was_told_to? method for this.
16
+ To specify a stub/expectation on an isolation you have one and only one way of doing that with the method called when_receiving.
17
+ Then only if you're interested in asserting if a method has been called you can use the did_receive? method for this.
18
18
 
19
19
  <pre>
20
20
  isolation = Isolation.for(Ninja)
21
- isolation.when_told_to(:attack) do |exp|
21
+ isolation.when_receiving(:attack) do |exp|
22
22
  exp.with(:shuriken)
23
23
  exp.return(3)
24
24
  end
@@ -26,7 +26,7 @@ end
26
26
  Battle.new(mock)
27
27
  battle.combat
28
28
 
29
- isolation.was_told_to?(:attack).should.be.true?
29
+ isolation.did_receive?(:attack).should.be.true?
30
30
  </pre>
31
31
 
32
32
  It may be very important to note that when you're going to be isolating CLR classes to be used in other CLR classes
data/spec/bacon_helper.rb CHANGED
@@ -12,6 +12,10 @@ load_assembly 'ClrModels'
12
12
 
13
13
  class Soldier
14
14
 
15
+ def initialize
16
+ @life = 10
17
+ end
18
+
15
19
  def name
16
20
  "Tommy Boy"
17
21
  end
@@ -20,6 +24,31 @@ class Soldier
20
24
  "I'm a soldier"
21
25
  end
22
26
 
27
+ def attack(target, weapon)
28
+ weapon.attack(target)
29
+ end
30
+
31
+ def is_killed_by?(weapon)
32
+ weapon.damage > 3
33
+ end
34
+
35
+ def survive_attack_with(weapon)
36
+ @life - weapon.damage
37
+ end
38
+
39
+ end
40
+
41
+ class Dagger
42
+
43
+ def damage
44
+ 2
45
+ end
46
+
47
+ def attack(target)
48
+ target.survive_attack_with self
49
+ end
50
+
51
+
23
52
  end
24
53
 
25
54
  module Caricature
@@ -2,139 +2,631 @@ require File.dirname(__FILE__) + "/bacon_helper"
2
2
 
3
3
  describe "Full scenarios" do
4
4
 
5
- describe "when isolating CLR interfaces" do
6
- before do
7
- @ninja = ClrModels::Ninja.new
8
- @weapon = Caricature::Isolation.for(ClrModels::IWeapon)
9
- end
5
+ describe "CLR to CLR interactions" do
6
+
7
+ describe "when isolating CLR interfaces" do
8
+ before do
9
+ @ninja = ClrModels::Ninja.new
10
+ @weapon = Caricature::Isolation.for(ClrModels::IWeapon)
11
+ end
10
12
 
11
- it "should work without expectations" do
12
- @ninja.attack ClrModels::Ninja.new, @weapon
13
+ it "should work without expectations" do
14
+ @ninja.attack ClrModels::Ninja.new, @weapon
13
15
 
14
- @weapon.was_told_to?(:attack).should.be.successful
15
- end
16
+ @weapon.did_receive?(:attack).should.be.successful
17
+ end
18
+
19
+ it "should work for expectations with an argument constraint" do
20
+ ninja = ClrModels::Ninja.new
21
+ @weapon.when_receiving(:attack).with(ninja).return(5)
22
+
23
+ @ninja.attack(ninja, @weapon).should.equal 5
24
+
25
+ @weapon.did_receive?(:attack).with(:any).should.be.successful
26
+ end
27
+
28
+ it "should work for expectations with an argument constraint when a wrong argument is passed in" do
29
+ @weapon.when_receiving(:attack).with(ClrModels::Ninja.new).return(5)
30
+
31
+ @ninja.attack(ClrModels::Ninja.new, @weapon).should.equal 0
32
+ end
33
+
34
+ it "should work for expectations with an argument constraint and an assertion argument constraint" do
35
+ ninja = ClrModels::Ninja.new
36
+ @weapon.when_receiving(:attack).with(ninja).return(5)
37
+
38
+ @ninja.attack(ninja, @weapon).should.equal 5
39
+
40
+ @weapon.did_receive?(:attack).with(ninja).should.be.successful
41
+ end
42
+
43
+ it "should fail for expectations with an argument constraint and an assertion argument constraint" do
44
+ ninja = ClrModels::Ninja.new
45
+ @weapon.when_receiving(:attack).with(ninja).return(5)
46
+
47
+ @ninja.attack(ninja, @weapon).should.equal 5
48
+
49
+ @weapon.did_receive?(:attack).with(ClrModels::Ninja.new).should.not.be.successful
50
+ end
51
+
52
+ it "should work with an expectation with any arguments" do
53
+ @weapon.when_receiving(:damage).return(5)
54
+
55
+ @ninja.is_killed_by(@weapon).should.be.true?
56
+ @weapon.did_receive?(:damage).should.be.successful
57
+ end
16
58
 
17
- it "should work with an expectation with any arguments" do
18
- @weapon.when_told_to(:damage).return(5)
59
+ it "should work with an expectation getting different method call result" do
60
+ @weapon.when_receiving(:damage).return(2)
61
+
62
+ @ninja.is_killed_by(@weapon).should.be.false?
63
+ @weapon.did_receive?(:damage).should.be.successful
64
+ end
65
+
66
+ it "should work for an assertion on a specific argument" do
67
+ @weapon.when_receiving(:damage).return(2)
68
+
69
+ @ninja.is_killed_by(@weapon).should.be.false?
70
+ @weapon.did_receive?(:damage).should.be.successful
71
+ end
19
72
 
20
- @ninja.is_killed_by(@weapon).should.be.true?
21
- @weapon.was_told_to?(:damage).should.be.successful
22
73
  end
23
74
 
24
- # it "should work with an expectation with any arguments" do
25
- # @weapon.when_told_to(:damage).return(5)
26
- #
27
- # @ninja.is_killed_by(@weapon).should.be.true?
28
- # @weapon.was_told_to?(:damage).should.be.successful
29
- # end
75
+ describe "when isolating CLR classes" do
76
+
77
+ before do
78
+ @weapon = ClrModels::Sword.new
79
+ @ninja = Caricature::Isolation.for(ClrModels::Ninja)
80
+ end
30
81
 
31
- it "should work with an expectation getting different method call result" do
32
- @weapon.when_told_to(:damage).return(2)
82
+ it "should work without expectations" do
83
+ result = @weapon.attack @ninja
84
+ result.should.equal 0
85
+
86
+ @ninja.did_receive?(:survive_attack_with).with(@weapon).should.be.successful
87
+ end
88
+
89
+ it "should work for expectations with an argument constraint" do
90
+ @ninja.when_receiving(:survive_attack_with).with(@weapon).return(5)
91
+
92
+ @weapon.attack(@ninja).should.equal 5
93
+
94
+ @ninja.did_receive?(:survive_attack_with).with(:any).should.be.successful
95
+ end
96
+
97
+ it "should work for expectations with an argument constraint when a wrong argument is passed in" do
98
+ @ninja.when_receiving(:survive_attack_with).with(@weapon).return(5)
99
+
100
+ @weapon.attack(ClrModels::Ninja.new).should.equal 6
101
+
102
+ @ninja.did_receive?(:survive_attack_with).with(@weapon).should.not.be.successful
103
+ end
104
+
105
+ it "should work for expectations with an argument constraint and an assertion argument constraint" do
106
+ ninja = ClrModels::Ninja.new
107
+ @ninja.when_receiving(:survive_attack_with).with(@weapon).return(5)
108
+
109
+ @weapon.attack(@ninja).should.equal 5
110
+
111
+ @ninja.did_receive?(:survive_attack_with).with(@weapon).should.be.successful
112
+ end
113
+
114
+ it "should fail for expectations with an argument constraint and an assertion argument constraint" do
115
+ ninja = ClrModels::Ninja.new
116
+ @ninja.when_receiving(:survive_attack_with).with(@weapon).return(5)
117
+
118
+ @weapon.attack(@ninja).should.equal 5
119
+
120
+ @ninja.did_receive?(:survive_attack_with).with(ClrModels::Sword.new).should.not.be.successful
121
+ end
122
+
123
+ it "should work with an expectation for any arguments" do
124
+ @ninja.when_receiving(:survive_attack_with).return(5)
125
+
126
+ result = @weapon.attack @ninja
127
+ result.should.equal 5
128
+
129
+ @ninja.did_receive?(:survive_attack_with).with(:any).should.be.successful
130
+ end
131
+
132
+ it "should work with an assertion for specific arguments" do
133
+ @ninja.when_receiving(:survive_attack_with) do |method_should|
134
+ method_should.return(5)
135
+ end
136
+
137
+ result = @weapon.attack @ninja
138
+ result.should.equal 5
139
+
140
+ @ninja.did_receive?(:survive_attack_with).with(@weapon).should.be.successful
141
+ end
142
+
143
+ it "should fail for an assertion with wrong arguments" do
144
+ @ninja.when_receiving(:survive_attack_with) do |method_should|
145
+ method_should.return(5)
146
+ end
147
+
148
+ result = @weapon.attack @ninja
149
+ result.should.equal 5
150
+
151
+ @ninja.
152
+ did_receive?(:survive_attack_with).
153
+ with(Caricature::Isolation.for(ClrModels::IWeapon)).
154
+ should.not.be.successful
155
+ end
33
156
 
34
- @ninja.is_killed_by(@weapon).should.be.false?
35
- @weapon.was_told_to?(:damage).should.be.successful
36
157
  end
37
158
 
38
- it "should work for an assertion on a specific argument" do
39
- @weapon.when_told_to(:damage).return(2)
159
+ describe "when isolating CLR instances" do
160
+
161
+ before do
162
+ @weapon = ClrModels::Sword.new
163
+ @ninja = Caricature::Isolation.for(ClrModels::Ninja.new)
164
+ end
165
+
166
+ it "should work without expectations" do
167
+ result = @weapon.attack @ninja
168
+ result.should.equal 0
169
+
170
+ @ninja.did_receive?(:survive_attack_with).with(@weapon).should.be.successful
171
+ end
172
+
173
+ it "should work for expectations with an argument constraint" do
174
+ @ninja.when_receiving(:survive_attack_with).with(@weapon).return(5)
175
+
176
+ @weapon.attack(@ninja).should.equal 5
177
+
178
+ @ninja.did_receive?(:survive_attack_with).with(:any).should.be.successful
179
+ end
180
+
181
+ it "should work for expectations with an argument constraint when a wrong argument is passed in" do
182
+ @ninja.when_receiving(:survive_attack_with).with(@weapon).return(5)
183
+
184
+ @weapon.attack(ClrModels::Ninja.new).should.equal 6
185
+
186
+ @ninja.did_receive?(:survive_attack_with).with(@weapon).should.not.be.successful
187
+ end
188
+
189
+ it "should work for expectations with an argument constraint and an assertion argument constraint" do
190
+ ninja = ClrModels::Ninja.new
191
+ @ninja.when_receiving(:survive_attack_with).with(@weapon).return(5)
192
+
193
+ @weapon.attack(@ninja).should.equal 5
194
+
195
+ @ninja.did_receive?(:survive_attack_with).with(@weapon).should.be.successful
196
+ end
197
+
198
+ it "should fail for expectations with an argument constraint and an assertion argument constraint" do
199
+ ninja = ClrModels::Ninja.new
200
+ @ninja.when_receiving(:survive_attack_with).with(@weapon).return(5)
201
+
202
+ @weapon.attack(@ninja).should.equal 5
203
+
204
+ @ninja.did_receive?(:survive_attack_with).with(ClrModels::Sword.new).should.not.be.successful
205
+ end
206
+
207
+ it "should work with an expectation for any arguments" do
208
+ @ninja.when_receiving(:survive_attack_with).return(5)
209
+
210
+ result = @weapon.attack @ninja
211
+ result.should.equal 5
212
+
213
+ @ninja.did_receive?(:survive_attack_with).with(:any).should.be.successful
214
+ end
215
+
216
+ it "should fail for an assertion for specific arguments" do
217
+ @ninja.when_receiving(:survive_attack_with) do |method_should|
218
+ method_should.return(5)
219
+ end
220
+
221
+ result = @weapon.attack @ninja
222
+ result.should.equal 5
223
+ var = @ninja.did_receive?(:survive_attack_with).with(:any)
224
+ @ninja.did_receive?(:survive_attack_with).with(@weapon).should.be.successful
225
+ end
226
+
227
+ it "should allow to delegate the method call to the real instance (partial mock)" do
228
+ @ninja.when_receiving(:survive_attack_with).super_after
229
+
230
+ result = @weapon.attack @ninja
231
+ result.should.equal 6
232
+
233
+ @ninja.did_receive?(:survive_attack_with).should.be.successful
234
+ end
40
235
 
41
- @ninja.is_killed_by(@weapon).should.be.false?
42
- @weapon.was_told_to?(:damage).should.be.successful
43
236
  end
44
237
 
45
238
  end
46
239
 
47
- describe "when isolating CLR classes" do
240
+ describe "CLR isolations for ruby objects" do
48
241
 
49
- before do
50
- @weapon = ClrModels::Sword.new
51
- @ninja = Caricature::Isolation.for(ClrModels::Ninja)
52
- end
242
+ describe "when isolating CLR interfaces" do
243
+ before do
244
+ @soldier = Soldier.new
245
+ @weapon = Caricature::Isolation.for(ClrModels::IWeapon)
246
+ end
53
247
 
54
- it "should work without expectations" do
55
- result = @weapon.attack @ninja
56
- result.should.equal 0
248
+ it "should work without expectations" do
249
+ @soldier.attack Soldier.new, @weapon
57
250
 
58
- @ninja.was_told_to?(:survive_attack_with).with(@weapon).should.be.successful
59
- end
251
+ @weapon.did_receive?(:attack).should.be.successful
252
+ end
253
+
254
+ it "should work for expectations with an argument constraint" do
255
+ soldier = Soldier.new
256
+ @weapon.when_receiving(:attack).with(soldier).return(5)
257
+
258
+ @soldier.attack(soldier, @weapon).should.equal 5
259
+
260
+ @weapon.did_receive?(:attack).with(:any).should.be.successful
261
+ end
262
+
263
+ it "should work for expectations with an argument constraint when a wrong argument is passed in" do
264
+ @weapon.when_receiving(:attack).with(Soldier.new).return(5)
265
+
266
+ @soldier.attack(Soldier.new, @weapon).should.equal 0
267
+ end
268
+
269
+ it "should work for expectations with an argument constraint and an assertion argument constraint" do
270
+ soldier = Soldier.new
271
+ @weapon.when_receiving(:attack).with(soldier).return(5)
272
+
273
+ @soldier.attack(soldier, @weapon).should.equal 5
274
+
275
+ @weapon.did_receive?(:attack).with(soldier).should.be.successful
276
+ end
277
+
278
+ it "should fail for expectations with an argument constraint and an assertion argument constraint" do
279
+ soldier = Soldier.new
280
+ @weapon.when_receiving(:attack).with(soldier).return(5)
281
+
282
+ @soldier.attack(soldier, @weapon).should.equal 5
283
+
284
+ @weapon.did_receive?(:attack).with(Soldier.new).should.not.be.successful
285
+ end
286
+
287
+ it "should work with an expectation with any arguments" do
288
+ @weapon.when_receiving(:damage).return(5)
289
+
290
+ @soldier.is_killed_by?(@weapon).should.be.true?
291
+ @weapon.did_receive?(:damage).should.be.successful
292
+ end
293
+
294
+ it "should work with an expectation getting different method call result" do
295
+ @weapon.when_receiving(:damage).return(2)
296
+
297
+ @soldier.is_killed_by?(@weapon).should.be.false?
298
+ @weapon.did_receive?(:damage).should.be.successful
299
+ end
60
300
 
61
- it "should work with an expectation for any arguments" do
62
- @ninja.when_told_to(:survive_attack_with).return(5)
301
+ it "should work for an assertion on a specific argument" do
302
+ @weapon.when_receiving(:damage).return(2)
63
303
 
64
- result = @weapon.attack @ninja
65
- result.should.equal 5
304
+ @soldier.is_killed_by?(@weapon).should.be.false?
305
+ @weapon.did_receive?(:damage).should.be.successful
306
+ end
66
307
 
67
- @ninja.was_told_to?(:survive_attack_with).with(:any).should.be.successful
68
308
  end
69
309
 
70
- it "should work with an assertion for specific arguments" do
71
- @ninja.when_told_to(:survive_attack_with) do |method_should|
72
- method_should.return(5)
310
+ describe "when isolating CLR classes" do
311
+
312
+ before do
313
+ @weapon = Dagger.new
314
+ @ninja = Caricature::Isolation.for(ClrModels::Ninja)
315
+ end
316
+
317
+ it "should work without expectations" do
318
+ result = @weapon.attack @ninja
319
+ result.should.equal 0
320
+
321
+ @ninja.did_receive?(:survive_attack_with).with(@weapon).should.be.successful
322
+ end
323
+
324
+ it "should work for expectations with an argument constraint" do
325
+ @ninja.when_receiving(:survive_attack_with).with(@weapon).return(5)
326
+
327
+ @weapon.attack(@ninja).should.equal 5
328
+
329
+ @ninja.did_receive?(:survive_attack_with).with(:any).should.be.successful
330
+ end
331
+
332
+ it "should work for expectations with an argument constraint when a wrong argument is passed in" do
333
+ @ninja.when_receiving(:survive_attack_with).with(@weapon).return(5)
334
+
335
+ @weapon.attack(Soldier.new).should.equal 8
336
+
337
+ @ninja.did_receive?(:survive_attack_with).with(@weapon).should.not.be.successful
338
+ end
339
+
340
+ it "should work for expectations with an argument constraint and an assertion argument constraint" do
341
+ ninja = ClrModels::Ninja.new
342
+ @ninja.when_receiving(:survive_attack_with).with(@weapon).return(5)
343
+
344
+ @weapon.attack(@ninja).should.equal 5
345
+
346
+ @ninja.did_receive?(:survive_attack_with).with(@weapon).should.be.successful
347
+ end
348
+
349
+ it "should fail for expectations with an argument constraint and an assertion argument constraint" do
350
+ ninja = ClrModels::Ninja.new
351
+ @ninja.when_receiving(:survive_attack_with).with(@weapon).return(5)
352
+
353
+ @weapon.attack(@ninja).should.equal 5
354
+
355
+ @ninja.did_receive?(:survive_attack_with).with(Dagger.new).should.not.be.successful
73
356
  end
74
357
 
75
- result = @weapon.attack @ninja
76
- result.should.equal 5
358
+ it "should work with an expectation for any arguments" do
359
+ @ninja.when_receiving(:survive_attack_with).return(5)
360
+
361
+ result = @weapon.attack @ninja
362
+ result.should.equal 5
363
+
364
+ @ninja.did_receive?(:survive_attack_with).with(:any).should.be.successful
365
+ end
366
+
367
+ it "should work with an assertion for specific arguments" do
368
+ @ninja.when_receiving(:survive_attack_with) do |method_should|
369
+ method_should.return(5)
370
+ end
371
+
372
+ result = @weapon.attack @ninja
373
+ result.should.equal 5
374
+
375
+ @ninja.did_receive?(:survive_attack_with).with(@weapon).should.be.successful
376
+ end
377
+
378
+ it "should fail for an assertion with wrong arguments" do
379
+ @ninja.when_receiving(:survive_attack_with) do |method_should|
380
+ method_should.return(5)
381
+ end
382
+
383
+ result = @weapon.attack @ninja
384
+ result.should.equal 5
385
+
386
+ @ninja.
387
+ did_receive?(:survive_attack_with).
388
+ with(Caricature::Isolation.for(ClrModels::IWeapon)).
389
+ should.not.be.successful
390
+ end
77
391
 
78
- @ninja.was_told_to?(:survive_attack_with).with(@weapon).should.be.successful
79
392
  end
80
393
 
81
- it "should fail for an assertion with wrong arguments" do
82
- @ninja.when_told_to(:survive_attack_with) do |method_should|
83
- method_should.return(5)
394
+ describe "when isolating CLR instances" do
395
+
396
+ before do
397
+ @weapon = Dagger.new
398
+ @ninja = Caricature::Isolation.for(ClrModels::Ninja.new)
399
+ end
400
+
401
+ it "should work without expectations" do
402
+ result = @weapon.attack @ninja
403
+ result.should.equal 0
404
+
405
+ @ninja.did_receive?(:survive_attack_with).with(@weapon).should.be.successful
84
406
  end
85
407
 
86
- result = @weapon.attack @ninja
87
- result.should.equal 5
408
+ it "should work for expectations with an argument constraint" do
409
+ @ninja.when_receiving(:survive_attack_with).with(@weapon).return(5)
410
+
411
+ @weapon.attack(@ninja).should.equal 5
412
+
413
+ @ninja.did_receive?(:survive_attack_with).with(:any).should.be.successful
414
+ end
415
+
416
+ it "should work for expectations with an argument constraint when a wrong argument is passed in" do
417
+ @ninja.when_receiving(:survive_attack_with).with(@weapon).return(5)
418
+
419
+ @weapon.attack(Soldier.new).should.equal 8
420
+
421
+ @ninja.did_receive?(:survive_attack_with).with(@weapon).should.not.be.successful
422
+ end
423
+
424
+ it "should work for expectations with an argument constraint and an assertion argument constraint" do
425
+ ninja = ClrModels::Ninja.new
426
+ @ninja.when_receiving(:survive_attack_with).with(@weapon).return(5)
427
+
428
+ @weapon.attack(@ninja).should.equal 5
429
+
430
+ @ninja.did_receive?(:survive_attack_with).with(@weapon).should.be.successful
431
+ end
432
+
433
+ it "should fail for expectations with an argument constraint and an assertion argument constraint" do
434
+ ninja = ClrModels::Ninja.new
435
+ @ninja.when_receiving(:survive_attack_with).with(@weapon).return(5)
436
+
437
+ @weapon.attack(@ninja).should.equal 5
438
+
439
+ @ninja.did_receive?(:survive_attack_with).with(Dagger.new).should.not.be.successful
440
+ end
441
+
442
+ it "should work with an expectation for any arguments" do
443
+ @ninja.when_receiving(:survive_attack_with).return(5)
444
+
445
+ result = @weapon.attack @ninja
446
+ result.should.equal 5
447
+
448
+ @ninja.did_receive?(:survive_attack_with).with(:any).should.be.successful
449
+ end
450
+
451
+ it "should fail for an assertion for specific arguments" do
452
+ @ninja.when_receiving(:survive_attack_with) do |method_should|
453
+ method_should.return(5)
454
+ end
455
+
456
+ result = @weapon.attack @ninja
457
+ result.should.equal 5
458
+ var = @ninja.did_receive?(:survive_attack_with).with(:any)
459
+ @ninja.did_receive?(:survive_attack_with).with(@weapon).should.be.successful
460
+ end
461
+
88
462
 
89
- @ninja.
90
- was_told_to?(:survive_attack_with).
91
- with(Caricature::Isolation.for(ClrModels::IWeapon)).
92
- should.not.be.successful
93
463
  end
94
464
 
95
465
  end
466
+
467
+ describe "Ruby to Ruby interactions" do
96
468
 
97
- describe "when isolating CLR instances" do
469
+ describe "when isolating Ruby classes" do
98
470
 
99
- before do
100
- @weapon = ClrModels::Sword.new
101
- @ninja = Caricature::Isolation.for(ClrModels::Ninja.new)
102
- end
471
+ before do
472
+ @dagger = Dagger.new
473
+ @soldier = Caricature::Isolation.for(Soldier)
474
+ end
103
475
 
104
- it "should work without expectations" do
105
- result = @weapon.attack @ninja
106
- result.should.equal 0
476
+ it "should work without expectations" do
477
+ result = @dagger.attack @soldier
478
+ result.should.equal nil
107
479
 
108
- @ninja.was_told_to?(:survive_attack_with).with(@weapon).should.be.successful
109
- end
480
+ @soldier.did_receive?(:survive_attack_with).with(@dagger).should.be.successful
481
+ end
110
482
 
111
- it "should work with an expectation for any arguments" do
112
- @ninja.when_told_to(:survive_attack_with).return(5)
483
+ it "should work for expectations with an argument constraint" do
484
+ @soldier.when_receiving(:survive_attack_with).with(@dagger).return(5)
113
485
 
114
- result = @weapon.attack @ninja
115
- result.should.equal 5
486
+ @dagger.attack(@soldier).should.equal 5
116
487
 
117
- @ninja.was_told_to?(:survive_attack_with).with(:any).should.be.successful
118
- end
488
+ @soldier.did_receive?(:survive_attack_with).with(:any).should.be.successful
489
+ end
490
+
491
+ it "should work for expectations with an argument constraint when a wrong argument is passed in" do
492
+ @soldier.when_receiving(:survive_attack_with).with(@dagger).return(5)
493
+
494
+ @dagger.attack(Soldier.new).should.equal 8
495
+
496
+ @soldier.did_receive?(:survive_attack_with).with(@dagger).should.not.be.successful
497
+ end
498
+
499
+ it "should work for expectations with an argument constraint and an assertion argument constraint" do
500
+ soldier = Soldier.new
501
+ @soldier.when_receiving(:survive_attack_with).with(@dagger).return(5)
502
+
503
+ @dagger.attack(@soldier).should.equal 5
504
+
505
+ @soldier.did_receive?(:survive_attack_with).with(@dagger).should.be.successful
506
+ end
507
+
508
+ it "should fail for expectations with an argument constraint and an assertion argument constraint" do
509
+ soldier = Soldier.new
510
+ @soldier.when_receiving(:survive_attack_with).with(@dagger).return(5)
511
+
512
+ @dagger.attack(@soldier).should.equal 5
119
513
 
120
- it "should fail for an assertion for specific arguments" do
121
- @ninja.when_told_to(:survive_attack_with) do |method_should|
122
- method_should.return(5)
514
+ @soldier.did_receive?(:survive_attack_with).with(Dagger.new).should.not.be.successful
515
+ end
516
+
517
+ it "should work with an expectation for any arguments" do
518
+ @soldier.when_receiving(:survive_attack_with).return(5)
519
+
520
+ result = @dagger.attack @soldier
521
+ result.should.equal 5
522
+
523
+ @soldier.did_receive?(:survive_attack_with).with(:any).should.be.successful
524
+ end
525
+
526
+ it "should work with an assertion for specific arguments" do
527
+ @soldier.when_receiving(:survive_attack_with) do |method_should|
528
+ method_should.return(5)
529
+ end
530
+
531
+ result = @dagger.attack @soldier
532
+ result.should.equal 5
533
+
534
+ @soldier.did_receive?(:survive_attack_with).with(@dagger).should.be.successful
535
+ end
536
+
537
+ it "should fail for an assertion with wrong arguments" do
538
+ @soldier.when_receiving(:survive_attack_with) do |method_should|
539
+ method_should.return(5)
540
+ end
541
+
542
+ result = @dagger.attack @soldier
543
+ result.should.equal 5
544
+
545
+ @soldier.
546
+ did_receive?(:survive_attack_with).
547
+ with(Caricature::Isolation.for(Dagger)).
548
+ should.not.be.successful
123
549
  end
124
550
 
125
- result = @weapon.attack @ninja
126
- result.should.equal 5
127
- var = @ninja.was_told_to?(:survive_attack_with).with(:any)
128
- @ninja.was_told_to?(:survive_attack_with).with(@weapon).should.be.successful
129
551
  end
130
552
 
131
- it "should allow to delegate the method call to the real instance (partial mock)" do
132
- @ninja.when_told_to(:survive_attack_with).super_after
553
+ describe "when isolating Ruby instances" do
554
+
555
+ before do
556
+ @dagger = Dagger.new
557
+ @soldier = Caricature::Isolation.for(Soldier.new)
558
+ end
559
+
560
+ it "should work without expectations" do
561
+ result = @dagger.attack @soldier
562
+ result.should.equal nil
563
+
564
+ @soldier.did_receive?(:survive_attack_with).with(@dagger).should.be.successful
565
+ end
566
+
567
+ it "should work for expectations with an argument constraint" do
568
+ @soldier.when_receiving(:survive_attack_with).with(@dagger).return(5)
569
+
570
+ @dagger.attack(@soldier).should.equal 5
571
+
572
+ @soldier.did_receive?(:survive_attack_with).with(:any).should.be.successful
573
+ end
574
+
575
+ it "should work for expectations with an argument constraint when a wrong argument is passed in" do
576
+ @soldier.when_receiving(:survive_attack_with).with(@dagger).return(5)
577
+
578
+ @dagger.attack(Soldier.new).should.equal 8
579
+
580
+ @soldier.did_receive?(:survive_attack_with).with(@dagger).should.not.be.successful
581
+ end
582
+
583
+ it "should work for expectations with an argument constraint and an assertion argument constraint" do
584
+ soldier = Soldier.new
585
+ @soldier.when_receiving(:survive_attack_with).with(@dagger).return(5)
586
+
587
+ @dagger.attack(@soldier).should.equal 5
588
+
589
+ @soldier.did_receive?(:survive_attack_with).with(@dagger).should.be.successful
590
+ end
133
591
 
134
- result = @weapon.attack @ninja
135
- result.should.equal 6
592
+ it "should fail for expectations with an argument constraint and an assertion argument constraint" do
593
+ soldier = Soldier.new
594
+ @soldier.when_receiving(:survive_attack_with).with(@dagger).return(5)
595
+
596
+ @dagger.attack(@soldier).should.equal 5
597
+
598
+ @soldier.did_receive?(:survive_attack_with).with(Dagger.new).should.not.be.successful
599
+ end
600
+
601
+ it "should work with an expectation for any arguments" do
602
+ @soldier.when_receiving(:survive_attack_with).return(5)
603
+
604
+ result = @dagger.attack @soldier
605
+ result.should.equal 5
606
+
607
+ @soldier.did_receive?(:survive_attack_with).with(:any).should.be.successful
608
+ end
609
+
610
+ it "should fail for an assertion for specific arguments" do
611
+ @soldier.when_receiving(:survive_attack_with) do |method_should|
612
+ method_should.return(5)
613
+ end
614
+
615
+ result = @dagger.attack @soldier
616
+ result.should.equal 5
617
+ var = @soldier.did_receive?(:survive_attack_with).with(:any)
618
+ @soldier.did_receive?(:survive_attack_with).with(@dagger).should.be.successful
619
+ end
620
+
621
+ it "should allow to delegate the method call to the real instance (partial mock)" do
622
+ @soldier.when_receiving(:survive_attack_with).super_after
623
+
624
+ result = @dagger.attack @soldier
625
+ result.should.equal 8
626
+
627
+ @soldier.did_receive?(:survive_attack_with).should.be.successful
628
+ end
136
629
 
137
- @ninja.was_told_to?(:survive_attack_with).should.be.successful
138
630
  end
139
631
 
140
632
  end
@@ -66,7 +66,7 @@ describe "Caricature::Isolation" do
66
66
 
67
67
  it "should create an expectation with a block" do
68
68
  nm = "What's in a name"
69
- expectation = @isolator.when_told_to(:name) do |cl|
69
+ expectation = @isolator.when_receiving(:name) do |cl|
70
70
  cl.return(nm)
71
71
  end
72
72
  expectation.method_name.should.equal :name
@@ -76,7 +76,7 @@ describe "Caricature::Isolation" do
76
76
 
77
77
  it "should create an expectation without a block" do
78
78
  nm = "What's in a name"
79
- expectation = @isolator.when_told_to(:name).return(nm)
79
+ expectation = @isolator.when_receiving(:name).return(nm)
80
80
  expectation.method_name.should.equal :name
81
81
  expectation.has_return_value?.should.be.true?
82
82
  expectation.return_value.should.equal nm
@@ -107,7 +107,7 @@ describe "Caricature::Isolation" do
107
107
 
108
108
  it "should create an expectation with a block" do
109
109
  nm = "What's in a name"
110
- expectation = @isolator.when_told_to(:name) do |cl|
110
+ expectation = @isolator.when_receiving(:name) do |cl|
111
111
  cl.return(nm)
112
112
  end
113
113
  expectation.method_name.should.equal :name
@@ -117,7 +117,7 @@ describe "Caricature::Isolation" do
117
117
 
118
118
  it "should create an expectation without a block" do
119
119
  nm = "What's in a name"
120
- expectation = @isolator.when_told_to(:name).return(nm)
120
+ expectation = @isolator.when_receiving(:name).return(nm)
121
121
  expectation.method_name.should.equal :name
122
122
  expectation.has_return_value?.should.be.true?
123
123
  expectation.return_value.should.equal nm
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: caricature
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ivan Porto Carrero
@@ -9,21 +9,19 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-15 00:00:00 +02:00
12
+ date: 2009-05-16 00:00:00 +02:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
16
- description: Caricature - Bringing simple mocking to the DLR
16
+ description: This project aims to make interop between IronRuby objects and .NET objects easier. The idea is that it integrates nicely with bacon and later rspec and that it transparently lets you mock ironruby ojbects as well as CLR objects/interfaces. Caricature handles interfaces, interface inheritance, CLR objects, CLR object instances, Ruby classes and instances of Ruby classes.
17
17
  email: ivan@flanders.co.nz
18
18
  executables: []
19
19
 
20
20
  extensions: []
21
21
 
22
22
  extra_rdoc_files:
23
- - README
24
23
  - README.markdown
25
24
  files:
26
- - README
27
25
  - README.markdown
28
26
  has_rdoc: true
29
27
  homepage: http://github.com/casualjim/caricature
data/README DELETED
@@ -1,29 +0,0 @@
1
- Caricature - Bringing simple mocking to the DLR
2
- ===============================================
3
-
4
- This project aims to make interop between IronRuby objects and .NET objects easier.
5
- The idea is that it integrates nicely with bacon and later rspec and that it transparently lets you isolate ironruby ojbects
6
- as well as CLR objects/interfaces.
7
- Caricature handles interfaces, interface inheritance, CLR objects, CLR object instances, Ruby classes and instances of Ruby classes.
8
-
9
- From the start I wanted to do away with names like mock, stub, record, replay, verify etc.
10
- Instead I took the advice from Roy Osherhove and went with a name of Isolation for creating a mock.
11
-
12
- An Isolation will create what in Rhino.Mocks would be called a DynamicMock (but can be a partial too :) )
13
- In Moq it would be the Loose mocking strategy.
14
-
15
- The naming of the methods for creating isolations is the one that JP Boodhoo proposed WhenToldTo and WasToldTo.
16
- To specify a stub/expectation on an isolation you have one and only one way of doing that with the method called when_told_to.
17
- Then only if you're interested in asserting if a method has been called you can use the was_told_to? method for this.
18
-
19
-
20
- isolation = Isolation.for(Ninja)
21
- isolation.when_told_to(:attack) do |exp|
22
- exp.with(:shuriken)
23
- exp.return(3)
24
- end
25
-
26
- Battle.new(isolation)
27
- battle.combat
28
-
29
- isolation.was_told_to?(:attack).should.be.true?