rspec-mocks 3.0.0.beta2 → 3.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (145) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/Changelog.md +104 -36
  5. data/README.md +35 -4
  6. data/lib/rspec/mocks.rb +52 -13
  7. data/lib/rspec/mocks/any_instance.rb +10 -0
  8. data/lib/rspec/mocks/any_instance/chain.rb +17 -0
  9. data/lib/rspec/mocks/any_instance/expect_chain_chain.rb +1 -1
  10. data/lib/rspec/mocks/any_instance/expectation_chain.rb +9 -6
  11. data/lib/rspec/mocks/any_instance/message_chains.rb +10 -1
  12. data/lib/rspec/mocks/any_instance/proxy.rb +112 -0
  13. data/lib/rspec/mocks/any_instance/recorder.rb +49 -18
  14. data/lib/rspec/mocks/any_instance/stub_chain.rb +5 -3
  15. data/lib/rspec/mocks/any_instance/stub_chain_chain.rb +1 -1
  16. data/lib/rspec/mocks/argument_list_matcher.rb +5 -3
  17. data/lib/rspec/mocks/argument_matchers.rb +28 -12
  18. data/lib/rspec/mocks/configuration.rb +29 -0
  19. data/lib/rspec/mocks/error_generator.rb +2 -5
  20. data/lib/rspec/mocks/example_methods.rb +122 -13
  21. data/lib/rspec/mocks/instance_method_stasher.rb +26 -2
  22. data/lib/rspec/mocks/marshal_extension.rb +41 -0
  23. data/lib/rspec/mocks/matchers/expectation_customization.rb +20 -0
  24. data/lib/rspec/mocks/matchers/receive.rb +6 -20
  25. data/lib/rspec/mocks/matchers/receive_message_chain.rb +7 -5
  26. data/lib/rspec/mocks/matchers/receive_messages.rb +1 -2
  27. data/lib/rspec/mocks/message_chain.rb +9 -9
  28. data/lib/rspec/mocks/message_expectation.rb +6 -5
  29. data/lib/rspec/mocks/method_double.rb +67 -10
  30. data/lib/rspec/mocks/method_reference.rb +1 -1
  31. data/lib/rspec/mocks/mutate_const.rb +10 -14
  32. data/lib/rspec/mocks/object_reference.rb +7 -4
  33. data/lib/rspec/mocks/proxy.rb +95 -8
  34. data/lib/rspec/mocks/space.rb +58 -9
  35. data/lib/rspec/mocks/syntax.rb +128 -210
  36. data/lib/rspec/mocks/targets.rb +6 -8
  37. data/lib/rspec/mocks/test_double.rb +16 -25
  38. data/lib/rspec/mocks/verifying_double.rb +15 -12
  39. data/lib/rspec/mocks/verifying_message_expecation.rb +3 -3
  40. data/lib/rspec/mocks/verifying_proxy.rb +23 -17
  41. data/lib/rspec/mocks/version.rb +3 -1
  42. metadata +29 -257
  43. metadata.gz.sig +0 -0
  44. data/features/README.md +0 -75
  45. data/features/Scope.md +0 -17
  46. data/features/Upgrade.md +0 -22
  47. data/features/argument_matchers/README.md +0 -27
  48. data/features/argument_matchers/explicit.feature +0 -59
  49. data/features/argument_matchers/general_matchers.feature +0 -85
  50. data/features/argument_matchers/type_matchers.feature +0 -26
  51. data/features/message_expectations/README.md +0 -75
  52. data/features/message_expectations/allow_any_instance_of.feature +0 -26
  53. data/features/message_expectations/any_instance.feature +0 -43
  54. data/features/message_expectations/block_local_expectations.feature.pending +0 -55
  55. data/features/message_expectations/call_original.feature +0 -23
  56. data/features/message_expectations/expect_message_using_expect.feature +0 -107
  57. data/features/message_expectations/expect_message_using_should_receive.feature +0 -118
  58. data/features/message_expectations/message_chains_using_expect.feature +0 -49
  59. data/features/message_expectations/receive_counts.feature +0 -209
  60. data/features/message_expectations/warn_when_expectation_is_set_on_nil.feature +0 -50
  61. data/features/method_stubs/README.md +0 -77
  62. data/features/method_stubs/allow_any_instance_of.feature +0 -136
  63. data/features/method_stubs/as_null_object.feature +0 -40
  64. data/features/method_stubs/receive_message_chain.feature +0 -51
  65. data/features/method_stubs/simple_return_value_with_allow.feature +0 -44
  66. data/features/method_stubs/simple_return_value_with_stub.feature +0 -64
  67. data/features/method_stubs/stub_implementation.feature +0 -48
  68. data/features/method_stubs/to_ary.feature +0 -51
  69. data/features/mutating_constants/README.md +0 -82
  70. data/features/mutating_constants/hiding_defined_constant.feature +0 -64
  71. data/features/mutating_constants/stub_defined_constant.feature +0 -78
  72. data/features/mutating_constants/stub_undefined_constant.feature +0 -50
  73. data/features/outside_rspec/standalone.feature +0 -33
  74. data/features/spies/spy_partial_mock_method.feature +0 -34
  75. data/features/spies/spy_pure_mock_method.feature +0 -76
  76. data/features/spies/spy_unstubbed_method.feature +0 -18
  77. data/features/step_definitions/additional_cli_steps.rb +0 -11
  78. data/features/support/env.rb +0 -22
  79. data/features/support/rubinius.rb +0 -6
  80. data/features/test_frameworks/test_unit.feature +0 -54
  81. data/features/verifying_doubles/README.md +0 -17
  82. data/features/verifying_doubles/class_doubles.feature +0 -73
  83. data/features/verifying_doubles/dynamic_classes.feature +0 -71
  84. data/features/verifying_doubles/instance_doubles.feature +0 -103
  85. data/features/verifying_doubles/object_doubles.feature +0 -65
  86. data/features/verifying_doubles/partial_doubles.feature +0 -34
  87. data/lib/rspec/mocks/extensions/marshal.rb +0 -17
  88. data/lib/rspec/mocks/framework.rb +0 -35
  89. data/lib/rspec/mocks/method_signature_verifier.rb +0 -207
  90. data/lib/rspec/mocks/ruby_features.rb +0 -24
  91. data/spec/rspec/mocks/and_call_original_spec.rb +0 -268
  92. data/spec/rspec/mocks/and_return_spec.rb +0 -23
  93. data/spec/rspec/mocks/and_yield_spec.rb +0 -126
  94. data/spec/rspec/mocks/any_instance/message_chains_spec.rb +0 -41
  95. data/spec/rspec/mocks/any_instance_spec.rb +0 -1045
  96. data/spec/rspec/mocks/array_including_matcher_spec.rb +0 -41
  97. data/spec/rspec/mocks/at_least_spec.rb +0 -123
  98. data/spec/rspec/mocks/at_most_spec.rb +0 -90
  99. data/spec/rspec/mocks/before_all_spec.rb +0 -132
  100. data/spec/rspec/mocks/block_return_value_spec.rb +0 -70
  101. data/spec/rspec/mocks/combining_implementation_instructions_spec.rb +0 -204
  102. data/spec/rspec/mocks/configuration_spec.rb +0 -242
  103. data/spec/rspec/mocks/double_spec.rb +0 -874
  104. data/spec/rspec/mocks/example_methods_spec.rb +0 -13
  105. data/spec/rspec/mocks/extensions/marshal_spec.rb +0 -54
  106. data/spec/rspec/mocks/failing_argument_matchers_spec.rb +0 -184
  107. data/spec/rspec/mocks/hash_excluding_matcher_spec.rb +0 -67
  108. data/spec/rspec/mocks/hash_including_matcher_spec.rb +0 -94
  109. data/spec/rspec/mocks/instance_method_stasher_spec.rb +0 -74
  110. data/spec/rspec/mocks/matchers/have_received_spec.rb +0 -347
  111. data/spec/rspec/mocks/matchers/receive_message_chain_spec.rb +0 -198
  112. data/spec/rspec/mocks/matchers/receive_messages_spec.rb +0 -140
  113. data/spec/rspec/mocks/matchers/receive_spec.rb +0 -418
  114. data/spec/rspec/mocks/method_signature_verifier_spec.rb +0 -272
  115. data/spec/rspec/mocks/methods_spec.rb +0 -26
  116. data/spec/rspec/mocks/mock_expectation_error_spec.rb +0 -22
  117. data/spec/rspec/mocks/mock_ordering_spec.rb +0 -114
  118. data/spec/rspec/mocks/multiple_return_value_spec.rb +0 -132
  119. data/spec/rspec/mocks/mutate_const_spec.rb +0 -542
  120. data/spec/rspec/mocks/nil_expectation_warning_spec.rb +0 -52
  121. data/spec/rspec/mocks/null_object_mock_spec.rb +0 -133
  122. data/spec/rspec/mocks/once_counts_spec.rb +0 -52
  123. data/spec/rspec/mocks/options_hash_spec.rb +0 -35
  124. data/spec/rspec/mocks/order_group_spec.rb +0 -27
  125. data/spec/rspec/mocks/partial_double_spec.rb +0 -308
  126. data/spec/rspec/mocks/partial_double_using_mocks_directly_spec.rb +0 -95
  127. data/spec/rspec/mocks/passing_argument_matchers_spec.rb +0 -145
  128. data/spec/rspec/mocks/precise_counts_spec.rb +0 -68
  129. data/spec/rspec/mocks/record_messages_spec.rb +0 -26
  130. data/spec/rspec/mocks/serialization_spec.rb +0 -90
  131. data/spec/rspec/mocks/space_spec.rb +0 -244
  132. data/spec/rspec/mocks/stash_spec.rb +0 -46
  133. data/spec/rspec/mocks/stub_chain_spec.rb +0 -168
  134. data/spec/rspec/mocks/stub_implementation_spec.rb +0 -81
  135. data/spec/rspec/mocks/stub_spec.rb +0 -327
  136. data/spec/rspec/mocks/stubbed_message_expectations_spec.rb +0 -58
  137. data/spec/rspec/mocks/syntax_agnostic_message_matchers_spec.rb +0 -101
  138. data/spec/rspec/mocks/syntax_spec.rb +0 -19
  139. data/spec/rspec/mocks/test_double_spec.rb +0 -49
  140. data/spec/rspec/mocks/to_ary_spec.rb +0 -54
  141. data/spec/rspec/mocks/twice_counts_spec.rb +0 -66
  142. data/spec/rspec/mocks/verifying_double_spec.rb +0 -590
  143. data/spec/rspec/mocks/verifying_message_expecation_spec.rb +0 -69
  144. data/spec/rspec/mocks_spec.rb +0 -192
  145. data/spec/spec_helper.rb +0 -121
@@ -1,347 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module RSpec
4
- module Mocks
5
- describe Matchers::HaveReceived do
6
- describe "expect(...).to have_received" do
7
- it 'passes when the double has received the given message' do
8
- dbl = double_with_met_expectation(:expected_method)
9
- expect(dbl).to have_received(:expected_method)
10
- end
11
-
12
- it 'passes when a null object has received the given message' do
13
- dbl = null_object_with_met_expectation(:expected_method)
14
- expect(dbl).to have_received(:expected_method)
15
- end
16
-
17
- it 'fails when the double has not received the given message' do
18
- dbl = double_with_unmet_expectation(:expected_method)
19
-
20
- expect {
21
- expect(dbl).to have_received(:expected_method)
22
- }.to raise_error(/expected: 1 time/)
23
- end
24
-
25
- it 'fails when a null object has not received the given message' do
26
- dbl = double.as_null_object
27
-
28
- expect {
29
- expect(dbl).to have_received(:expected_method)
30
- }.to raise_error(/expected: 1 time/)
31
- end
32
-
33
- it 'fails when the method has not been previously stubbed' do
34
- dbl = double
35
-
36
- expect {
37
- expect(dbl).to have_received(:expected_method)
38
- }.to raise_error(/method has not been stubbed/)
39
- end
40
-
41
- it 'fails when the method has been mocked' do
42
- dbl = double
43
- dbl.should_receive(:expected_method)
44
- dbl.expected_method
45
-
46
- expect {
47
- expect(dbl).to have_received(:expected_method)
48
- }.to raise_error(/method has been mocked instead of stubbed/)
49
- end
50
-
51
- it "takes a curly-bracket block and yields the arguments given to the stubbed method call" do
52
- dbl = double(:foo => nil)
53
- yielded = []
54
- dbl.foo(:a, :b, :c)
55
- expect(dbl).to have_received(:foo) { |*args|
56
- yielded << args
57
- }
58
- expect(yielded).to include([:a,:b,:c])
59
- end
60
-
61
- it "takes a do-end block and yields the arguments given to the stubbed method call" do
62
- dbl = double(:foo => nil)
63
- yielded = []
64
- dbl.foo(:a, :b, :c)
65
- expect(dbl).to have_received(:foo) do |*args|
66
- yielded << args
67
- end
68
- expect(yielded).to include([:a,:b,:c])
69
- end
70
-
71
- it "passes if expectations against the yielded arguments pass" do
72
- dbl = double(:foo => nil)
73
- dbl.foo(42)
74
- expect {
75
- expect(dbl).to have_received(:foo) { |arg|
76
- expect(arg).to eq(42)
77
- }
78
- }.to_not raise_error
79
- end
80
-
81
- it "fails if expectations against the yielded arguments fail" do
82
- dbl = double(:foo => nil)
83
- dbl.foo(43)
84
- expect {
85
- expect(dbl).to have_received(:foo) { |arg|
86
- expect(arg).to eq(42)
87
- }
88
- }.to raise_error(RSpec::Expectations::ExpectationNotMetError)
89
- end
90
-
91
- it 'gives precedence to a `{ ... }` block when both forms are provided ' +
92
- 'since that form actually binds to `receive`' do
93
- dbl = double(:foo => nil)
94
- called = []
95
- dbl.foo
96
- expect(dbl).to have_received(:foo) { called << :curly } do
97
- called << :do_end
98
- end
99
- expect(called).to include(:curly)
100
- expect(called).not_to include(:do_end)
101
- end
102
-
103
- it 'resets expectations on class methods when mocks are reset' do
104
- dbl = Object
105
- dbl.stub(:expected_method)
106
- dbl.expected_method
107
- reset dbl
108
- dbl.stub(:expected_method)
109
-
110
- expect {
111
- expect(dbl).to have_received(:expected_method)
112
- }.to raise_error(/0 times/)
113
- end
114
-
115
- context "with" do
116
- it 'passes when the given args match the args used with the message' do
117
- dbl = double_with_met_expectation(:expected_method, :expected, :args)
118
- expect(dbl).to have_received(:expected_method).with(:expected, :args)
119
- end
120
-
121
- it 'fails when the given args do not match the args used with the message' do
122
- dbl = double_with_met_expectation(:expected_method, :expected, :args)
123
-
124
- expect {
125
- expect(dbl).to have_received(:expected_method).with(:unexpected, :args)
126
- }.to raise_error(/with unexpected arguments/)
127
- end
128
- end
129
-
130
- it 'generates a useful description' do
131
- matcher = have_received(:expected_method).with(:expected_args).once
132
- expect(matcher.description).to eq 'have received expected_method(:expected_args) 1 time'
133
- end
134
-
135
- it 'can generate a description after mocks have been torn down (e.g. when rspec-core requests it)' do
136
- matcher = have_received(:expected_method).with(:expected_args).once
137
- matcher.matches?(double(:expected_method => 1))
138
- RSpec::Mocks.teardown
139
- expect(matcher.description).to eq 'have received expected_method(:expected_args) 1 time'
140
- end
141
-
142
- context "counts" do
143
- let(:dbl) { double(:expected_method => nil) }
144
-
145
- before do
146
- dbl.expected_method
147
- dbl.expected_method
148
- dbl.expected_method
149
- end
150
-
151
- context "exactly" do
152
- it 'passes when the message was received the given number of times' do
153
- expect(dbl).to have_received(:expected_method).exactly(3).times
154
- end
155
-
156
- it 'fails when the message was received more times' do
157
- expect {
158
- expect(dbl).to have_received(:expected_method).exactly(2).times
159
- }.to raise_error(/expected: 2 times.*received: 3 times/m)
160
- end
161
-
162
- it 'fails when the message was received fewer times' do
163
- expect {
164
- expect(dbl).to have_received(:expected_method).exactly(4).times
165
- }.to raise_error(/expected: 4 times.*received: 3 times/m)
166
- end
167
- end
168
-
169
- context 'at_least' do
170
- it 'passes when the message was received the given number of times' do
171
- expect(dbl).to have_received(:expected_method).at_least(3).times
172
- end
173
-
174
- it 'passes when the message was received more times' do
175
- expect(dbl).to have_received(:expected_method).at_least(2).times
176
- end
177
-
178
- it 'fails when the message was received fewer times' do
179
- expect {
180
- expect(dbl).to have_received(:expected_method).at_least(4).times
181
- }.to raise_error(/expected: at least 4 times.*received: 3 times/m)
182
- end
183
- end
184
-
185
- context 'at_most' do
186
- it 'passes when the message was received the given number of times' do
187
- expect(dbl).to have_received(:expected_method).at_most(3).times
188
- end
189
-
190
- it 'passes when the message was received fewer times' do
191
- expect(dbl).to have_received(:expected_method).at_most(4).times
192
- end
193
-
194
- it 'fails when the message was received more times' do
195
- expect {
196
- expect(dbl).to have_received(:expected_method).at_most(2).times
197
- }.to raise_error(/expected: at most 2 times.*received: 3 times/m)
198
- end
199
- end
200
-
201
- context 'once' do
202
- it 'passes when the message was received once' do
203
- dbl = double(:expected_method => nil)
204
- dbl.expected_method
205
- expect(dbl).to have_received(:expected_method).once
206
- end
207
-
208
- it 'fails when the message was never received' do
209
- dbl = double(:expected_method => nil)
210
-
211
- expect {
212
- expect(dbl).to have_received(:expected_method).once
213
- }.to raise_error(/expected: 1 time.*received: 0 times/m)
214
- end
215
-
216
- it 'fails when the message was received twice' do
217
- dbl = double(:expected_method => nil)
218
- dbl.expected_method
219
- dbl.expected_method
220
-
221
- expect {
222
- expect(dbl).to have_received(:expected_method).once
223
- }.to raise_error(/expected: 1 time.*received: 2 times/m)
224
- end
225
- end
226
-
227
- context 'twice' do
228
- it 'passes when the message was received twice' do
229
- dbl = double(:expected_method => nil)
230
- dbl.expected_method
231
- dbl.expected_method
232
-
233
- expect(dbl).to have_received(:expected_method).twice
234
- end
235
-
236
- it 'fails when the message was received once' do
237
- dbl = double(:expected_method => nil)
238
- dbl.expected_method
239
-
240
- expect {
241
- expect(dbl).to have_received(:expected_method).twice
242
- }.to raise_error(/expected: 2 times.*received: 1 time/m)
243
- end
244
-
245
- it 'fails when the message was received thrice' do
246
- dbl = double(:expected_method => nil)
247
- dbl.expected_method
248
- dbl.expected_method
249
- dbl.expected_method
250
-
251
- expect {
252
- expect(dbl).to have_received(:expected_method).twice
253
- }.to raise_error(/expected: 2 times.*received: 3 times/m)
254
- end
255
- end
256
- end
257
-
258
- context 'ordered' do
259
- let(:dbl) { double :one => 1, :two => 2 }
260
-
261
- it 'passes when the messages were received in order' do
262
- dbl.one
263
- dbl.two
264
-
265
- expect(dbl).to have_received(:one).ordered
266
- expect(dbl).to have_received(:two).ordered
267
- end
268
-
269
- it 'fails when the messages are received out of order' do
270
- dbl.two
271
- dbl.one
272
-
273
- expect {
274
- expect(dbl).to have_received(:one).ordered
275
- expect(dbl).to have_received(:two).ordered
276
- }.to raise_error(/received :two out of order/m)
277
- end
278
- end
279
- end
280
-
281
- describe "expect(...).not_to have_received" do
282
- it 'passes when the double has not received the given message' do
283
- dbl = double_with_unmet_expectation(:expected_method)
284
- expect(dbl).not_to have_received(:expected_method)
285
- end
286
-
287
- it 'fails when the double has received the given message' do
288
- dbl = double_with_met_expectation(:expected_method)
289
-
290
- expect {
291
- expect(dbl).not_to have_received(:expected_method)
292
- }.to raise_error(/expected: 0 times.*received: 1 time/m)
293
- end
294
-
295
- it 'fails when the method has not been previously stubbed' do
296
- dbl = double
297
-
298
- expect {
299
- expect(dbl).not_to have_received(:expected_method)
300
- }.to raise_error(/method has not been stubbed/)
301
- end
302
-
303
- context "with" do
304
- it 'passes when the given args do not match the args used with the message' do
305
- dbl = double_with_met_expectation(:expected_method, :expected, :args)
306
- expect(dbl).not_to have_received(:expected_method).with(:unexpected, :args)
307
- end
308
-
309
- it 'fails when the given args match the args used with the message' do
310
- dbl = double_with_met_expectation(:expected_method, :expected, :args)
311
-
312
- expect {
313
- expect(dbl).not_to have_received(:expected_method).with(:expected, :args)
314
- }.to raise_error(/expected: 0 times.*received: 1 time/m) # TODO: better message
315
- end
316
- end
317
-
318
- %w(exactly at_least at_most times once twice).each do |constraint|
319
- it "does not allow #{constraint} to be used because it creates confusion" do
320
- dbl = double_with_unmet_expectation(:expected_method)
321
- expect {
322
- expect(dbl).not_to have_received(:expected_method).send(constraint)
323
- }.to raise_error(/can't use #{constraint} when negative/)
324
- end
325
- end
326
- end
327
-
328
- def double_with_met_expectation(method_name, *args)
329
- double = double_with_unmet_expectation(method_name)
330
- meet_expectation(double, method_name, *args)
331
- end
332
-
333
- def null_object_with_met_expectation(method_name, *args)
334
- meet_expectation(double.as_null_object, method_name, *args)
335
- end
336
-
337
- def meet_expectation(double, method_name, *args)
338
- double.send(method_name, *args)
339
- double
340
- end
341
-
342
- def double_with_unmet_expectation(method_name)
343
- double('double', method_name => true)
344
- end
345
- end
346
- end
347
- end
@@ -1,198 +0,0 @@
1
- require "spec_helper"
2
-
3
-
4
- module RSpec::Mocks::Matchers
5
- describe "receive_message_chain" do
6
- let(:object) { double(:object) }
7
-
8
- context "with only the expect syntax enabled" do
9
- include_context "with syntax", :expect
10
-
11
- it "errors with a negative allowance" do
12
- expect {
13
- allow(object).not_to receive_message_chain(:to_a)
14
- }.to raise_error(RSpec::Mocks::NegationUnsupportedError)
15
- end
16
-
17
- it "errors with a negative expectation" do
18
- expect {
19
- expect(object).not_to receive_message_chain(:to_a)
20
- }.to raise_error(RSpec::Mocks::NegationUnsupportedError)
21
- end
22
-
23
- it "errors with a negative any_instance expectation" do
24
- expect {
25
- expect_any_instance_of(Object).not_to receive_message_chain(:to_a)
26
- }.to raise_error(RSpec::Mocks::NegationUnsupportedError)
27
- end
28
-
29
- it "errors with a negative any_instance allowance" do
30
- expect {
31
- allow_any_instance_of(Object).not_to receive_message_chain(:to_a)
32
- }.to raise_error(RSpec::Mocks::NegationUnsupportedError)
33
- end
34
-
35
- it "works with a do block" do
36
- allow(object).to receive_message_chain(:to_a, :length) do
37
- 3
38
- end
39
-
40
- expect(object.to_a.length).to eq(3)
41
- end
42
-
43
- it "works with a {} block" do
44
- allow(object).to receive_message_chain(:to_a, :length) { 3 }
45
-
46
- expect(object.to_a.length).to eq(3)
47
- end
48
-
49
- it "gives the { } block prescedence over the do block" do
50
- allow(object).to receive_message_chain(:to_a, :length) { 3 } do
51
- 4
52
- end
53
-
54
- expect(object.to_a.length).to eq(3)
55
- end
56
-
57
- it "works with and_return" do
58
- allow(object).to receive_message_chain(:to_a, :length).and_return(3)
59
-
60
- expect(object.to_a.length).to eq(3)
61
- end
62
-
63
- it "works with and_call_original", :pending => "See https://github.com/rspec/rspec-mocks/pull/467#issuecomment-28631621" do
64
- list = [1, 2, 3]
65
- expect(list).to receive_message_chain(:to_a, :length).and_call_original
66
- expect(list.to_a.length).to eq(3)
67
- end
68
-
69
- it "fails with and_call_original when the entire chain is not called", :pending => "See https://github.com/rspec/rspec-mocks/pull/467#issuecomment-28631621" do
70
- list = [1, 2, 3]
71
- expect(list).to receive_message_chain(:to_a, :length).and_call_original
72
- expect(list.to_a).to eq([1, 2, 3])
73
- end
74
-
75
- it "works with and_raise" do
76
- allow(object).to receive_message_chain(:to_a, :length).and_raise(StandardError.new("hi"))
77
-
78
- expect { object.to_a.length }.to raise_error(StandardError, "hi")
79
- end
80
-
81
- it "works with and_throw" do
82
- allow(object).to receive_message_chain(:to_a, :length).and_throw(:nope)
83
-
84
- expect { object.to_a.length }.to throw_symbol(:nope)
85
- end
86
-
87
- it "works with and_yield" do
88
- allow(object).to receive_message_chain(:to_a, :length).and_yield(3)
89
-
90
- expect { |blk| object.to_a.length(&blk) }.to yield_with_args(3)
91
- end
92
-
93
- it "works with a string of messages to chain" do
94
- allow(object).to receive_message_chain("to_a.length").and_yield(3)
95
-
96
- expect { |blk| object.to_a.length(&blk) }.to yield_with_args(3)
97
- end
98
-
99
- it "works with a hash return as the last argument in the chain" do
100
- allow(object).to receive_message_chain(:to_a, :length => 3)
101
-
102
- expect(object.to_a.length).to eq(3)
103
- end
104
-
105
- it "accepts any number of arguments to the stubbed messages" do
106
- allow(object).to receive_message_chain(:msg1, :msg2).and_return(:return_value)
107
-
108
- expect(object.msg1("nonsense", :value).msg2("another", :nonsense, 3.0, "value")).to eq(:return_value)
109
- end
110
-
111
- it "accepts any number of arguments to the stubbed messages with an inline hash return value" do
112
- allow(object).to receive_message_chain(:msg1, :msg2 => :return_value)
113
-
114
- expect(object.msg1("nonsense", :value).msg2("another", :nonsense, 3.0, "value")).to eq(:return_value)
115
- end
116
-
117
- it "raises when expect is used and the entire chain isn't called" do
118
- expect {
119
- expect(object).to receive_message_chain(:to_a, :farce, :length => 3)
120
- object.to_a
121
- verify_all
122
- }.to raise_error(RSpec::Mocks::MockExpectationError)
123
- end
124
-
125
- it "does not raise when expect is used and the entire chain is called" do
126
- expect {
127
- expect(object).to receive_message_chain(:to_a, :length => 3)
128
- object.to_a.length
129
- verify_all
130
- }.not_to raise_error
131
- end
132
-
133
- it "works with allow_any_instance" do
134
- o = Object.new
135
-
136
- allow_any_instance_of(Object).to receive_message_chain(:to_a, :length => 3)
137
-
138
- expect(o.to_a.length).to eq(3)
139
- end
140
-
141
- it "fails when with expect_any_instance_of is used and the entire chain is not called" do
142
- expect {
143
- expect_any_instance_of(Object).to receive_message_chain(:to_a, :length => 3)
144
- verify_all
145
- }.to raise_error(RSpec::Mocks::MockExpectationError)
146
- end
147
-
148
- it "passes when with expect_any_instance_of is used and the entire chain is called" do
149
- o = Object.new
150
-
151
- expect_any_instance_of(Object).to receive_message_chain(:to_a, :length => 3)
152
- o.to_a.length
153
- end
154
-
155
- it "works with expect where the first level of the chain is already expected" do
156
- o = Object.new
157
- expect(o).to receive(:foo).and_return(double)
158
- expect(o).to receive_message_chain(:foo, :bar, :baz)
159
-
160
- o.foo.bar.baz
161
- end
162
-
163
- it "works with allow where the first level of the chain is already expected" do
164
- o = Object.new
165
- expect(o).to receive(:foo).and_return(double)
166
- allow(o).to receive_message_chain(:foo, :bar, :baz).and_return(3)
167
-
168
- expect(o.foo.bar.baz).to eq(3)
169
- end
170
-
171
- it "works with expect where the first level of the chain is already stubbed" do
172
- o = Object.new
173
- allow(o).to receive(:foo).and_return(double)
174
- expect(o).to receive_message_chain(:foo, :bar, :baz)
175
-
176
- o.foo.bar.baz
177
- end
178
-
179
- it "works with allow where the first level of the chain is already stubbed" do
180
- o = Object.new
181
- allow(o).to receive(:foo).and_return(double)
182
- allow(o).to receive_message_chain(:foo, :bar, :baz).and_return(3)
183
-
184
- expect(o.foo.bar.baz).to eq(3)
185
- end
186
- end
187
-
188
- context "when the expect and should syntaxes are enabled" do
189
- include_context "with syntax", [:expect, :should]
190
-
191
- it "stubs the message correctly" do
192
- allow(object).to receive_message_chain(:to_a, :length)
193
-
194
- expect { object.to_a.length }.not_to raise_error
195
- end
196
- end
197
- end
198
- end