rspec-mocks 3.0.0.beta1 → 3.0.0.beta2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (114) hide show
  1. data.tar.gz.sig +1 -1
  2. data/Changelog.md +95 -3
  3. data/README.md +27 -13
  4. data/features/README.md +15 -7
  5. data/features/argument_matchers/README.md +5 -5
  6. data/features/argument_matchers/explicit.feature +6 -6
  7. data/features/argument_matchers/general_matchers.feature +4 -4
  8. data/features/argument_matchers/type_matchers.feature +2 -2
  9. data/features/message_expectations/README.md +29 -27
  10. data/features/message_expectations/call_original.feature +0 -1
  11. data/features/message_expectations/message_chains_using_expect.feature +49 -0
  12. data/features/method_stubs/README.md +2 -1
  13. data/features/method_stubs/{any_instance.feature → allow_any_instance_of.feature} +12 -12
  14. data/features/method_stubs/{stub_chain.feature → receive_message_chain.feature} +3 -3
  15. data/features/method_stubs/to_ary.feature +1 -1
  16. data/features/mutating_constants/stub_defined_constant.feature +0 -1
  17. data/features/outside_rspec/standalone.feature +1 -1
  18. data/features/spies/spy_pure_mock_method.feature +1 -1
  19. data/features/test_frameworks/test_unit.feature +21 -10
  20. data/features/verifying_doubles/README.md +17 -0
  21. data/features/verifying_doubles/class_doubles.feature +1 -16
  22. data/features/verifying_doubles/dynamic_classes.feature +0 -1
  23. data/features/verifying_doubles/{introduction.feature → instance_doubles.feature} +41 -23
  24. data/features/verifying_doubles/partial_doubles.feature +2 -2
  25. data/lib/rspec/mocks.rb +69 -82
  26. data/lib/rspec/mocks/any_instance/expect_chain_chain.rb +35 -0
  27. data/lib/rspec/mocks/any_instance/expectation_chain.rb +1 -2
  28. data/lib/rspec/mocks/any_instance/recorder.rb +52 -18
  29. data/lib/rspec/mocks/any_instance/stub_chain.rb +1 -1
  30. data/lib/rspec/mocks/any_instance/stub_chain_chain.rb +4 -0
  31. data/lib/rspec/mocks/argument_list_matcher.rb +10 -44
  32. data/lib/rspec/mocks/argument_matchers.rb +132 -163
  33. data/lib/rspec/mocks/configuration.rb +28 -4
  34. data/lib/rspec/mocks/error_generator.rb +46 -13
  35. data/lib/rspec/mocks/example_methods.rb +13 -12
  36. data/lib/rspec/mocks/extensions/marshal.rb +1 -1
  37. data/lib/rspec/mocks/framework.rb +3 -4
  38. data/lib/rspec/mocks/instance_method_stasher.rb +2 -3
  39. data/lib/rspec/mocks/matchers/have_received.rb +8 -6
  40. data/lib/rspec/mocks/matchers/receive.rb +28 -20
  41. data/lib/rspec/mocks/matchers/receive_message_chain.rb +65 -0
  42. data/lib/rspec/mocks/matchers/receive_messages.rb +3 -2
  43. data/lib/rspec/mocks/message_chain.rb +91 -0
  44. data/lib/rspec/mocks/message_expectation.rb +86 -80
  45. data/lib/rspec/mocks/method_double.rb +2 -11
  46. data/lib/rspec/mocks/method_reference.rb +82 -23
  47. data/lib/rspec/mocks/method_signature_verifier.rb +207 -0
  48. data/lib/rspec/mocks/mutate_const.rb +34 -50
  49. data/lib/rspec/mocks/object_reference.rb +0 -1
  50. data/lib/rspec/mocks/proxy.rb +70 -13
  51. data/lib/rspec/mocks/ruby_features.rb +24 -0
  52. data/lib/rspec/mocks/space.rb +105 -31
  53. data/lib/rspec/mocks/standalone.rb +2 -2
  54. data/lib/rspec/mocks/syntax.rb +43 -8
  55. data/lib/rspec/mocks/targets.rb +16 -7
  56. data/lib/rspec/mocks/test_double.rb +41 -15
  57. data/lib/rspec/mocks/verifying_double.rb +51 -4
  58. data/lib/rspec/mocks/verifying_message_expecation.rb +12 -12
  59. data/lib/rspec/mocks/verifying_proxy.rb +32 -19
  60. data/lib/rspec/mocks/version.rb +1 -1
  61. data/spec/rspec/mocks/and_call_original_spec.rb +28 -7
  62. data/spec/rspec/mocks/and_return_spec.rb +23 -0
  63. data/spec/rspec/mocks/and_yield_spec.rb +1 -2
  64. data/spec/rspec/mocks/any_instance_spec.rb +33 -17
  65. data/spec/rspec/mocks/array_including_matcher_spec.rb +6 -6
  66. data/spec/rspec/mocks/before_all_spec.rb +132 -0
  67. data/spec/rspec/mocks/block_return_value_spec.rb +12 -1
  68. data/spec/rspec/mocks/combining_implementation_instructions_spec.rb +9 -11
  69. data/spec/rspec/mocks/configuration_spec.rb +14 -1
  70. data/spec/rspec/mocks/double_spec.rb +867 -24
  71. data/spec/rspec/mocks/example_methods_spec.rb +13 -0
  72. data/spec/rspec/mocks/extensions/marshal_spec.rb +17 -17
  73. data/spec/rspec/mocks/failing_argument_matchers_spec.rb +29 -1
  74. data/spec/rspec/mocks/hash_excluding_matcher_spec.rb +12 -12
  75. data/spec/rspec/mocks/hash_including_matcher_spec.rb +21 -17
  76. data/spec/rspec/mocks/instance_method_stasher_spec.rb +2 -3
  77. data/spec/rspec/mocks/matchers/have_received_spec.rb +7 -0
  78. data/spec/rspec/mocks/matchers/receive_message_chain_spec.rb +198 -0
  79. data/spec/rspec/mocks/matchers/receive_messages_spec.rb +2 -2
  80. data/spec/rspec/mocks/matchers/receive_spec.rb +19 -6
  81. data/spec/rspec/mocks/method_signature_verifier_spec.rb +272 -0
  82. data/spec/rspec/mocks/methods_spec.rb +0 -1
  83. data/spec/rspec/mocks/multiple_return_value_spec.rb +2 -2
  84. data/spec/rspec/mocks/mutate_const_spec.rb +24 -1
  85. data/spec/rspec/mocks/nil_expectation_warning_spec.rb +6 -22
  86. data/spec/rspec/mocks/null_object_mock_spec.rb +13 -7
  87. data/spec/rspec/mocks/options_hash_spec.rb +3 -3
  88. data/spec/rspec/mocks/{partial_mock_spec.rb → partial_double_spec.rb} +5 -2
  89. data/spec/rspec/mocks/{partial_mock_using_mocks_directly_spec.rb → partial_double_using_mocks_directly_spec.rb} +1 -1
  90. data/spec/rspec/mocks/passing_argument_matchers_spec.rb +18 -0
  91. data/spec/rspec/mocks/serialization_spec.rb +1 -0
  92. data/spec/rspec/mocks/space_spec.rb +218 -7
  93. data/spec/rspec/mocks/stub_chain_spec.rb +6 -0
  94. data/spec/rspec/mocks/stub_spec.rb +0 -6
  95. data/spec/rspec/mocks/syntax_spec.rb +19 -0
  96. data/spec/rspec/mocks/test_double_spec.rb +0 -1
  97. data/spec/rspec/mocks/verifying_double_spec.rb +281 -18
  98. data/spec/rspec/mocks/verifying_message_expecation_spec.rb +7 -6
  99. data/spec/rspec/mocks_spec.rb +168 -42
  100. data/spec/spec_helper.rb +34 -22
  101. metadata +94 -63
  102. metadata.gz.sig +0 -0
  103. checksums.yaml +0 -15
  104. checksums.yaml.gz.sig +0 -2
  105. data/features/outside_rspec/configuration.feature +0 -60
  106. data/lib/rspec/mocks/arity_calculator.rb +0 -66
  107. data/lib/rspec/mocks/errors.rb +0 -12
  108. data/lib/rspec/mocks/mock.rb +0 -7
  109. data/lib/rspec/mocks/proxy_for_nil.rb +0 -37
  110. data/lib/rspec/mocks/stub_chain.rb +0 -51
  111. data/spec/rspec/mocks/argument_expectation_spec.rb +0 -32
  112. data/spec/rspec/mocks/arity_calculator_spec.rb +0 -95
  113. data/spec/rspec/mocks/mock_space_spec.rb +0 -113
  114. data/spec/rspec/mocks/mock_spec.rb +0 -788
@@ -1,788 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module RSpec
4
- module Mocks
5
- describe Mock do
6
- before(:each) { @double = double("test double") }
7
- after(:each) { reset @double }
8
-
9
- it "has method_missing as private" do
10
- expect(RSpec::Mocks::Mock.private_instance_methods).to include_method(:method_missing)
11
- end
12
-
13
- it "does not respond_to? method_missing (because it's private)" do
14
- expect(RSpec::Mocks::Mock.new).not_to respond_to(:method_missing)
15
- end
16
-
17
- it "reports line number of expectation of unreceived message" do
18
- expected_error_line = __LINE__; @double.should_receive(:wont_happen).with("x", 3)
19
- begin
20
- verify @double
21
- violated
22
- rescue RSpec::Mocks::MockExpectationError => e
23
- # NOTE - this regexp ended w/ $, but jruby adds extra info at the end of the line
24
- expect(e.backtrace[0]).to match(/#{File.basename(__FILE__)}:#{expected_error_line}/)
25
- end
26
- end
27
-
28
- it "reports line number of expectation of unreceived message after #should_receive after similar stub" do
29
- @double.stub(:wont_happen)
30
- expected_error_line = __LINE__; @double.should_receive(:wont_happen).with("x", 3)
31
- begin
32
- verify @double
33
- violated
34
- rescue RSpec::Mocks::MockExpectationError => e
35
- # NOTE - this regexp ended w/ $, but jruby adds extra info at the end of the line
36
- expect(e.backtrace[0]).to match(/#{File.basename(__FILE__)}:#{expected_error_line}/)
37
- end
38
- end
39
-
40
- it "passes when not receiving message specified as not to be received" do
41
- @double.should_not_receive(:not_expected)
42
- verify @double
43
- end
44
-
45
- it "prevents confusing double-negative expressions involving `never`" do
46
- expect {
47
- @double.should_not_receive(:not_expected).never
48
- }.to raise_error(/trying to negate it again/)
49
- end
50
-
51
- it "warns when `and_return` is called on a negative expectation" do
52
- expect {
53
- @double.should_not_receive(:do_something).and_return(1)
54
- }.to raise_error(/not supported/)
55
-
56
- expect {
57
- expect(@double).not_to receive(:do_something).and_return(1)
58
- }.to raise_error(/not supported/)
59
-
60
- expect {
61
- expect(@double).to receive(:do_something).never.and_return(1)
62
- }.to raise_error(/not supported/)
63
- end
64
-
65
- it "passes when receiving message specified as not to be received with different args" do
66
- @double.should_not_receive(:message).with("unwanted text")
67
- @double.should_receive(:message).with("other text")
68
- @double.message "other text"
69
- verify @double
70
- end
71
-
72
- it "fails when receiving message specified as not to be received" do
73
- @double.should_not_receive(:not_expected)
74
- expect {
75
- @double.not_expected
76
- violated
77
- }.to raise_error(
78
- RSpec::Mocks::MockExpectationError,
79
- %Q|(Double "test double").not_expected(no args)\n expected: 0 times with any arguments\n received: 1 time|
80
- )
81
- end
82
-
83
- it "fails when receiving message specified as not to be received with args" do
84
- @double.should_not_receive(:not_expected).with("unexpected text")
85
- expect {
86
- @double.not_expected("unexpected text")
87
- violated
88
- }.to raise_error(
89
- RSpec::Mocks::MockExpectationError,
90
- %Q|(Double "test double").not_expected("unexpected text")\n expected: 0 times with arguments: ("unexpected text")\n received: 1 time with arguments: ("unexpected text")|
91
- )
92
- end
93
-
94
- it "fails when array arguments do not match" do
95
- @double.should_not_receive(:not_expected).with(["do not want"])
96
- expect {
97
- @double.not_expected(["do not want"])
98
- violated
99
- }.to raise_error(
100
- RSpec::Mocks::MockExpectationError,
101
- %Q|(Double "test double").not_expected(["do not want"])\n expected: 0 times with arguments: (["do not want"])\n received: 1 time with arguments: (["do not want"])|
102
- )
103
- end
104
-
105
- it "passes when receiving message specified as not to be received with wrong args" do
106
- @double.should_not_receive(:not_expected).with("unexpected text")
107
- @double.not_expected "really unexpected text"
108
- verify @double
109
-
110
- @double.should_receive(:not_expected).with("unexpected text").never
111
- @double.not_expected "really unexpected text"
112
- verify @double
113
- end
114
-
115
- it 'does not get confused when `should_not_received` is used with a string and symbol message' do
116
- @double.stub(:foo) { 3 }
117
- @double.should_not_receive(:foo).with(1)
118
- @double.should_not_receive("foo").with(2)
119
-
120
- expect(@double.foo).to eq(3)
121
- verify @double
122
- end
123
-
124
- it 'does not get confused when `should_received` is used with a string and symbol message' do
125
- @double.should_receive(:foo).with(1)
126
- @double.should_receive("foo").with(2)
127
-
128
- @double.foo(1)
129
- @double.foo(2)
130
-
131
- verify @double
132
- end
133
-
134
- it "allows block to calculate return values" do
135
- @double.should_receive(:something).with("a","b","c").and_return { |a,b,c| c+b+a }
136
- expect(@double.something("a","b","c")).to eq "cba"
137
- verify @double
138
- end
139
-
140
- it "allows parameter as return value" do
141
- @double.should_receive(:something).with("a","b","c").and_return("booh")
142
- expect(@double.something("a","b","c")).to eq "booh"
143
- verify @double
144
- end
145
-
146
- it "returns the previously stubbed value if no return value is set" do
147
- @double.stub(:something).with("a","b","c").and_return(:stubbed_value)
148
- @double.should_receive(:something).with("a","b","c")
149
- expect(@double.something("a","b","c")).to eq :stubbed_value
150
- verify @double
151
- end
152
-
153
- it "returns nil if no return value is set and there is no previously stubbed value" do
154
- @double.should_receive(:something).with("a","b","c")
155
- expect(@double.something("a","b","c")).to be_nil
156
- verify @double
157
- end
158
-
159
- it "raises exception if args don't match when method called" do
160
- @double.should_receive(:something).with("a","b","c").and_return("booh")
161
- expect {
162
- @double.something("a","d","c")
163
- violated
164
- }.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :something with unexpected arguments\n expected: (\"a\", \"b\", \"c\")\n got: (\"a\", \"d\", \"c\")")
165
- end
166
-
167
- describe "even when a similar expectation with different arguments exist" do
168
- it "raises exception if args don't match when method called, correctly reporting the offending arguments" do
169
- @double.should_receive(:something).with("a","b","c").once
170
- @double.should_receive(:something).with("z","x","c").once
171
- expect {
172
- @double.something("a","b","c")
173
- @double.something("z","x","g")
174
- }.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :something with unexpected arguments\n expected: (\"z\", \"x\", \"c\")\n got: (\"z\", \"x\", \"g\")")
175
- end
176
- end
177
-
178
- it "raises exception if args don't match when method called even when the method is stubbed" do
179
- @double.stub(:something)
180
- @double.should_receive(:something).with("a","b","c")
181
- expect {
182
- @double.something("a","d","c")
183
- verify @double
184
- }.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :something with unexpected arguments\n expected: (\"a\", \"b\", \"c\")\n got: (\"a\", \"d\", \"c\")")
185
- end
186
-
187
- it "raises exception if args don't match when method called even when using null_object" do
188
- @double = double("test double").as_null_object
189
- @double.should_receive(:something).with("a","b","c")
190
- expect {
191
- @double.something("a","d","c")
192
- verify @double
193
- }.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :something with unexpected arguments\n expected: (\"a\", \"b\", \"c\")\n got: (\"a\", \"d\", \"c\")")
194
- end
195
-
196
- describe 'with a method that has a default argument' do
197
- it "raises an exception if the arguments don't match when the method is called, correctly reporting the offending arguments" do
198
- def @double.method_with_default_argument(arg={}); end
199
- @double.should_receive(:method_with_default_argument).with({})
200
-
201
- expect {
202
- @double.method_with_default_argument(nil)
203
- verify @double
204
- }.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :method_with_default_argument with unexpected arguments\n expected: ({})\n got: (nil)")
205
- end
206
- end
207
-
208
- it "fails if unexpected method called" do
209
- expect {
210
- @double.something("a","b","c")
211
- violated
212
- }.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received unexpected message :something with (\"a\", \"b\", \"c\")")
213
- end
214
-
215
- it "uses block for expectation if provided" do
216
- @double.should_receive(:something) do | a, b |
217
- expect(a).to eq "a"
218
- expect(b).to eq "b"
219
- "booh"
220
- end
221
- expect(@double.something("a", "b")).to eq "booh"
222
- verify @double
223
- end
224
-
225
- it "fails if expectation block fails" do
226
- @double.should_receive(:something) do |bool|
227
- expect(bool).to be_truthy
228
- end
229
-
230
- expect {
231
- @double.something false
232
- }.to raise_error(RSpec::Expectations::ExpectationNotMetError)
233
- end
234
-
235
- it "is wrappable in an array" do
236
- expect( Array(@double) ).to eq([@double])
237
- end
238
-
239
- it "is wrappable in an array when a null object" do
240
- expect( Array(@double.as_null_object) ).to eq [@double]
241
- end
242
-
243
- it "responds to to_ary as a null object" do
244
- expect(@double.as_null_object.to_ary).to eq nil
245
- end
246
-
247
- it "responds to to_a as a null object" do
248
- if RUBY_VERSION.to_f > 1.8
249
- expect(@double.as_null_object.to_a).to eq nil
250
- else
251
- expect(@double.as_null_object.to_a).to eq [@double]
252
- end
253
- end
254
-
255
- it "passes proc to expectation block without an argument" do
256
- @double.should_receive(:foo) { |&block| expect(block.call).to eq(:bar) }
257
- @double.foo { :bar }
258
- end
259
-
260
- it "passes proc to expectation block with an argument" do
261
- @double.should_receive(:foo) { |arg, &block| expect(block.call).to eq(:bar) }
262
- @double.foo(:arg) { :bar }
263
- end
264
-
265
- it "passes proc to stub block without an argurment" do
266
- @double.stub(:foo) { |&block| expect(block.call).to eq(:bar) }
267
- @double.foo { :bar }
268
- end
269
-
270
- it "passes proc to stub block with an argument" do
271
- @double.stub(:foo) { |arg, &block| expect(block.call).to eq(:bar) }
272
- @double.foo(:arg) { :bar }
273
- end
274
-
275
- it "fails right away when method defined as never is received" do
276
- @double.should_receive(:not_expected).never
277
- expect { @double.not_expected }.
278
- to raise_error(RSpec::Mocks::MockExpectationError,
279
- %Q|(Double "test double").not_expected(no args)\n expected: 0 times with any arguments\n received: 1 time|
280
- )
281
- end
282
-
283
- it "raises RuntimeError by default" do
284
- @double.should_receive(:something).and_raise
285
- expect { @double.something }.to raise_error(RuntimeError)
286
- end
287
-
288
- it "raises RuntimeError with a message by default" do
289
- @double.should_receive(:something).and_raise("error message")
290
- expect { @double.something }.to raise_error(RuntimeError, "error message")
291
- end
292
-
293
- it "raises an exception of a given type without an error message" do
294
- @double.should_receive(:something).and_raise(StandardError)
295
- expect { @double.something }.to raise_error(StandardError)
296
- end
297
-
298
- it "raises an exception of a given type with a message" do
299
- @double.should_receive(:something).and_raise(RuntimeError, "error message")
300
- expect { @double.something }.to raise_error(RuntimeError, "error message")
301
- end
302
-
303
- it "raises a given instance of an exception" do
304
- @double.should_receive(:something).and_raise(RuntimeError.new("error message"))
305
- expect { @double.something }.to raise_error(RuntimeError, "error message")
306
- end
307
-
308
- class OutOfGas < StandardError
309
- attr_reader :amount, :units
310
- def initialize(amount, units)
311
- @amount = amount
312
- @units = units
313
- end
314
- end
315
-
316
- it "raises a given instance of an exception with arguments other than the standard 'message'" do
317
- @double.should_receive(:something).and_raise(OutOfGas.new(2, :oz))
318
-
319
- begin
320
- @double.something
321
- fail "OutOfGas was not raised"
322
- rescue OutOfGas => e
323
- expect(e.amount).to eq 2
324
- expect(e.units).to eq :oz
325
- end
326
- end
327
-
328
- it "does not raise when told to if args dont match" do
329
- @double.should_receive(:something).with(2).and_raise(RuntimeError)
330
- expect {
331
- @double.something 1
332
- }.to raise_error(RSpec::Mocks::MockExpectationError)
333
- end
334
-
335
- it "throws when told to" do
336
- @double.should_receive(:something).and_throw(:blech)
337
- expect {
338
- @double.something
339
- }.to throw_symbol(:blech)
340
- end
341
-
342
- it "ignores args on any args" do
343
- @double.should_receive(:something).at_least(:once).with(any_args)
344
- @double.something
345
- @double.something 1
346
- @double.something "a", 2
347
- @double.something [], {}, "joe", 7
348
- verify @double
349
- end
350
-
351
- it "fails on no args if any args received" do
352
- @double.should_receive(:something).with(no_args())
353
- expect {
354
- @double.something 1
355
- }.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :something with unexpected arguments\n expected: (no args)\n got: (1)")
356
- end
357
-
358
- it "fails when args are expected but none are received" do
359
- @double.should_receive(:something).with(1)
360
- expect {
361
- @double.something
362
- }.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :something with unexpected arguments\n expected: (1)\n got: (no args)")
363
- end
364
-
365
- it "returns value from block by default" do
366
- @double.stub(:method_that_yields).and_yield
367
- value = @double.method_that_yields { :returned_obj }
368
- expect(value).to eq :returned_obj
369
- verify @double
370
- end
371
-
372
- it "yields 0 args to blocks that take a variable number of arguments" do
373
- @double.should_receive(:yield_back).with(no_args()).once.and_yield
374
- a = nil
375
- @double.yield_back {|*x| a = x}
376
- expect(a).to eq []
377
- verify @double
378
- end
379
-
380
- it "yields 0 args multiple times to blocks that take a variable number of arguments" do
381
- @double.should_receive(:yield_back).once.with(no_args()).once.and_yield.
382
- and_yield
383
- b = []
384
- @double.yield_back {|*a| b << a}
385
- expect(b).to eq [ [], [] ]
386
- verify @double
387
- end
388
-
389
- it "yields one arg to blocks that take a variable number of arguments" do
390
- @double.should_receive(:yield_back).with(no_args()).once.and_yield(99)
391
- a = nil
392
- @double.yield_back {|*x| a = x}
393
- expect(a).to eq [99]
394
- verify @double
395
- end
396
-
397
- it "yields one arg 3 times consecutively to blocks that take a variable number of arguments" do
398
- @double.should_receive(:yield_back).once.with(no_args()).once.and_yield(99).
399
- and_yield(43).
400
- and_yield("something fruity")
401
- b = []
402
- @double.yield_back {|*a| b << a}
403
- expect(b).to eq [[99], [43], ["something fruity"]]
404
- verify @double
405
- end
406
-
407
- it "yields many args to blocks that take a variable number of arguments" do
408
- @double.should_receive(:yield_back).with(no_args()).once.and_yield(99, 27, "go")
409
- a = nil
410
- @double.yield_back {|*x| a = x}
411
- expect(a).to eq [99, 27, "go"]
412
- verify @double
413
- end
414
-
415
- it "yields many args 3 times consecutively to blocks that take a variable number of arguments" do
416
- @double.should_receive(:yield_back).once.with(no_args()).once.and_yield(99, :green, "go").
417
- and_yield("wait", :amber).
418
- and_yield("stop", 12, :red)
419
- b = []
420
- @double.yield_back {|*a| b << a}
421
- expect(b).to eq [[99, :green, "go"], ["wait", :amber], ["stop", 12, :red]]
422
- verify @double
423
- end
424
-
425
- it "yields single value" do
426
- @double.should_receive(:yield_back).with(no_args()).once.and_yield(99)
427
- a = nil
428
- @double.yield_back {|x| a = x}
429
- expect(a).to eq 99
430
- verify @double
431
- end
432
-
433
- it "yields single value 3 times consecutively" do
434
- @double.should_receive(:yield_back).once.with(no_args()).once.and_yield(99).
435
- and_yield(43).
436
- and_yield("something fruity")
437
- b = []
438
- @double.yield_back {|a| b << a}
439
- expect(b).to eq [99, 43, "something fruity"]
440
- verify @double
441
- end
442
-
443
- it "yields two values" do
444
- @double.should_receive(:yield_back).with(no_args()).once.and_yield('wha', 'zup')
445
- a, b = nil
446
- @double.yield_back {|x,y| a=x; b=y}
447
- expect(a).to eq 'wha'
448
- expect(b).to eq 'zup'
449
- verify @double
450
- end
451
-
452
- it "yields two values 3 times consecutively" do
453
- @double.should_receive(:yield_back).once.with(no_args()).once.and_yield('wha', 'zup').
454
- and_yield('not', 'down').
455
- and_yield(14, 65)
456
- c = []
457
- @double.yield_back {|a,b| c << [a, b]}
458
- expect(c).to eq [['wha', 'zup'], ['not', 'down'], [14, 65]]
459
- verify @double
460
- end
461
-
462
- it "fails when calling yielding method with wrong arity" do
463
- @double.should_receive(:yield_back).with(no_args()).once.and_yield('wha', 'zup')
464
- expect {
465
- @double.yield_back {|a|}
466
- }.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" yielded |\"wha\", \"zup\"| to block with arity of 1")
467
- end
468
-
469
- it "fails when calling yielding method consecutively with wrong arity" do
470
- @double.should_receive(:yield_back).once.with(no_args()).once.and_yield('wha', 'zup').
471
- and_yield('down').
472
- and_yield(14, 65)
473
- expect {
474
- c = []
475
- @double.yield_back {|a,b| c << [a, b]}
476
- }.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" yielded |\"down\"| to block with arity of 2")
477
- end
478
-
479
- it "fails when calling yielding method without block" do
480
- @double.should_receive(:yield_back).with(no_args()).once.and_yield('wha', 'zup')
481
- expect {
482
- @double.yield_back
483
- }.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" asked to yield |[\"wha\", \"zup\"]| but no block was passed")
484
- end
485
-
486
- it "is able to double send" do
487
- @double.should_receive(:send).with(any_args)
488
- @double.send 'hi'
489
- verify @double
490
- end
491
-
492
- it "is able to raise from method calling yielding double" do
493
- @double.should_receive(:yield_me).and_yield 44
494
-
495
- expect {
496
- @double.yield_me do |x|
497
- raise "Bang"
498
- end
499
- }.to raise_error(StandardError, "Bang")
500
-
501
- verify @double
502
- end
503
-
504
- it "clears expectations after verify" do
505
- @double.should_receive(:foobar)
506
- @double.foobar
507
- verify @double
508
- expect {
509
- @double.foobar
510
- }.to raise_error(RSpec::Mocks::MockExpectationError, %q|Double "test double" received unexpected message :foobar with (no args)|)
511
- end
512
-
513
- it "restores objects to their original state on rspec_reset" do
514
- double = double("this is a double")
515
- double.should_receive(:blah)
516
- reset double
517
- verify double #should throw if reset didn't work
518
- end
519
-
520
- it "works even after method_missing starts raising NameErrors instead of NoMethodErrors" do
521
- # Object#method_missing throws either NameErrors or NoMethodErrors.
522
- #
523
- # On a fresh ruby program Object#method_missing:
524
- # * raises a NoMethodError when called directly
525
- # * raises a NameError when called indirectly
526
- #
527
- # Once Object#method_missing has been called at least once (on any object)
528
- # it starts behaving differently:
529
- # * raises a NameError when called directly
530
- # * raises a NameError when called indirectly
531
- #
532
- # There was a bug in Mock#method_missing that relied on the fact
533
- # that calling Object#method_missing directly raises a NoMethodError.
534
- # This example tests that the bug doesn't exist anymore.
535
-
536
-
537
- # Ensures that method_missing always raises NameErrors.
538
- a_method_that_doesnt_exist rescue
539
-
540
-
541
- @double.should_receive(:foobar)
542
- @double.foobar
543
- verify @double
544
-
545
- expect { @double.foobar }.to raise_error(RSpec::Mocks::MockExpectationError)
546
- end
547
-
548
- it "temporarily replaces a method stub on a double" do
549
- @double.stub(:msg).and_return(:stub_value)
550
- @double.should_receive(:msg).with(:arg).and_return(:double_value)
551
- expect(@double.msg(:arg)).to equal(:double_value)
552
- expect(@double.msg).to equal(:stub_value)
553
- expect(@double.msg).to equal(:stub_value)
554
- verify @double
555
- end
556
-
557
- it "does not require a different signature to replace a method stub" do
558
- @double.stub(:msg).and_return(:stub_value)
559
- @double.should_receive(:msg).and_return(:double_value)
560
- expect(@double.msg(:arg)).to equal(:double_value)
561
- expect(@double.msg).to equal(:stub_value)
562
- expect(@double.msg).to equal(:stub_value)
563
- verify @double
564
- end
565
-
566
- it "raises an error when a previously stubbed method has a negative expectation" do
567
- @double.stub(:msg).and_return(:stub_value)
568
- @double.should_not_receive(:msg)
569
- expect { @double.msg(:arg) }.to raise_error(RSpec::Mocks::MockExpectationError)
570
- end
571
-
572
- it "temporarily replaces a method stub on a non-double" do
573
- non_double = Object.new
574
- non_double.stub(:msg).and_return(:stub_value)
575
- non_double.should_receive(:msg).with(:arg).and_return(:double_value)
576
- expect(non_double.msg(:arg)).to equal(:double_value)
577
- expect(non_double.msg).to equal(:stub_value)
578
- expect(non_double.msg).to equal(:stub_value)
579
- verify non_double
580
- end
581
-
582
- it "returns the stubbed value when no new value specified" do
583
- @double.stub(:msg).and_return(:stub_value)
584
- @double.should_receive(:msg)
585
- expect(@double.msg).to equal(:stub_value)
586
- verify @double
587
- end
588
-
589
- it "returns the stubbed value when stubbed with args and no new value specified" do
590
- @double.stub(:msg).with(:arg).and_return(:stub_value)
591
- @double.should_receive(:msg).with(:arg)
592
- expect(@double.msg(:arg)).to equal(:stub_value)
593
- verify @double
594
- end
595
-
596
- it "does not mess with the stub's yielded values when also doubleed" do
597
- @double.stub(:yield_back).and_yield(:stub_value)
598
- @double.should_receive(:yield_back).and_yield(:double_value)
599
- @double.yield_back{|v| expect(v).to eq :double_value }
600
- @double.yield_back{|v| expect(v).to eq :stub_value }
601
- verify @double
602
- end
603
-
604
- it "can yield multiple times when told to do so" do
605
- @double.stub(:foo).and_yield(:stub_value)
606
- @double.should_receive(:foo).and_yield(:first_yield).and_yield(:second_yield)
607
-
608
- expect { |b| @double.foo(&b) }.to yield_successive_args(:first_yield, :second_yield)
609
- expect { |b| @double.foo(&b) }.to yield_with_args(:stub_value)
610
-
611
- verify @double
612
- end
613
-
614
- it "assigns stub return values" do
615
- double = RSpec::Mocks::Mock.new('name', :message => :response)
616
- expect(double.message).to eq :response
617
- end
618
-
619
- end
620
-
621
- describe "a double message receiving a block" do
622
- before(:each) do
623
- @double = double("double")
624
- @calls = 0
625
- end
626
-
627
- def add_call
628
- @calls = @calls + 1
629
- end
630
-
631
- it "calls the block after #should_receive" do
632
- @double.should_receive(:foo) { add_call }
633
-
634
- @double.foo
635
-
636
- expect(@calls).to eq 1
637
- end
638
-
639
- it "calls the block after #should_receive after a similar stub" do
640
- @double.stub(:foo).and_return(:bar)
641
- @double.should_receive(:foo) { add_call }
642
-
643
- @double.foo
644
-
645
- expect(@calls).to eq 1
646
- end
647
-
648
- it "calls the block after #once" do
649
- @double.should_receive(:foo).once { add_call }
650
-
651
- @double.foo
652
-
653
- expect(@calls).to eq 1
654
- end
655
-
656
- it "calls the block after #twice" do
657
- @double.should_receive(:foo).twice { add_call }
658
-
659
- @double.foo
660
- @double.foo
661
-
662
- expect(@calls).to eq 2
663
- end
664
-
665
- it "calls the block after #times" do
666
- @double.should_receive(:foo).exactly(10).times { add_call }
667
-
668
- (1..10).each { @double.foo }
669
-
670
- expect(@calls).to eq 10
671
- end
672
-
673
- it "calls the block after #ordered" do
674
- @double.should_receive(:foo).ordered { add_call }
675
- @double.should_receive(:bar).ordered { add_call }
676
-
677
- @double.foo
678
- @double.bar
679
-
680
- expect(@calls).to eq 2
681
- end
682
- end
683
-
684
- describe 'string representation generated by #to_s' do
685
- it 'does not contain < because that might lead to invalid HTML in some situations' do
686
- double = double("Dog")
687
- valid_html_str = "#{double}"
688
- expect(valid_html_str).not_to include('<')
689
- end
690
- end
691
-
692
- describe "string representation generated by #to_str" do
693
- it "looks the same as #to_s" do
694
- double = double("Foo")
695
- expect(double.to_str).to eq double.to_s
696
- end
697
- end
698
-
699
- describe "double created with no name" do
700
- it "does not use a name in a failure message" do
701
- double = double()
702
- expect {double.foo}.to raise_error(/Double received/)
703
- end
704
-
705
- it "does respond to initially stubbed methods" do
706
- double = double(:foo => "woo", :bar => "car")
707
- expect(double.foo).to eq "woo"
708
- expect(double.bar).to eq "car"
709
- end
710
- end
711
-
712
- describe "==" do
713
- it "sends '== self' to the comparison object" do
714
- first = double('first')
715
- second = double('second')
716
-
717
- first.should_receive(:==).with(second)
718
- second == first
719
- end
720
- end
721
-
722
- describe "with" do
723
- before { @double = double('double') }
724
- context "with args" do
725
- context "with matching args" do
726
- it "passes" do
727
- @double.should_receive(:foo).with('bar')
728
- @double.foo('bar')
729
- end
730
- end
731
-
732
- context "with non-matching args" do
733
- it "fails" do
734
- @double.should_receive(:foo).with('bar')
735
- expect do
736
- @double.foo('baz')
737
- end.to raise_error
738
- reset @double
739
- end
740
- end
741
-
742
- context "with non-matching doubles" do
743
- it "fails" do
744
- d1 = double('1')
745
- d2 = double('2')
746
- @double.should_receive(:foo).with(d1)
747
- expect do
748
- @double.foo(d2)
749
- end.to raise_error
750
- reset @double
751
- end
752
- end
753
-
754
- context "with non-matching doubles as_null_object" do
755
- it "fails" do
756
- d1 = double('1').as_null_object
757
- d2 = double('2').as_null_object
758
- @double.should_receive(:foo).with(d1)
759
- expect do
760
- @double.foo(d2)
761
- end.to raise_error
762
- reset @double
763
- end
764
- end
765
- end
766
-
767
- context "with a block" do
768
- context "with matching args" do
769
- it "returns the result of the block" do
770
- @double.should_receive(:foo).with('bar') { 'baz' }
771
- expect(@double.foo('bar')).to eq('baz')
772
- end
773
- end
774
-
775
- context "with non-matching args" do
776
- it "fails" do
777
- @double.should_receive(:foo).with('bar') { 'baz' }
778
- expect do
779
- expect(@double.foo('wrong')).to eq('baz')
780
- end.to raise_error(/received :foo with unexpected arguments/)
781
- reset @double
782
- end
783
- end
784
- end
785
- end
786
- end
787
- end
788
-