transpec 1.2.2 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -106,137 +106,234 @@ module Transpec
106
106
  end
107
107
 
108
108
  describe '#allowize!' do
109
- [:stub, :stub!].each do |method|
110
- context "when it is `subject.#{method}(:method)` form" do
111
- let(:source) do
112
- <<-END
113
- describe 'example' do
114
- it 'responds to #foo' do
115
- subject.#{method}(:foo)
116
- end
109
+ context 'when it is `subject.stub(:method)` form' do
110
+ let(:source) do
111
+ <<-END
112
+ describe 'example' do
113
+ it 'responds to #foo' do
114
+ subject.stub(:foo)
117
115
  end
118
- END
119
- end
116
+ end
117
+ END
118
+ end
120
119
 
121
- let(:expected_source) do
122
- <<-END
123
- describe 'example' do
124
- it 'responds to #foo' do
125
- allow(subject).to receive(:foo)
126
- end
120
+ let(:expected_source) do
121
+ <<-END
122
+ describe 'example' do
123
+ it 'responds to #foo' do
124
+ allow(subject).to receive(:foo)
127
125
  end
128
- END
129
- end
126
+ end
127
+ END
128
+ end
130
129
 
131
- it 'converts into `allow(subject).to receive(:method)` form' do
132
- method_stub_object.allowize!
133
- rewritten_source.should == expected_source
134
- end
130
+ it 'converts into `allow(subject).to receive(:method)` form' do
131
+ method_stub_object.allowize!
132
+ rewritten_source.should == expected_source
133
+ end
135
134
 
136
- it "adds record \"`obj.#{method}(:message)` -> `allow(obj).to receive(:message)`\"" do
137
- method_stub_object.allowize!
138
- record.original_syntax.should == "obj.#{method}(:message)"
139
- record.converted_syntax.should == 'allow(obj).to receive(:message)'
140
- end
135
+ it "adds record \"`obj.stub(:message)` -> `allow(obj).to receive(:message)`\"" do
136
+ method_stub_object.allowize!
137
+ record.original_syntax.should == 'obj.stub(:message)'
138
+ record.converted_syntax.should == 'allow(obj).to receive(:message)'
139
+ end
141
140
 
142
- context 'and #allow and #receive are not available in the context' do
143
- context 'and the context is determinable statically' do
144
- let(:source) do
145
- <<-END
146
- describe 'example' do
147
- class TestRunner
148
- def run
149
- 'something'.#{method}(:foo)
150
- end
141
+ context 'and #allow and #receive are not available in the context' do
142
+ context 'and the context is determinable statically' do
143
+ let(:source) do
144
+ <<-END
145
+ describe 'example' do
146
+ class TestRunner
147
+ def run
148
+ 'something'.stub(:foo)
151
149
  end
150
+ end
152
151
 
153
- it 'responds to #foo' do
154
- TestRunner.new.run
155
- end
152
+ it 'responds to #foo' do
153
+ TestRunner.new.run
156
154
  end
157
- END
155
+ end
156
+ END
157
+ end
158
+
159
+ context 'with runtime information' do
160
+ include_context 'dynamic analysis objects'
161
+
162
+ it 'raises InvalidContextError' do
163
+ -> { method_stub_object.allowize! }.should raise_error(InvalidContextError)
164
+ end
165
+ end
166
+
167
+ context 'without runtime information' do
168
+ it 'raises InvalidContextError' do
169
+ -> { method_stub_object.allowize! }.should raise_error(InvalidContextError)
158
170
  end
171
+ end
172
+ end
159
173
 
160
- context 'with runtime information' do
161
- include_context 'dynamic analysis objects'
174
+ context 'and the context is not determinable statically' do
175
+ let(:source) do
176
+ <<-END
177
+ def my_eval(&block)
178
+ Object.new.instance_eval(&block)
179
+ end
162
180
 
163
- it 'raises InvalidContextError' do
164
- -> { method_stub_object.allowize! }.should raise_error(InvalidContextError)
181
+ describe 'example' do
182
+ it 'responds to #foo' do
183
+ my_eval { 'something'.stub(:foo) }
184
+ end
165
185
  end
186
+ END
187
+ end
188
+
189
+ context 'with runtime information' do
190
+ include_context 'dynamic analysis objects'
191
+
192
+ it 'raises InvalidContextError' do
193
+ -> { method_stub_object.allowize! }.should raise_error(InvalidContextError)
166
194
  end
195
+ end
167
196
 
168
- context 'without runtime information' do
169
- it 'raises InvalidContextError' do
170
- -> { method_stub_object.allowize! }.should raise_error(InvalidContextError)
171
- end
197
+ context 'without runtime information' do
198
+ it 'does not raise InvalidContextError' do
199
+ -> { method_stub_object.allowize! }.should_not raise_error
172
200
  end
173
201
  end
202
+ end
203
+ end
204
+ end
174
205
 
175
- context 'and the context is not determinable statically' do
176
- let(:source) do
177
- <<-END
178
- def my_eval(&block)
179
- Object.new.instance_eval(&block)
180
- end
206
+ context 'when it is `subject.stub!(:method)` form' do
207
+ let(:source) do
208
+ <<-END
209
+ describe 'example' do
210
+ it 'responds to #foo' do
211
+ subject.stub!(:foo)
212
+ end
213
+ end
214
+ END
215
+ end
181
216
 
182
- describe 'example' do
183
- it 'responds to #foo' do
184
- my_eval { 'something'.#{method}(:foo) }
185
- end
186
- end
187
- END
217
+ let(:expected_source) do
218
+ <<-END
219
+ describe 'example' do
220
+ it 'responds to #foo' do
221
+ allow(subject).to receive(:foo)
188
222
  end
223
+ end
224
+ END
225
+ end
189
226
 
190
- context 'with runtime information' do
191
- include_context 'dynamic analysis objects'
227
+ it 'converts into `allow(subject).to receive(:method)` form' do
228
+ method_stub_object.allowize!
229
+ rewritten_source.should == expected_source
230
+ end
192
231
 
193
- it 'raises InvalidContextError' do
194
- -> { method_stub_object.allowize! }.should raise_error(InvalidContextError)
195
- end
232
+ it "adds record \"`obj.stub!(:message)` -> `allow(obj).to receive(:message)`\"" do
233
+ method_stub_object.allowize!
234
+ record.original_syntax.should == 'obj.stub!(:message)'
235
+ record.converted_syntax.should == 'allow(obj).to receive(:message)'
236
+ end
237
+ end
238
+
239
+ context 'when it is `subject.stub(:method).and_return(value)` form' do
240
+ let(:source) do
241
+ <<-END
242
+ describe 'example' do
243
+ it 'responds to #foo and returns 1' do
244
+ subject.stub(:foo).and_return(1)
196
245
  end
246
+ end
247
+ END
248
+ end
197
249
 
198
- context 'without runtime information' do
199
- it 'does not raise InvalidContextError' do
200
- -> { method_stub_object.allowize! }.should_not raise_error
201
- end
250
+ let(:expected_source) do
251
+ <<-END
252
+ describe 'example' do
253
+ it 'responds to #foo and returns 1' do
254
+ allow(subject).to receive(:foo).and_return(1)
202
255
  end
203
256
  end
204
- end
257
+ END
205
258
  end
206
259
 
207
- context "when it is `subject.#{method}(:method).and_return(value)` form" do
208
- let(:source) do
209
- <<-END
210
- describe 'example' do
211
- it 'responds to #foo and returns 1' do
212
- subject.#{method}(:foo).and_return(1)
213
- end
260
+ it 'converts into `allow(subject).to receive(:method).and_return(value)` form' do
261
+ method_stub_object.allowize!
262
+ rewritten_source.should == expected_source
263
+ end
264
+ end
265
+
266
+ context 'when it is `subject.stub(:method).and_raise(RuntimeError)` form' do
267
+ let(:source) do
268
+ <<-END
269
+ describe 'example' do
270
+ it 'responds to #foo and raises RuntimeError' do
271
+ subject.stub(:foo).and_raise(RuntimeError)
214
272
  end
215
- END
216
- end
273
+ end
274
+ END
275
+ end
217
276
 
218
- let(:expected_source) do
219
- <<-END
220
- describe 'example' do
221
- it 'responds to #foo and returns 1' do
222
- allow(subject).to receive(:foo).and_return(1)
223
- end
277
+ let(:expected_source) do
278
+ <<-END
279
+ describe 'example' do
280
+ it 'responds to #foo and raises RuntimeError' do
281
+ allow(subject).to receive(:foo).and_raise(RuntimeError)
224
282
  end
225
- END
226
- end
283
+ end
284
+ END
285
+ end
227
286
 
228
- it 'converts into `allow(subject).to receive(:method).and_return(value)` form' do
229
- method_stub_object.allowize!
230
- rewritten_source.should == expected_source
231
- end
287
+ it 'converts into `allow(subject).to receive(:method).and_raise(RuntimeError)` form' do
288
+ method_stub_object.allowize!
289
+ rewritten_source.should == expected_source
290
+ end
291
+ end
292
+
293
+ context 'when the statement continues over multi lines' do
294
+ let(:source) do
295
+ <<-END
296
+ describe 'example' do
297
+ it 'responds to #foo and returns 1' do
298
+ subject.stub(
299
+ :foo
300
+ ).
301
+ and_return(
302
+ 1
303
+ )
304
+ end
305
+ end
306
+ END
232
307
  end
233
308
 
234
- context "when it is `subject.#{method}(:method).and_raise(RuntimeError)` form" do
309
+ let(:expected_source) do
310
+ <<-END
311
+ describe 'example' do
312
+ it 'responds to #foo and returns 1' do
313
+ allow(subject).to receive(
314
+ :foo
315
+ ).
316
+ and_return(
317
+ 1
318
+ )
319
+ end
320
+ end
321
+ END
322
+ end
323
+
324
+ it 'keeps the style as far as possible' do
325
+ method_stub_object.allowize!
326
+ rewritten_source.should == expected_source
327
+ end
328
+ end
329
+
330
+ context 'when it is `subject.stub(:method => value)` form' do
331
+ context 'and #receive_messages is available' do
235
332
  let(:source) do
236
333
  <<-END
237
334
  describe 'example' do
238
- it 'responds to #foo and raises RuntimeError' do
239
- subject.#{method}(:foo).and_raise(RuntimeError)
335
+ it 'responds to #foo and returns 1' do
336
+ subject.stub(:foo => 1)
240
337
  end
241
338
  end
242
339
  END
@@ -245,30 +342,32 @@ module Transpec
245
342
  let(:expected_source) do
246
343
  <<-END
247
344
  describe 'example' do
248
- it 'responds to #foo and raises RuntimeError' do
249
- allow(subject).to receive(:foo).and_raise(RuntimeError)
345
+ it 'responds to #foo and returns 1' do
346
+ allow(subject).to receive_messages(:foo => 1)
250
347
  end
251
348
  end
252
349
  END
253
350
  end
254
351
 
255
- it 'converts into `allow(subject).to receive(:method).and_raise(RuntimeError)` form' do
256
- method_stub_object.allowize!
352
+ it 'converts into `allow(subject).to receive_messages(:method => value)` form' do
353
+ method_stub_object.allowize!(true)
257
354
  rewritten_source.should == expected_source
258
355
  end
356
+
357
+ it 'adds record ' +
358
+ "\"`obj.stub(:message => value)` -> `allow(obj).to receive_messages(:message => value)`\"" do
359
+ method_stub_object.allowize!(true)
360
+ record.original_syntax.should == 'obj.stub(:message => value)'
361
+ record.converted_syntax.should == 'allow(obj).to receive_messages(:message => value)'
362
+ end
259
363
  end
260
364
 
261
- context 'when the statement continues over multi lines' do
365
+ context 'and #receive_messages is not available' do
262
366
  let(:source) do
263
367
  <<-END
264
368
  describe 'example' do
265
369
  it 'responds to #foo and returns 1' do
266
- subject.#{method}(
267
- :foo
268
- ).
269
- and_return(
270
- 1
271
- )
370
+ subject.stub(:foo => 1)
272
371
  end
273
372
  end
274
373
  END
@@ -278,30 +377,106 @@ module Transpec
278
377
  <<-END
279
378
  describe 'example' do
280
379
  it 'responds to #foo and returns 1' do
281
- allow(subject).to receive(
282
- :foo
283
- ).
284
- and_return(
285
- 1
286
- )
380
+ allow(subject).to receive(:foo).and_return(1)
287
381
  end
288
382
  end
289
383
  END
290
384
  end
291
385
 
292
- it 'keeps the style as far as possible' do
386
+ it 'converts into `allow(subject).to receive(:method).and_return(value)` form' do
293
387
  method_stub_object.allowize!
294
388
  rewritten_source.should == expected_source
295
389
  end
390
+
391
+ it 'adds record ' +
392
+ "\"`obj.stub(:message => value)` -> `allow(obj).to receive(:message).and_return(value)`\"" do
393
+ method_stub_object.allowize!
394
+ record.original_syntax.should == 'obj.stub(:message => value)'
395
+ record.converted_syntax.should == 'allow(obj).to receive(:message).and_return(value)'
396
+ end
296
397
  end
398
+ end
297
399
 
298
- context "when it is `subject.#{method}(:method => value)` form" do
400
+ context 'when it is `subject.stub(method: value)` form' do
401
+ let(:source) do
402
+ <<-END
403
+ describe 'example' do
404
+ it 'responds to #foo and returns 1' do
405
+ subject.stub(foo: 1)
406
+ end
407
+ end
408
+ END
409
+ end
410
+
411
+ let(:expected_source) do
412
+ <<-END
413
+ describe 'example' do
414
+ it 'responds to #foo and returns 1' do
415
+ allow(subject).to receive(:foo).and_return(1)
416
+ end
417
+ end
418
+ END
419
+ end
420
+
421
+ it 'converts into `allow(subject).to receive(:method).and_return(value)` form' do
422
+ method_stub_object.allowize!
423
+ rewritten_source.should == expected_source
424
+ end
425
+
426
+ it 'adds record ' +
427
+ "\"`obj.stub(:message => value)` -> `allow(obj).to receive(:message).and_return(value)`\"" do
428
+ method_stub_object.allowize!
429
+ record.original_syntax.should == 'obj.stub(:message => value)'
430
+ record.converted_syntax.should == 'allow(obj).to receive(:message).and_return(value)'
431
+ end
432
+ end
433
+
434
+ context 'when it is `subject.stub(:a_method => a_value, b_method => b_value)` form' do
435
+ let(:source) do
436
+ <<-END
437
+ describe 'example' do
438
+ it 'responds to #foo and returns 1, and responds to #bar and returns 2' do
439
+ subject.stub(:foo => 1, :bar => 2)
440
+ end
441
+ end
442
+ END
443
+ end
444
+
445
+ let(:expected_source) do
446
+ <<-END
447
+ describe 'example' do
448
+ it 'responds to #foo and returns 1, and responds to #bar and returns 2' do
449
+ allow(subject).to receive(:foo).and_return(1)
450
+ allow(subject).to receive(:bar).and_return(2)
451
+ end
452
+ end
453
+ END
454
+ end
455
+
456
+ it 'converts into `allow(subject).to receive(:a_method).and_return(a_value)` ' +
457
+ 'and `allow(subject).to receive(:b_method).and_return(b_value)` form' do
458
+ method_stub_object.allowize!
459
+ rewritten_source.should == expected_source
460
+ end
461
+
462
+ it 'adds record ' +
463
+ "\"`obj.stub(:message => value)` -> `allow(obj).to receive(:message).and_return(value)`\"" do
464
+ method_stub_object.allowize!
465
+ record.original_syntax.should == 'obj.stub(:message => value)'
466
+ record.converted_syntax.should == 'allow(obj).to receive(:message).and_return(value)'
467
+ end
468
+
469
+ context 'when the statement continues over multi lines' do
299
470
  context 'and #receive_messages is available' do
300
471
  let(:source) do
301
472
  <<-END
302
473
  describe 'example' do
303
- it 'responds to #foo and returns 1' do
304
- subject.#{method}(:foo => 1)
474
+ it 'responds to #foo and returns 1, and responds to #bar and returns 2' do
475
+ subject
476
+ .stub(
477
+ :foo => 1,
478
+ :bar => 2
479
+ )
305
480
  end
306
481
  end
307
482
  END
@@ -310,32 +485,33 @@ module Transpec
310
485
  let(:expected_source) do
311
486
  <<-END
312
487
  describe 'example' do
313
- it 'responds to #foo and returns 1' do
314
- allow(subject).to receive_messages(:foo => 1)
488
+ it 'responds to #foo and returns 1, and responds to #bar and returns 2' do
489
+ allow(subject)
490
+ .to receive_messages(
491
+ :foo => 1,
492
+ :bar => 2
493
+ )
315
494
  end
316
495
  end
317
496
  END
318
497
  end
319
498
 
320
- it 'converts into `allow(subject).to receive_messages(:method => value)` form' do
499
+ it 'keeps the style' do
321
500
  method_stub_object.allowize!(true)
322
501
  rewritten_source.should == expected_source
323
502
  end
324
-
325
- it 'adds record ' +
326
- "\"`obj.#{method}(:message => value)` -> `allow(obj).to receive_messages(:message => value)`\"" do
327
- method_stub_object.allowize!(true)
328
- record.original_syntax.should == "obj.#{method}(:message => value)"
329
- record.converted_syntax.should == 'allow(obj).to receive_messages(:message => value)'
330
- end
331
503
  end
332
504
 
333
505
  context 'and #receive_messages is not available' do
334
506
  let(:source) do
335
507
  <<-END
336
508
  describe 'example' do
337
- it 'responds to #foo and returns 1' do
338
- subject.#{method}(:foo => 1)
509
+ it 'responds to #foo and returns 1, and responds to #bar and returns 2' do
510
+ subject
511
+ .stub(
512
+ :foo => 1,
513
+ :bar => 2
514
+ )
339
515
  end
340
516
  end
341
517
  END
@@ -344,250 +520,218 @@ module Transpec
344
520
  let(:expected_source) do
345
521
  <<-END
346
522
  describe 'example' do
347
- it 'responds to #foo and returns 1' do
348
- allow(subject).to receive(:foo).and_return(1)
523
+ it 'responds to #foo and returns 1, and responds to #bar and returns 2' do
524
+ allow(subject)
525
+ .to receive(:foo).and_return(1)
526
+ allow(subject)
527
+ .to receive(:bar).and_return(2)
349
528
  end
350
529
  end
351
530
  END
352
531
  end
353
532
 
354
- it 'converts into `allow(subject).to receive(:method).and_return(value)` form' do
533
+ it 'keeps the style except around the hash' do
355
534
  method_stub_object.allowize!
356
535
  rewritten_source.should == expected_source
357
536
  end
358
-
359
- it 'adds record ' +
360
- "\"`obj.#{method}(:message => value)` -> `allow(obj).to receive(:message).and_return(value)`\"" do
361
- method_stub_object.allowize!
362
- record.original_syntax.should == "obj.#{method}(:message => value)"
363
- record.converted_syntax.should == 'allow(obj).to receive(:message).and_return(value)'
364
- end
365
537
  end
366
538
  end
539
+ end
540
+ end
367
541
 
368
- context "when it is `subject.#{method}(method: value)` form" do
369
- let(:source) do
370
- <<-END
371
- describe 'example' do
372
- it 'responds to #foo and returns 1' do
373
- subject.#{method}(foo: 1)
374
- end
375
- end
376
- END
377
- end
378
-
379
- let(:expected_source) do
380
- <<-END
381
- describe 'example' do
382
- it 'responds to #foo and returns 1' do
383
- allow(subject).to receive(:foo).and_return(1)
384
- end
542
+ [:unstub, :unstub!].each do |method|
543
+ context "when it is `subject.#{method}(:method)` form" do
544
+ let(:source) do
545
+ <<-END
546
+ describe 'example' do
547
+ it 'does not respond to #foo' do
548
+ subject.#{method}(:foo)
385
549
  end
386
- END
387
- end
388
-
389
- it 'converts into `allow(subject).to receive(:method).and_return(value)` form' do
390
- method_stub_object.allowize!
391
- rewritten_source.should == expected_source
392
- end
550
+ end
551
+ END
552
+ end
393
553
 
394
- it 'adds record ' +
395
- "\"`obj.#{method}(:message => value)` -> `allow(obj).to receive(:message).and_return(value)`\"" do
396
- method_stub_object.allowize!
397
- record.original_syntax.should == "obj.#{method}(:message => value)"
398
- record.converted_syntax.should == 'allow(obj).to receive(:message).and_return(value)'
399
- end
554
+ it 'does nothing' do
555
+ method_stub_object.allowize!
556
+ rewritten_source.should == source
400
557
  end
401
558
 
402
- context "when it is `subject.#{method}(:a_method => a_value, b_method => b_value)` form" do
403
- let(:source) do
404
- <<-END
405
- describe 'example' do
406
- it 'responds to #foo and returns 1, and responds to #bar and returns 2' do
407
- subject.#{method}(:foo => 1, :bar => 2)
408
- end
409
- end
410
- END
411
- end
559
+ it 'reports nothing' do
560
+ method_stub_object.allowize!
561
+ method_stub_object.report.records.should be_empty
562
+ end
563
+ end
564
+ end
412
565
 
413
- let(:expected_source) do
414
- <<-END
415
- describe 'example' do
416
- it 'responds to #foo and returns 1, and responds to #bar and returns 2' do
417
- allow(subject).to receive(:foo).and_return(1)
418
- allow(subject).to receive(:bar).and_return(2)
419
- end
420
- end
421
- END
566
+ context 'when it is `Klass.any_instance.stub(:method)` form' do
567
+ let(:source) do
568
+ <<-END
569
+ describe 'example' do
570
+ it 'responds to #foo' do
571
+ Klass.any_instance.stub(:foo)
572
+ end
422
573
  end
574
+ END
575
+ end
423
576
 
424
- it 'converts into `allow(subject).to receive(:a_method).and_return(a_value)` ' +
425
- 'and `allow(subject).to receive(:b_method).and_return(b_value)` form' do
426
- method_stub_object.allowize!
427
- rewritten_source.should == expected_source
577
+ let(:expected_source) do
578
+ <<-END
579
+ describe 'example' do
580
+ it 'responds to #foo' do
581
+ allow_any_instance_of(Klass).to receive(:foo)
582
+ end
428
583
  end
584
+ END
585
+ end
429
586
 
430
- it 'adds record ' +
431
- "\"`obj.#{method}(:message => value)` -> `allow(obj).to receive(:message).and_return(value)`\"" do
432
- method_stub_object.allowize!
433
- record.original_syntax.should == "obj.#{method}(:message => value)"
434
- record.converted_syntax.should == 'allow(obj).to receive(:message).and_return(value)'
435
- end
587
+ it 'converts into `allow_any_instance_of(Klass).to receive(:method)` form' do
588
+ method_stub_object.allowize!
589
+ rewritten_source.should == expected_source
590
+ end
436
591
 
437
- context 'when the statement continues over multi lines' do
438
- context 'and #receive_messages is available' do
439
- let(:source) do
440
- <<-END
441
- describe 'example' do
442
- it 'responds to #foo and returns 1, and responds to #bar and returns 2' do
443
- subject
444
- .#{method}(
445
- :foo => 1,
446
- :bar => 2
447
- )
448
- end
449
- end
450
- END
451
- end
452
-
453
- let(:expected_source) do
454
- <<-END
455
- describe 'example' do
456
- it 'responds to #foo and returns 1, and responds to #bar and returns 2' do
457
- allow(subject)
458
- .to receive_messages(
459
- :foo => 1,
460
- :bar => 2
461
- )
462
- end
463
- end
464
- END
465
- end
592
+ it "adds record \"`Klass.any_instance.stub(:message)` " +
593
+ '-> `allow_any_instance_of(obj).to receive(:message)`"' do
594
+ method_stub_object.allowize!
595
+ record.original_syntax.should == 'Klass.any_instance.stub(:message)'
596
+ record.converted_syntax.should == 'allow_any_instance_of(Klass).to receive(:message)'
597
+ end
466
598
 
467
- it 'keeps the style' do
468
- method_stub_object.allowize!(true)
469
- rewritten_source.should == expected_source
599
+ context 'when the statement continues over multi lines' do
600
+ let(:source) do
601
+ <<-END
602
+ describe 'example' do
603
+ it 'responds to #foo and returns 1' do
604
+ Klass
605
+ .any_instance
606
+ .stub(
607
+ :foo
608
+ ).
609
+ and_return(
610
+ 1
611
+ )
470
612
  end
471
613
  end
614
+ END
615
+ end
472
616
 
473
- context 'and #receive_messages is not available' do
474
- let(:source) do
475
- <<-END
476
- describe 'example' do
477
- it 'responds to #foo and returns 1, and responds to #bar and returns 2' do
478
- subject
479
- .#{method}(
480
- :foo => 1,
481
- :bar => 2
482
- )
483
- end
484
- end
485
- END
617
+ let(:expected_source) do
618
+ <<-END
619
+ describe 'example' do
620
+ it 'responds to #foo and returns 1' do
621
+ allow_any_instance_of(Klass)
622
+ .to receive(
623
+ :foo
624
+ ).
625
+ and_return(
626
+ 1
627
+ )
486
628
  end
629
+ end
630
+ END
631
+ end
487
632
 
488
- let(:expected_source) do
489
- <<-END
490
- describe 'example' do
491
- it 'responds to #foo and returns 1, and responds to #bar and returns 2' do
492
- allow(subject)
493
- .to receive(:foo).and_return(1)
494
- allow(subject)
495
- .to receive(:bar).and_return(2)
496
- end
497
- end
498
- END
499
- end
633
+ it 'keeps the style as far as possible' do
634
+ method_stub_object.allowize!
635
+ rewritten_source.should == expected_source
636
+ end
637
+ end
638
+ end
500
639
 
501
- it 'keeps the style except around the hash' do
502
- method_stub_object.allowize!
503
- rewritten_source.should == expected_source
504
- end
640
+ context 'when it is `described_class.any_instance.stub(:method)` form' do
641
+ let(:source) do
642
+ <<-END
643
+ describe 'example' do
644
+ it 'responds to #foo' do
645
+ described_class.any_instance.stub(:foo)
505
646
  end
506
647
  end
507
- end
648
+ END
508
649
  end
509
650
 
510
- [:unstub, :unstub!].each do |method|
511
- context "when it is `subject.#{method}(:method)` form" do
512
- let(:source) do
513
- <<-END
514
- describe 'example' do
515
- it 'does not respond to #foo' do
516
- subject.#{method}(:foo)
517
- end
518
- end
519
- END
651
+ let(:expected_source) do
652
+ <<-END
653
+ describe 'example' do
654
+ it 'responds to #foo' do
655
+ allow_any_instance_of(described_class).to receive(:foo)
656
+ end
520
657
  end
658
+ END
659
+ end
521
660
 
522
- it 'does nothing' do
523
- method_stub_object.allowize!
524
- rewritten_source.should == source
525
- end
661
+ it 'converts into `allow_any_instance_of(described_class).to receive(:method)` form' do
662
+ method_stub_object.allowize!
663
+ rewritten_source.should == expected_source
664
+ end
526
665
 
527
- it 'reports nothing' do
528
- method_stub_object.allowize!
529
- method_stub_object.report.records.should be_empty
530
- end
531
- end
666
+ it "adds record \"`Klass.any_instance.stub(:message)` " +
667
+ '-> `allow_any_instance_of(obj).to receive(:message)`"' do
668
+ method_stub_object.allowize!
669
+ record.original_syntax.should == 'Klass.any_instance.stub(:message)'
670
+ record.converted_syntax.should == 'allow_any_instance_of(Klass).to receive(:message)'
532
671
  end
672
+ end
533
673
 
534
- [:stub, :stub!].each do |method|
535
- context "when it is `SomeClass.any_instance.#{method}(:method)` form" do
536
- let(:source) do
537
- <<-END
538
- describe 'example' do
539
- it 'responds to #foo' do
540
- SomeClass.any_instance.#{method}(:foo)
541
- end
674
+ context 'when it is `variable.any_instance.stub(:method)` form ' +
675
+ 'and the variable is an AnyInstance::Recorder' do
676
+ context 'with runtime information' do
677
+ include_context 'dynamic analysis objects'
678
+
679
+ let(:source) do
680
+ <<-END
681
+ describe 'example' do
682
+ it 'responds to #foo' do
683
+ variable = String.any_instance
684
+ variable.stub(:foo)
542
685
  end
543
- END
544
- end
686
+ end
687
+ END
688
+ end
545
689
 
546
- let(:expected_source) do
547
- <<-END
548
- describe 'example' do
549
- it 'responds to #foo' do
550
- allow_any_instance_of(SomeClass).to receive(:foo)
551
- end
690
+ let(:expected_source) do
691
+ <<-END
692
+ describe 'example' do
693
+ it 'responds to #foo' do
694
+ variable = String.any_instance
695
+ allow_any_instance_of(String).to receive(:foo)
552
696
  end
553
- END
554
- end
697
+ end
698
+ END
699
+ end
555
700
 
556
- it 'converts into `allow_any_instance_of(SomeClass).to receive(:method)` form' do
557
- method_stub_object.allowize!
558
- rewritten_source.should == expected_source
559
- end
701
+ it 'converts into `allow_any_instance_of(Klass).to receive(:method)` form' do
702
+ method_stub_object.allowize!
703
+ rewritten_source.should == expected_source
704
+ end
560
705
 
561
- it "adds record \"`SomeClass.any_instance.#{method}(:message)` " +
562
- '-> `allow_any_instance_of(obj).to receive(:message)`"' do
563
- method_stub_object.allowize!
564
- record.original_syntax.should == "SomeClass.any_instance.#{method}(:message)"
565
- record.converted_syntax.should == 'allow_any_instance_of(SomeClass).to receive(:message)'
566
- end
706
+ it "adds record \"`Klass.any_instance.stub(:message)` " +
707
+ '-> `allow_any_instance_of(obj).to receive(:message)`"' do
708
+ method_stub_object.allowize!
709
+ record.original_syntax.should == 'Klass.any_instance.stub(:message)'
710
+ record.converted_syntax.should == 'allow_any_instance_of(Klass).to receive(:message)'
567
711
  end
568
712
  end
713
+ end
569
714
 
570
- [:unstub, :unstub!].each do |method|
571
- context "when it is `SomeClass.any_instance.#{method}(:method)` form" do
572
- let(:source) do
573
- <<-END
574
- describe 'example' do
575
- it 'does not respond to #foo' do
576
- SomeClass.any_instance.#{method}(:foo)
577
- end
715
+ [:unstub, :unstub!].each do |method|
716
+ context "when it is `Klass.any_instance.#{method}(:method)` form" do
717
+ let(:source) do
718
+ <<-END
719
+ describe 'example' do
720
+ it 'does not respond to #foo' do
721
+ Klass.any_instance.#{method}(:foo)
578
722
  end
579
- END
580
- end
723
+ end
724
+ END
725
+ end
581
726
 
582
- it 'does nothing' do
583
- method_stub_object.allowize!
584
- rewritten_source.should == source
585
- end
727
+ it 'does nothing' do
728
+ method_stub_object.allowize!
729
+ rewritten_source.should == source
730
+ end
586
731
 
587
- it 'reports nothing' do
588
- method_stub_object.allowize!
589
- method_stub_object.report.records.should be_empty
590
- end
732
+ it 'reports nothing' do
733
+ method_stub_object.allowize!
734
+ method_stub_object.report.records.should be_empty
591
735
  end
592
736
  end
593
737
  end