spy 0.1.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/.travis.yml +5 -0
  2. data/Gemfile +1 -0
  3. data/README.md +22 -7
  4. data/Rakefile +2 -0
  5. data/lib/spy.rb +39 -6
  6. data/lib/spy/agency.rb +42 -27
  7. data/lib/spy/call_log.rb +26 -0
  8. data/lib/spy/constant.rb +72 -14
  9. data/lib/spy/core_ext/marshal.rb +1 -0
  10. data/lib/spy/double.rb +17 -0
  11. data/lib/spy/nest.rb +27 -0
  12. data/lib/spy/subroutine.rb +146 -44
  13. data/lib/spy/version.rb +1 -1
  14. data/spec/spy/any_instance_spec.rb +518 -0
  15. data/spec/spy/mock_spec.rb +46 -554
  16. data/spec/spy/mutate_const_spec.rb +21 -63
  17. data/spec/spy/null_object_mock_spec.rb +11 -39
  18. data/spec/spy/partial_mock_spec.rb +3 -62
  19. data/spec/spy/stash_spec.rb +30 -37
  20. data/spec/spy/stub_spec.rb +0 -6
  21. data/spec/spy/to_ary_spec.rb +5 -5
  22. data/test/integration/test_constant_spying.rb +1 -1
  23. data/test/integration/test_instance_method.rb +32 -0
  24. data/test/integration/test_subroutine_spying.rb +7 -4
  25. data/test/spy/test_double.rb +4 -0
  26. data/test/spy/test_subroutine.rb +28 -3
  27. data/test/support/pen.rb +15 -0
  28. metadata +8 -30
  29. data/spec/spy/bug_report_10260_spec.rb +0 -8
  30. data/spec/spy/bug_report_10263_spec.rb +0 -24
  31. data/spec/spy/bug_report_496_spec.rb +0 -18
  32. data/spec/spy/bug_report_600_spec.rb +0 -24
  33. data/spec/spy/bug_report_7611_spec.rb +0 -16
  34. data/spec/spy/bug_report_8165_spec.rb +0 -31
  35. data/spec/spy/bug_report_830_spec.rb +0 -21
  36. data/spec/spy/bug_report_957_spec.rb +0 -22
  37. data/spec/spy/double_spec.rb +0 -12
  38. data/spec/spy/failing_argument_matchers_spec.rb +0 -94
  39. data/spec/spy/options_hash_spec.rb +0 -35
  40. data/spec/spy/precise_counts_spec.rb +0 -68
  41. data/spec/spy/stubbed_message_expectations_spec.rb +0 -47
  42. data/spec/spy/test_double_spec.rb +0 -54
@@ -12,169 +12,29 @@ module Spy
12
12
  expect(Spy.double("stuff")).not_to respond_to(:method_missing)
13
13
  end
14
14
 
15
- it "reports line number of expectation of unreceived message" do
16
- expected_error_line = __LINE__; @double.should_receive(:wont_happen).with("x", 3)
17
- begin
18
- @double.rspec_verify
19
- violated
20
- rescue RSpec::Mocks::MockExpectationError => e
21
- # NOTE - this regexp ended w/ $, but jruby adds extra info at the end of the line
22
- expect(e.backtrace[0]).to match(/#{File.basename(__FILE__)}:#{expected_error_line}/)
23
- end
24
- end
25
-
26
- it "reports line number of expectation of unreceived message after #should_receive after similar stub" do
27
- Spy.on(@double, :wont_happen)
28
- expected_error_line = __LINE__; @double.should_receive(:wont_happen).with("x", 3)
29
- begin
30
- @double.rspec_verify
31
- violated
32
- rescue RSpec::Mocks::MockExpectationError => e
33
- # NOTE - this regexp ended w/ $, but jruby adds extra info at the end of the line
34
- expect(e.backtrace[0]).to match(/#{File.basename(__FILE__)}:#{expected_error_line}/)
35
- end
36
- end
37
-
38
- it "passes when not receiving message specified as not to be received" do
39
- @double.should_not_receive(:not_expected)
40
- @double.rspec_verify
41
- end
42
-
43
- it "warns when should_not_receive is followed by and_return" do
44
- RSpec::Mocks.should_receive(:warn_deprecation).
45
- with(/`and_return` with `should_not_receive` is deprecated/)
46
-
47
- @double.should_not_receive(:do_something).and_return(1)
48
- end
49
-
50
- it "passes when receiving message specified as not to be received with different args" do
51
- @double.should_not_receive(:message).with("unwanted text")
52
- @double.should_receive(:message).with("other text")
53
- @double.message "other text"
54
- @double.rspec_verify
55
- end
56
-
57
15
  it "fails when receiving message specified as not to be received" do
58
- @double.should_not_receive(:not_expected)
59
- expect {
60
- @double.not_expected
61
- violated
62
- }.to raise_error(
63
- RSpec::Mocks::MockExpectationError,
64
- %Q|(Double "test double").not_expected(no args)\n expected: 0 times\n received: 1 time|
65
- )
66
- end
67
-
68
- it "fails when receiving message specified as not to be received with args" do
69
- @double.should_not_receive(:not_expected).with("unexpected text")
70
- expect {
71
- @double.not_expected("unexpected text")
72
- violated
73
- }.to raise_error(
74
- RSpec::Mocks::MockExpectationError,
75
- %Q|(Double "test double").not_expected("unexpected text")\n expected: 0 times\n received: 1 time|
76
- )
77
- end
78
-
79
- it "passes when receiving message specified as not to be received with wrong args" do
80
- @double.should_not_receive(:not_expected).with("unexpected text")
81
- @double.not_expected "really unexpected text"
82
- @double.rspec_verify
83
- end
84
-
85
- it "allows block to calculate return values" do
86
- @double.should_receive(:something).with("a","b","c").and_return { |a,b,c| c+b+a }
87
- expect(@double.something("a","b","c")).to eq "cba"
88
- @double.rspec_verify
89
- end
90
-
91
- it "allows parameter as return value" do
92
- @double.should_receive(:something).with("a","b","c").and_return("booh")
93
- expect(@double.something("a","b","c")).to eq "booh"
94
- @double.rspec_verify
95
- end
96
-
97
- it "returns the previously stubbed value if no return value is set" do
98
- Spy.on(@double, :something).with("a","b","c").and_return(:stubbed_value)
99
- @double.should_receive(:something).with("a","b","c")
100
- expect(@double.something("a","b","c")).to eq :stubbed_value
101
- @double.rspec_verify
102
- end
103
-
104
- it "returns nil if no return value is set and there is no previously stubbed value" do
105
- @double.should_receive(:something).with("a","b","c")
106
- expect(@double.something("a","b","c")).to be_nil
107
- @double.rspec_verify
108
- end
109
-
110
- it "raises exception if args don't match when method called" do
111
- @double.should_receive(:something).with("a","b","c").and_return("booh")
112
- expect {
113
- @double.something("a","d","c")
114
- violated
115
- }.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :something with unexpected arguments\n expected: (\"a\", \"b\", \"c\")\n got: (\"a\", \"d\", \"c\")")
116
- end
117
-
118
- describe "even when a similar expectation with different arguments exist" do
119
- it "raises exception if args don't match when method called, correctly reporting the offending arguments" do
120
- @double.should_receive(:something).with("a","b","c").once
121
- @double.should_receive(:something).with("z","x","c").once
122
- expect {
123
- @double.something("a","b","c")
124
- @double.something("z","x","g")
125
- }.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :something with unexpected arguments\n expected: (\"z\", \"x\", \"c\")\n got: (\"z\", \"x\", \"g\")")
126
- end
127
- end
128
-
129
- it "raises exception if args don't match when method called even when the method is stubbed" do
130
- Spy.on(@double, :something)
131
- @double.should_receive(:something).with("a","b","c")
132
- expect {
133
- @double.something("a","d","c")
134
- @double.rspec_verify
135
- }.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :something with unexpected arguments\n expected: (\"a\", \"b\", \"c\")\n got: (\"a\", \"d\", \"c\")")
136
- end
137
-
138
- it "raises exception if args don't match when method called even when using null_object" do
139
- @double = double("test double").as_null_object
140
- @double.should_receive(:something).with("a","b","c")
141
- expect {
142
- @double.something("a","d","c")
143
- @double.rspec_verify
144
- }.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :something with unexpected arguments\n expected: (\"a\", \"b\", \"c\")\n got: (\"a\", \"d\", \"c\")")
145
- end
146
-
147
- describe 'with a method that has a default argument' do
148
- it "raises an exception if the arguments don't match when the method is called, correctly reporting the offending arguments" do
149
- def @double.method_with_default_argument(arg={}); end
150
- @double.should_receive(:method_with_default_argument).with({})
151
-
152
- expect {
153
- @double.method_with_default_argument(nil)
154
- @double.rspec_verify
155
- }.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :method_with_default_argument with unexpected arguments\n expected: ({})\n got: (nil)")
156
- end
16
+ spy = Spy.on(@double, :not_expected)
17
+ expect(spy).to_not have_been_called
157
18
  end
158
19
 
159
20
  it "fails if unexpected method called" do
160
21
  expect {
161
22
  @double.something("a","b","c")
162
- violated
163
- }.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received unexpected message :something with (\"a\", \"b\", \"c\")")
23
+ }.to raise_error
164
24
  end
165
25
 
166
26
  it "uses block for expectation if provided" do
167
- @double.should_receive(:something) do | a, b |
27
+ spy = Spy.on(@double, :something).and_return do | a, b |
168
28
  expect(a).to eq "a"
169
29
  expect(b).to eq "b"
170
30
  "booh"
171
31
  end
172
32
  expect(@double.something("a", "b")).to eq "booh"
173
- @double.rspec_verify
33
+ expect(spy).to have_been_called
174
34
  end
175
35
 
176
36
  it "fails if expectation block fails" do
177
- @double.should_receive(:something) do |bool|
37
+ Spy.on(@double, :something).and_return do | bool|
178
38
  expect(bool).to be_true
179
39
  end
180
40
 
@@ -183,32 +43,40 @@ module Spy
183
43
  }.to raise_error(RSpec::Expectations::ExpectationNotMetError)
184
44
  end
185
45
 
186
- context "with Ruby > 1.8.6", :unless => RUBY_VERSION.to_s == '1.8.6' do
187
- it "passes proc to expectation block without an argument" do
188
- # We eval this because Ruby 1.8.6's syntax parser barfs on { |&block| ... }
189
- # and prevents the entire spec suite from running.
190
- eval("@double.should_receive(:foo) {|&block| expect(block.call).to eq(:bar)}")
191
- @double.foo { :bar }
46
+ it "passes proc to expectation block without an argument" do
47
+ spy = Spy.on(@double, :foo).and_return do |&block|
48
+ expect(block.call).to eq(:bar)
192
49
  end
50
+ @double.foo { :bar }
51
+ expect(spy).to have_been_called
52
+ end
193
53
 
194
- it "passes proc to expectation block with an argument" do
195
- eval("@double.should_receive(:foo) {|arg, &block| expect(block.call).to eq(:bar)}")
196
- @double.foo(:arg) { :bar }
54
+ it "passes proc to expectation block with an argument" do
55
+ spy = Spy.on(@double, :foo).and_return do |arg, &block|
56
+ expect(block.call).to eq(:bar)
197
57
  end
58
+ @double.foo(:arg) { :bar }
59
+ expect(spy).to have_been_called
60
+ end
198
61
 
199
- it "passes proc to stub block without an argurment" do
200
- eval("Spy.on(@double, :foo) {|&block| expect(block.call).to eq(:bar)}")
201
- @double.foo { :bar }
62
+ it "passes proc to stub block without an argurment" do
63
+ spy = Spy.on(@double, :foo).and_return do |&block|
64
+ expect(block.call).to eq(:bar)
202
65
  end
66
+ @double.foo { :bar }
67
+ expect(spy).to have_been_called
68
+ end
203
69
 
204
- it "passes proc to stub block with an argument" do
205
- eval("Spy.on(@double, :foo) {|arg, &block| expect(block.call).to eq(:bar)}")
206
- @double.foo(:arg) { :bar }
70
+ it "passes proc to stub block with an argument" do
71
+ spy = Spy.on(@double, :foo) do |arg, &block|
72
+ expect(block.call).to eq(:bar)
207
73
  end
74
+ @double.foo(:arg) { :bar }
75
+ expect(spy).to have_been_called
208
76
  end
209
77
 
210
78
  it "fails right away when method defined as never is received" do
211
- @double.should_receive(:not_expected).never
79
+ Spy.on(@double, :not_expected).never
212
80
  expect { @double.not_expected }.
213
81
  to raise_error(RSpec::Mocks::MockExpectationError,
214
82
  %Q|(Double "test double").not_expected(no args)\n expected: 0 times\n received: 1 time|
@@ -216,27 +84,27 @@ module Spy
216
84
  end
217
85
 
218
86
  it "raises RuntimeError by default" do
219
- @double.should_receive(:something).and_raise
87
+ Spy.on(@double, :something).and_raise
220
88
  expect { @double.something }.to raise_error(RuntimeError)
221
89
  end
222
90
 
223
91
  it "raises RuntimeError with a message by default" do
224
- @double.should_receive(:something).and_raise("error message")
92
+ Spy.on(@double, :something).and_raise("error message")
225
93
  expect { @double.something }.to raise_error(RuntimeError, "error message")
226
94
  end
227
95
 
228
96
  it "raises an exception of a given type without an error message" do
229
- @double.should_receive(:something).and_raise(StandardError)
97
+ Spy.on(@double, :something).and_raise(StandardError)
230
98
  expect { @double.something }.to raise_error(StandardError)
231
99
  end
232
100
 
233
101
  it "raises an exception of a given type with a message" do
234
- @double.should_receive(:something).and_raise(RuntimeError, "error message")
102
+ Spy.on(@double, :something).and_raise(RuntimeError, "error message")
235
103
  expect { @double.something }.to raise_error(RuntimeError, "error message")
236
104
  end
237
105
 
238
106
  it "raises a given instance of an exception" do
239
- @double.should_receive(:something).and_raise(RuntimeError.new("error message"))
107
+ Spy.on(@double, :something).and_raise(RuntimeError.new("error message"))
240
108
  expect { @double.something }.to raise_error(RuntimeError, "error message")
241
109
  end
242
110
 
@@ -249,7 +117,7 @@ module Spy
249
117
  end
250
118
 
251
119
  it "raises a given instance of an exception with arguments other than the standard 'message'" do
252
- @double.should_receive(:something).and_raise(OutOfGas.new(2, :oz))
120
+ Spy.on(@double, :something).and_raise(OutOfGas.new(2, :oz))
253
121
 
254
122
  begin
255
123
  @double.something
@@ -260,172 +128,22 @@ module Spy
260
128
  end
261
129
  end
262
130
 
263
- it "does not raise when told to if args dont match" do
264
- @double.should_receive(:something).with(2).and_raise(RuntimeError)
265
- expect {
266
- @double.something 1
267
- }.to raise_error(RSpec::Mocks::MockExpectationError)
268
- end
269
-
270
131
  it "throws when told to" do
271
- @double.should_receive(:something).and_throw(:blech)
132
+ Spy.on(@double, :something).and_throw(:blech)
272
133
  expect {
273
134
  @double.something
274
135
  }.to throw_symbol(:blech)
275
136
  end
276
137
 
277
- it "ignores args on any args" do
278
- @double.should_receive(:something).at_least(:once).with(any_args)
279
- @double.something
280
- @double.something 1
281
- @double.something "a", 2
282
- @double.something [], {}, "joe", 7
283
- @double.rspec_verify
284
- end
285
-
286
- it "fails on no args if any args received" do
287
- @double.should_receive(:something).with(no_args())
288
- expect {
289
- @double.something 1
290
- }.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :something with unexpected arguments\n expected: (no args)\n got: (1)")
291
- end
292
-
293
- it "fails when args are expected but none are received" do
294
- @double.should_receive(:something).with(1)
295
- expect {
296
- @double.something
297
- }.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :something with unexpected arguments\n expected: (1)\n got: (no args)")
298
- end
299
-
300
138
  it "returns value from block by default" do
301
- Spy.on(@double, :method_that_yields).and_yield
139
+ spy = Spy.on(@double, :method_that_yields).and_yield
302
140
  value = @double.method_that_yields { :returned_obj }
303
141
  expect(value).to eq :returned_obj
304
- @double.rspec_verify
305
- end
306
-
307
- it "yields 0 args to blocks that take a variable number of arguments" do
308
- @double.should_receive(:yield_back).with(no_args()).once.and_yield
309
- a = nil
310
- @double.yield_back {|*x| a = x}
311
- expect(a).to eq []
312
- @double.rspec_verify
313
- end
314
-
315
- it "yields 0 args multiple times to blocks that take a variable number of arguments" do
316
- @double.should_receive(:yield_back).once.with(no_args()).once.and_yield.
317
- and_yield
318
- b = []
319
- @double.yield_back {|*a| b << a}
320
- expect(b).to eq [ [], [] ]
321
- @double.rspec_verify
322
- end
323
-
324
- it "yields one arg to blocks that take a variable number of arguments" do
325
- @double.should_receive(:yield_back).with(no_args()).once.and_yield(99)
326
- a = nil
327
- @double.yield_back {|*x| a = x}
328
- expect(a).to eq [99]
329
- @double.rspec_verify
330
- end
331
-
332
- it "yields one arg 3 times consecutively to blocks that take a variable number of arguments" do
333
- @double.should_receive(:yield_back).once.with(no_args()).once.and_yield(99).
334
- and_yield(43).
335
- and_yield("something fruity")
336
- b = []
337
- @double.yield_back {|*a| b << a}
338
- expect(b).to eq [[99], [43], ["something fruity"]]
339
- @double.rspec_verify
340
- end
341
-
342
- it "yields many args to blocks that take a variable number of arguments" do
343
- @double.should_receive(:yield_back).with(no_args()).once.and_yield(99, 27, "go")
344
- a = nil
345
- @double.yield_back {|*x| a = x}
346
- expect(a).to eq [99, 27, "go"]
347
- @double.rspec_verify
348
- end
349
-
350
- it "yields many args 3 times consecutively to blocks that take a variable number of arguments" do
351
- @double.should_receive(:yield_back).once.with(no_args()).once.and_yield(99, :green, "go").
352
- and_yield("wait", :amber).
353
- and_yield("stop", 12, :red)
354
- b = []
355
- @double.yield_back {|*a| b << a}
356
- expect(b).to eq [[99, :green, "go"], ["wait", :amber], ["stop", 12, :red]]
357
- @double.rspec_verify
358
- end
359
-
360
- it "yields single value" do
361
- @double.should_receive(:yield_back).with(no_args()).once.and_yield(99)
362
- a = nil
363
- @double.yield_back {|x| a = x}
364
- expect(a).to eq 99
365
- @double.rspec_verify
366
- end
367
-
368
- it "yields single value 3 times consecutively" do
369
- @double.should_receive(:yield_back).once.with(no_args()).once.and_yield(99).
370
- and_yield(43).
371
- and_yield("something fruity")
372
- b = []
373
- @double.yield_back {|a| b << a}
374
- expect(b).to eq [99, 43, "something fruity"]
375
- @double.rspec_verify
376
- end
377
-
378
- it "yields two values" do
379
- @double.should_receive(:yield_back).with(no_args()).once.and_yield('wha', 'zup')
380
- a, b = nil
381
- @double.yield_back {|x,y| a=x; b=y}
382
- expect(a).to eq 'wha'
383
- expect(b).to eq 'zup'
384
- @double.rspec_verify
385
- end
386
-
387
- it "yields two values 3 times consecutively" do
388
- @double.should_receive(:yield_back).once.with(no_args()).once.and_yield('wha', 'zup').
389
- and_yield('not', 'down').
390
- and_yield(14, 65)
391
- c = []
392
- @double.yield_back {|a,b| c << [a, b]}
393
- expect(c).to eq [['wha', 'zup'], ['not', 'down'], [14, 65]]
394
- @double.rspec_verify
395
- end
396
-
397
- it "fails when calling yielding method with wrong arity" do
398
- @double.should_receive(:yield_back).with(no_args()).once.and_yield('wha', 'zup')
399
- expect {
400
- @double.yield_back {|a|}
401
- }.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" yielded |\"wha\", \"zup\"| to block with arity of 1")
402
- end
403
-
404
- it "fails when calling yielding method consecutively with wrong arity" do
405
- @double.should_receive(:yield_back).once.with(no_args()).once.and_yield('wha', 'zup').
406
- and_yield('down').
407
- and_yield(14, 65)
408
- expect {
409
- c = []
410
- @double.yield_back {|a,b| c << [a, b]}
411
- }.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" yielded |\"down\"| to block with arity of 2")
412
- end
413
-
414
- it "fails when calling yielding method without block" do
415
- @double.should_receive(:yield_back).with(no_args()).once.and_yield('wha', 'zup')
416
- expect {
417
- @double.yield_back
418
- }.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" asked to yield |[\"wha\", \"zup\"]| but no block was passed")
419
- end
420
-
421
- it "is able to double send" do
422
- @double.should_receive(:send).with(any_args)
423
- @double.send 'hi'
424
- @double.rspec_verify
142
+ expect(spy).to have_been_called
425
143
  end
426
144
 
427
145
  it "is able to raise from method calling yielding double" do
428
- @double.should_receive(:yield_me).and_yield 44
146
+ spy = Spy.on(@double, :yield_me).and_yield 44
429
147
 
430
148
  expect {
431
149
  @double.yield_me do |x|
@@ -433,118 +151,7 @@ module Spy
433
151
  end
434
152
  }.to raise_error(StandardError, "Bang")
435
153
 
436
- @double.rspec_verify
437
- end
438
-
439
- it "clears expectations after verify" do
440
- @double.should_receive(:foobar)
441
- @double.foobar
442
- @double.rspec_verify
443
- expect {
444
- @double.foobar
445
- }.to raise_error(RSpec::Mocks::MockExpectationError, %q|Double "test double" received unexpected message :foobar with (no args)|)
446
- end
447
-
448
- it "restores objects to their original state on rspec_reset" do
449
- double = double("this is a double")
450
- double.should_receive(:blah)
451
- double.rspec_reset
452
- double.rspec_verify #should throw if reset didn't work
453
- end
454
-
455
- it "works even after method_missing starts raising NameErrors instead of NoMethodErrors" do
456
- # Object#method_missing throws either NameErrors or NoMethodErrors.
457
- #
458
- # On a fresh ruby program Object#method_missing:
459
- # * raises a NoMethodError when called directly
460
- # * raises a NameError when called indirectly
461
- #
462
- # Once Object#method_missing has been called at least once (on any object)
463
- # it starts behaving differently:
464
- # * raises a NameError when called directly
465
- # * raises a NameError when called indirectly
466
- #
467
- # There was a bug in Mock#method_missing that relied on the fact
468
- # that calling Object#method_missing directly raises a NoMethodError.
469
- # This example tests that the bug doesn't exist anymore.
470
-
471
-
472
- # Ensures that method_missing always raises NameErrors.
473
- a_method_that_doesnt_exist rescue
474
-
475
-
476
- @double.should_receive(:foobar)
477
- @double.foobar
478
- @double.rspec_verify
479
-
480
- expect { @double.foobar }.to_not raise_error(NameError)
481
- expect { @double.foobar }.to raise_error(RSpec::Mocks::MockExpectationError)
482
- end
483
-
484
- it "temporarily replaces a method stub on a double" do
485
- Spy.on(@double, :msg).and_return(:stub_value)
486
- @double.should_receive(:msg).with(:arg).and_return(:double_value)
487
- expect(@double.msg(:arg)).to equal(:double_value)
488
- expect(@double.msg).to equal(:stub_value)
489
- expect(@double.msg).to equal(:stub_value)
490
- @double.rspec_verify
491
- end
492
-
493
- it "does not require a different signature to replace a method stub" do
494
- Spy.on(@double, :msg).and_return(:stub_value)
495
- @double.should_receive(:msg).and_return(:double_value)
496
- expect(@double.msg(:arg)).to equal(:double_value)
497
- expect(@double.msg).to equal(:stub_value)
498
- expect(@double.msg).to equal(:stub_value)
499
- @double.rspec_verify
500
- end
501
-
502
- it "raises an error when a previously stubbed method has a negative expectation" do
503
- Spy.on(@double, :msg).and_return(:stub_value)
504
- @double.should_not_receive(:msg)
505
- expect { @double.msg(:arg) }.to raise_error(RSpec::Mocks::MockExpectationError)
506
- end
507
-
508
- it "temporarily replaces a method stub on a non-double" do
509
- non_double = Object.new
510
- Spy.on(non_double, :msg).and_return(:stub_value)
511
- non_double.should_receive(:msg).with(:arg).and_return(:double_value)
512
- expect(non_double.msg(:arg)).to equal(:double_value)
513
- expect(non_double.msg).to equal(:stub_value)
514
- expect(non_double.msg).to equal(:stub_value)
515
- non_double.rspec_verify
516
- end
517
-
518
- it "returns the stubbed value when no new value specified" do
519
- Spy.on(@double, :msg).and_return(:stub_value)
520
- @double.should_receive(:msg)
521
- expect(@double.msg).to equal(:stub_value)
522
- @double.rspec_verify
523
- end
524
-
525
- it "returns the stubbed value when stubbed with args and no new value specified" do
526
- Spy.on(@double, :msg).with(:arg).and_return(:stub_value)
527
- @double.should_receive(:msg).with(:arg)
528
- expect(@double.msg(:arg)).to equal(:stub_value)
529
- @double.rspec_verify
530
- end
531
-
532
- it "does not mess with the stub's yielded values when also doubleed" do
533
- Spy.on(@double, :yield_back).and_yield(:stub_value)
534
- @double.should_receive(:yield_back).and_yield(:double_value)
535
- @double.yield_back{|v| expect(v).to eq :double_value }
536
- @double.yield_back{|v| expect(v).to eq :stub_value }
537
- @double.rspec_verify
538
- end
539
-
540
- it "yields multiple values after a similar stub" do
541
- FSpy.on(ile, :open).and_yield(:stub_value)
542
- File.should_receive(:open).and_yield(:first_call).and_yield(:second_call)
543
- yielded_args = []
544
- File.open {|v| yielded_args << v }
545
- expect(yielded_args).to eq [:first_call, :second_call]
546
- File.open {|v| expect(v).to eq :stub_value }
547
- File.rspec_verify
154
+ expect(spy).to have_been_called
548
155
  end
549
156
 
550
157
  it "assigns stub return values" do
@@ -565,63 +172,12 @@ module Spy
565
172
  end
566
173
 
567
174
  it "calls the block after #should_receive" do
568
- @double.should_receive(:foo) { add_call }
175
+ spy = Spy.on(@double, :foo).and_return { add_call }
569
176
 
570
177
  @double.foo
571
178
 
572
179
  expect(@calls).to eq 1
573
- end
574
-
575
- it "calls the block after #should_receive after a similar stub" do
576
- Spy.on(@double, :foo).and_return(:bar)
577
- @double.should_receive(:foo) { add_call }
578
-
579
- @double.foo
580
-
581
- expect(@calls).to eq 1
582
- end
583
-
584
- it "calls the block after #once" do
585
- @double.should_receive(:foo).once { add_call }
586
-
587
- @double.foo
588
-
589
- expect(@calls).to eq 1
590
- end
591
-
592
- it "calls the block after #twice" do
593
- @double.should_receive(:foo).twice { add_call }
594
-
595
- @double.foo
596
- @double.foo
597
-
598
- expect(@calls).to eq 2
599
- end
600
-
601
- it "calls the block after #times" do
602
- @double.should_receive(:foo).exactly(10).times { add_call }
603
-
604
- (1..10).each { @double.foo }
605
-
606
- expect(@calls).to eq 10
607
- end
608
-
609
- it "calls the block after #any_number_of_times" do
610
- @double.should_receive(:foo).any_number_of_times { add_call }
611
-
612
- (1..7).each { @double.foo }
613
-
614
- expect(@calls).to eq 7
615
- end
616
-
617
- it "calls the block after #ordered" do
618
- @double.should_receive(:foo).ordered { add_call }
619
- @double.should_receive(:bar).ordered { add_call }
620
-
621
- @double.foo
622
- @double.bar
623
-
624
- expect(@calls).to eq 2
180
+ expect(spy).to have_been_called
625
181
  end
626
182
  end
627
183
 
@@ -647,7 +203,7 @@ module Spy
647
203
  end
648
204
 
649
205
  it "does respond to initially stubbed methods" do
650
- double = Spy.double(:foo => "woo", :bar => "car")
206
+ double = Spy.double("name", :foo => "woo", :bar => "car")
651
207
  expect(double.foo).to eq "woo"
652
208
  expect(double.bar).to eq "car"
653
209
  end
@@ -658,73 +214,9 @@ module Spy
658
214
  first = Spy.double('first')
659
215
  second = Spy.double('second')
660
216
 
661
- first.should_receive(:==).with(second)
217
+ spy = Spy.on(first, :==)
662
218
  second == first
663
- end
664
- end
665
-
666
- describe "with" do
667
- before { @double = Spy.double('double') }
668
- context "with args" do
669
- context "with matching args" do
670
- it "passes" do
671
- @double.should_receive(:foo).with('bar')
672
- @double.foo('bar')
673
- end
674
- end
675
-
676
- context "with non-matching args" do
677
- it "fails" do
678
- @double.should_receive(:foo).with('bar')
679
- expect do
680
- @double.foo('baz')
681
- end.to raise_error
682
- @double.rspec_reset
683
- end
684
- end
685
-
686
- context "with non-matching doubles" do
687
- it "fails" do
688
- d1 = Spy.double('1')
689
- d2 = Spy.double('2')
690
- @double.should_receive(:foo).with(d1)
691
- expect do
692
- @double.foo(d2)
693
- end.to raise_error
694
- @double.rspec_reset
695
- end
696
- end
697
-
698
- context "with non-matching doubles as_null_object" do
699
- it "fails" do
700
- d1 = Spy.double('1').as_null_object
701
- d2 = Spy.double('2').as_null_object
702
- @double.should_receive(:foo).with(d1)
703
- expect do
704
- @double.foo(d2)
705
- end.to raise_error
706
- @double.rspec_reset
707
- end
708
- end
709
- end
710
-
711
- context "with a block" do
712
- context "with matching args" do
713
- it "returns the result of the block" do
714
- @double.should_receive(:foo).with('bar') { 'baz' }
715
- expect(@double.foo('bar')).to eq('baz')
716
- end
717
- end
718
-
719
- context "with non-matching args" do
720
- it "fails" do
721
- @double.should_receive(:foo).with('bar') { 'baz' }
722
- expect do
723
- expect(@double.foo('wrong')).to eq('baz')
724
- end.to raise_error(/received :foo with unexpected arguments/)
725
- @double.rspec_reset
726
- end
727
- end
219
+ expect(spy.calls.first.args).to eq([second])
728
220
  end
729
221
  end
730
222
  end