rspec-mocks 2.11.3 → 3.11.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +0 -0
- data/.document +1 -1
- data/.yardopts +1 -1
- data/Changelog.md +989 -21
- data/{License.txt → LICENSE.md} +5 -3
- data/README.md +260 -73
- data/lib/rspec/mocks/any_instance/chain.rb +58 -24
- data/lib/rspec/mocks/any_instance/error_generator.rb +31 -0
- data/lib/rspec/mocks/any_instance/expect_chain_chain.rb +31 -0
- data/lib/rspec/mocks/any_instance/expectation_chain.rb +23 -30
- data/lib/rspec/mocks/any_instance/message_chains.rb +38 -15
- data/lib/rspec/mocks/any_instance/proxy.rb +116 -0
- data/lib/rspec/mocks/any_instance/recorder.rb +155 -59
- data/lib/rspec/mocks/any_instance/stub_chain.rb +33 -19
- data/lib/rspec/mocks/any_instance/stub_chain_chain.rb +10 -12
- data/lib/rspec/mocks/any_instance.rb +11 -81
- data/lib/rspec/mocks/argument_list_matcher.rb +59 -37
- data/lib/rspec/mocks/argument_matchers.rb +233 -149
- data/lib/rspec/mocks/configuration.rb +212 -0
- data/lib/rspec/mocks/error_generator.rb +304 -49
- data/lib/rspec/mocks/example_methods.rb +361 -22
- data/lib/rspec/mocks/instance_method_stasher.rb +146 -0
- data/lib/rspec/mocks/marshal_extension.rb +41 -0
- data/lib/rspec/mocks/matchers/expectation_customization.rb +20 -0
- data/lib/rspec/mocks/matchers/have_received.rb +134 -0
- data/lib/rspec/mocks/matchers/receive.rb +133 -0
- data/lib/rspec/mocks/matchers/receive_message_chain.rb +82 -0
- data/lib/rspec/mocks/matchers/receive_messages.rb +77 -0
- data/lib/rspec/mocks/message_chain.rb +87 -0
- data/lib/rspec/mocks/message_expectation.rb +648 -314
- data/lib/rspec/mocks/method_double.rb +185 -58
- data/lib/rspec/mocks/method_reference.rb +202 -0
- data/lib/rspec/mocks/minitest_integration.rb +68 -0
- data/lib/rspec/mocks/mutate_const.rb +339 -0
- data/lib/rspec/mocks/object_reference.rb +149 -0
- data/lib/rspec/mocks/order_group.rb +48 -7
- data/lib/rspec/mocks/proxy.rb +405 -74
- data/lib/rspec/mocks/space.rb +219 -15
- data/lib/rspec/mocks/standalone.rb +2 -2
- data/lib/rspec/mocks/syntax.rb +325 -0
- data/lib/rspec/mocks/targets.rb +124 -0
- data/lib/rspec/mocks/test_double.rb +125 -57
- data/lib/rspec/mocks/verifying_double.rb +121 -0
- data/lib/rspec/mocks/verifying_message_expectation.rb +54 -0
- data/lib/rspec/mocks/verifying_proxy.rb +220 -0
- data/lib/rspec/mocks/version.rb +3 -1
- data/lib/rspec/mocks.rb +121 -27
- data.tar.gz.sig +0 -0
- metadata +178 -253
- metadata.gz.sig +3 -0
- data/features/README.md +0 -67
- data/features/Scope.md +0 -17
- data/features/Upgrade.md +0 -22
- data/features/argument_matchers/README.md +0 -27
- data/features/argument_matchers/explicit.feature +0 -60
- data/features/argument_matchers/general_matchers.feature +0 -85
- data/features/argument_matchers/type_matchers.feature +0 -27
- data/features/message_expectations/README.md +0 -69
- data/features/message_expectations/any_instance.feature +0 -21
- data/features/message_expectations/block_local_expectations.feature.pending +0 -55
- data/features/message_expectations/expect_message.feature +0 -94
- data/features/message_expectations/receive_counts.feature +0 -209
- data/features/message_expectations/warn_when_expectation_is_set_on_nil.feature +0 -50
- data/features/method_stubs/README.md +0 -47
- data/features/method_stubs/any_instance.feature +0 -133
- data/features/method_stubs/as_null_object.feature +0 -35
- data/features/method_stubs/simple_return_value.feature +0 -64
- data/features/method_stubs/stub_chain.feature +0 -51
- data/features/method_stubs/stub_implementation.feature +0 -26
- data/features/method_stubs/to_ary.feature +0 -47
- data/features/outside_rspec/configuration.feature +0 -82
- data/features/outside_rspec/standalone.feature +0 -32
- data/features/step_definitions/additional_cli_steps.rb +0 -4
- data/features/stubbing_constants/README.md +0 -62
- data/features/stubbing_constants/stub_defined_constant.feature +0 -79
- data/features/stubbing_constants/stub_undefined_constant.feature +0 -50
- data/features/support/env.rb +0 -6
- data/lib/rspec/mocks/errors.rb +0 -12
- data/lib/rspec/mocks/extensions/instance_exec.rb +0 -34
- data/lib/rspec/mocks/extensions/marshal.rb +0 -23
- data/lib/rspec/mocks/extensions/psych.rb +0 -23
- data/lib/rspec/mocks/framework.rb +0 -21
- data/lib/rspec/mocks/methods.rb +0 -155
- data/lib/rspec/mocks/mock.rb +0 -7
- data/lib/rspec/mocks/serialization.rb +0 -34
- data/lib/rspec/mocks/stashed_instance_method.rb +0 -60
- data/lib/rspec/mocks/stub_const.rb +0 -332
- data/lib/spec/mocks.rb +0 -2
- data/spec/rspec/mocks/and_yield_spec.rb +0 -114
- data/spec/rspec/mocks/any_instance/message_chains_spec.rb +0 -40
- data/spec/rspec/mocks/any_instance_spec.rb +0 -877
- data/spec/rspec/mocks/any_number_of_times_spec.rb +0 -30
- data/spec/rspec/mocks/argument_expectation_spec.rb +0 -34
- data/spec/rspec/mocks/at_least_spec.rb +0 -142
- data/spec/rspec/mocks/at_most_spec.rb +0 -90
- data/spec/rspec/mocks/block_return_value_spec.rb +0 -53
- data/spec/rspec/mocks/bug_report_10260_spec.rb +0 -8
- data/spec/rspec/mocks/bug_report_10263_spec.rb +0 -25
- data/spec/rspec/mocks/bug_report_11545_spec.rb +0 -32
- data/spec/rspec/mocks/bug_report_496_spec.rb +0 -17
- data/spec/rspec/mocks/bug_report_600_spec.rb +0 -22
- data/spec/rspec/mocks/bug_report_7611_spec.rb +0 -16
- data/spec/rspec/mocks/bug_report_8165_spec.rb +0 -31
- data/spec/rspec/mocks/bug_report_830_spec.rb +0 -21
- data/spec/rspec/mocks/bug_report_957_spec.rb +0 -22
- data/spec/rspec/mocks/double_spec.rb +0 -12
- data/spec/rspec/mocks/failing_argument_matchers_spec.rb +0 -95
- data/spec/rspec/mocks/hash_excluding_matcher_spec.rb +0 -67
- data/spec/rspec/mocks/hash_including_matcher_spec.rb +0 -90
- data/spec/rspec/mocks/mock_ordering_spec.rb +0 -103
- data/spec/rspec/mocks/mock_space_spec.rb +0 -58
- data/spec/rspec/mocks/mock_spec.rb +0 -730
- data/spec/rspec/mocks/multiple_return_value_spec.rb +0 -119
- data/spec/rspec/mocks/nil_expectation_warning_spec.rb +0 -62
- data/spec/rspec/mocks/null_object_mock_spec.rb +0 -106
- data/spec/rspec/mocks/once_counts_spec.rb +0 -52
- data/spec/rspec/mocks/options_hash_spec.rb +0 -35
- data/spec/rspec/mocks/partial_mock_spec.rb +0 -171
- data/spec/rspec/mocks/partial_mock_using_mocks_directly_spec.rb +0 -95
- data/spec/rspec/mocks/passing_argument_matchers_spec.rb +0 -142
- data/spec/rspec/mocks/precise_counts_spec.rb +0 -68
- data/spec/rspec/mocks/record_messages_spec.rb +0 -26
- data/spec/rspec/mocks/serialization_spec.rb +0 -111
- data/spec/rspec/mocks/stash_spec.rb +0 -27
- data/spec/rspec/mocks/stashed_instance_method_spec.rb +0 -53
- data/spec/rspec/mocks/stub_chain_spec.rb +0 -154
- data/spec/rspec/mocks/stub_const_spec.rb +0 -334
- data/spec/rspec/mocks/stub_implementation_spec.rb +0 -81
- data/spec/rspec/mocks/stub_spec.rb +0 -247
- data/spec/rspec/mocks/stubbed_message_expectations_spec.rb +0 -47
- data/spec/rspec/mocks/test_double_spec.rb +0 -57
- data/spec/rspec/mocks/to_ary_spec.rb +0 -40
- data/spec/rspec/mocks/twice_counts_spec.rb +0 -66
- data/spec/rspec/mocks_spec.rb +0 -51
- data/spec/spec_helper.rb +0 -21
@@ -1,877 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module RSpec
|
4
|
-
module Mocks
|
5
|
-
describe "#any_instance" do
|
6
|
-
class CustomErrorForAnyInstanceSpec < StandardError;end
|
7
|
-
|
8
|
-
let(:klass) do
|
9
|
-
Class.new do
|
10
|
-
def existing_method; :existing_method_return_value; end
|
11
|
-
def existing_method_with_arguments(arg_one, arg_two = nil); :existing_method_with_arguments_return_value; end
|
12
|
-
def another_existing_method; end
|
13
|
-
private
|
14
|
-
def private_method; :private_method_return_value; end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
let(:existing_method_return_value){ :existing_method_return_value }
|
18
|
-
|
19
|
-
context "invocation order" do
|
20
|
-
context "#stub" do
|
21
|
-
it "raises an error if 'stub' follows 'with'" do
|
22
|
-
lambda{ klass.any_instance.with("1").stub(:foo) }.should raise_error(NoMethodError)
|
23
|
-
end
|
24
|
-
|
25
|
-
it "raises an error if 'with' follows 'and_return'" do
|
26
|
-
lambda{ klass.any_instance.stub(:foo).and_return(1).with("1") }.should raise_error(NoMethodError)
|
27
|
-
end
|
28
|
-
|
29
|
-
it "raises an error if 'with' follows 'and_raise'" do
|
30
|
-
lambda{ klass.any_instance.stub(:foo).and_raise(1).with("1") }.should raise_error(NoMethodError)
|
31
|
-
end
|
32
|
-
|
33
|
-
it "raises an error if 'with' follows 'and_yield'" do
|
34
|
-
lambda{ klass.any_instance.stub(:foo).and_yield(1).with("1") }.should raise_error(NoMethodError)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
context "#stub_chain" do
|
39
|
-
it "raises an error if 'stub_chain' follows 'any_instance'" do
|
40
|
-
lambda{ klass.any_instance.and_return("1").stub_chain(:foo, :bar) }.should raise_error(NoMethodError)
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
context "#should_receive" do
|
45
|
-
it "raises an error if 'should_receive' follows 'with'" do
|
46
|
-
lambda{ klass.any_instance.with("1").should_receive(:foo) }.should raise_error(NoMethodError)
|
47
|
-
end
|
48
|
-
|
49
|
-
it "raises an error if 'with' follows 'and_return'" do
|
50
|
-
pending "see Github issue #42"
|
51
|
-
lambda{ klass.any_instance.should_receive(:foo).and_return(1).with("1") }.should raise_error(NoMethodError)
|
52
|
-
end
|
53
|
-
|
54
|
-
it "raises an error if 'with' follows 'and_raise'" do
|
55
|
-
pending "see Github issue #42"
|
56
|
-
lambda{ klass.any_instance.should_receive(:foo).and_raise(1).with("1") }.should raise_error(NoMethodError)
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
context "with #stub" do
|
62
|
-
it "does not suppress an exception when a method that doesn't exist is invoked" do
|
63
|
-
klass.any_instance.stub(:foo)
|
64
|
-
lambda{ klass.new.bar }.should raise_error(NoMethodError)
|
65
|
-
end
|
66
|
-
|
67
|
-
context 'multiple methods' do
|
68
|
-
it "allows multiple methods to be stubbed in a single invocation" do
|
69
|
-
klass.any_instance.stub(:foo => 'foo', :bar => 'bar')
|
70
|
-
instance = klass.new
|
71
|
-
instance.foo.should eq('foo')
|
72
|
-
instance.bar.should eq('bar')
|
73
|
-
end
|
74
|
-
|
75
|
-
it "adheres to the contract of multiple method stubbing withou any instance" do
|
76
|
-
Object.new.stub(:foo => 'foo', :bar => 'bar').should eq(:foo => 'foo', :bar => 'bar')
|
77
|
-
klass.any_instance.stub(:foo => 'foo', :bar => 'bar').should eq(:foo => 'foo', :bar => 'bar')
|
78
|
-
end
|
79
|
-
|
80
|
-
context "allows a chain of methods to be stubbed using #stub_chain" do
|
81
|
-
it "given symbols representing the methods" do
|
82
|
-
klass.any_instance.stub_chain(:one, :two, :three).and_return(:four)
|
83
|
-
klass.new.one.two.three.should eq(:four)
|
84
|
-
end
|
85
|
-
|
86
|
-
it "given a hash as the last argument uses the value as the expected return value" do
|
87
|
-
klass.any_instance.stub_chain(:one, :two, :three => :four)
|
88
|
-
klass.new.one.two.three.should eq(:four)
|
89
|
-
end
|
90
|
-
|
91
|
-
it "given a string of '.' separated method names representing the chain" do
|
92
|
-
klass.any_instance.stub_chain('one.two.three').and_return(:four)
|
93
|
-
klass.new.one.two.three.should eq(:four)
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
context "behaves as 'every instance'" do
|
99
|
-
it "stubs every instance in the spec" do
|
100
|
-
klass.any_instance.stub(:foo).and_return(result = Object.new)
|
101
|
-
klass.new.foo.should eq(result)
|
102
|
-
klass.new.foo.should eq(result)
|
103
|
-
end
|
104
|
-
|
105
|
-
it "stubs instance created before any_instance was called" do
|
106
|
-
instance = klass.new
|
107
|
-
klass.any_instance.stub(:foo).and_return(result = Object.new)
|
108
|
-
instance.foo.should eq(result)
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
context "with argument matching" do
|
113
|
-
before do
|
114
|
-
klass.any_instance.stub(:foo).with(:param_one, :param_two).and_return(:result_one)
|
115
|
-
klass.any_instance.stub(:foo).with(:param_three, :param_four).and_return(:result_two)
|
116
|
-
end
|
117
|
-
|
118
|
-
it "returns the stubbed value when arguments match" do
|
119
|
-
instance = klass.new
|
120
|
-
instance.foo(:param_one, :param_two).should eq(:result_one)
|
121
|
-
instance.foo(:param_three, :param_four).should eq(:result_two)
|
122
|
-
end
|
123
|
-
|
124
|
-
it "fails the spec with an expectation error when the arguments do not match" do
|
125
|
-
expect do
|
126
|
-
klass.new.foo(:param_one, :param_three)
|
127
|
-
end.to(raise_error(RSpec::Mocks::MockExpectationError))
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
context "with multiple stubs" do
|
132
|
-
before do
|
133
|
-
klass.any_instance.stub(:foo).and_return(1)
|
134
|
-
klass.any_instance.stub(:bar).and_return(2)
|
135
|
-
end
|
136
|
-
|
137
|
-
it "stubs a method" do
|
138
|
-
instance = klass.new
|
139
|
-
instance.foo.should eq(1)
|
140
|
-
instance.bar.should eq(2)
|
141
|
-
end
|
142
|
-
|
143
|
-
it "returns the same value for calls on different instances" do
|
144
|
-
klass.new.foo.should eq(klass.new.foo)
|
145
|
-
klass.new.bar.should eq(klass.new.bar)
|
146
|
-
end
|
147
|
-
end
|
148
|
-
|
149
|
-
context "with #and_return" do
|
150
|
-
it "stubs a method that doesn't exist" do
|
151
|
-
klass.any_instance.stub(:foo).and_return(1)
|
152
|
-
klass.new.foo.should eq(1)
|
153
|
-
end
|
154
|
-
|
155
|
-
it "stubs a method that exists" do
|
156
|
-
klass.any_instance.stub(:existing_method).and_return(1)
|
157
|
-
klass.new.existing_method.should eq(1)
|
158
|
-
end
|
159
|
-
|
160
|
-
it "returns the same object for calls on different instances" do
|
161
|
-
return_value = Object.new
|
162
|
-
klass.any_instance.stub(:foo).and_return(return_value)
|
163
|
-
klass.new.foo.should be(return_value)
|
164
|
-
klass.new.foo.should be(return_value)
|
165
|
-
end
|
166
|
-
end
|
167
|
-
|
168
|
-
context "with #and_yield" do
|
169
|
-
it "yields the value specified" do
|
170
|
-
yielded_value = Object.new
|
171
|
-
klass.any_instance.stub(:foo).and_yield(yielded_value)
|
172
|
-
klass.new.foo{|value| value.should be(yielded_value)}
|
173
|
-
end
|
174
|
-
end
|
175
|
-
|
176
|
-
context "with #and_raise" do
|
177
|
-
it "stubs a method that doesn't exist" do
|
178
|
-
klass.any_instance.stub(:foo).and_raise(CustomErrorForAnyInstanceSpec)
|
179
|
-
lambda{ klass.new.foo}.should raise_error(CustomErrorForAnyInstanceSpec)
|
180
|
-
end
|
181
|
-
|
182
|
-
it "stubs a method that exists" do
|
183
|
-
klass.any_instance.stub(:existing_method).and_raise(CustomErrorForAnyInstanceSpec)
|
184
|
-
lambda{ klass.new.existing_method}.should raise_error(CustomErrorForAnyInstanceSpec)
|
185
|
-
end
|
186
|
-
end
|
187
|
-
|
188
|
-
context "with a block" do
|
189
|
-
it "stubs a method" do
|
190
|
-
klass.any_instance.stub(:foo) { 1 }
|
191
|
-
klass.new.foo.should eq(1)
|
192
|
-
end
|
193
|
-
|
194
|
-
it "returns the same computed value for calls on different instances" do
|
195
|
-
klass.any_instance.stub(:foo) { 1 + 2 }
|
196
|
-
klass.new.foo.should eq(klass.new.foo)
|
197
|
-
end
|
198
|
-
end
|
199
|
-
|
200
|
-
context "core ruby objects" do
|
201
|
-
it "works uniformly across *everything*" do
|
202
|
-
Object.any_instance.stub(:foo).and_return(1)
|
203
|
-
Object.new.foo.should eq(1)
|
204
|
-
end
|
205
|
-
|
206
|
-
it "works with the non-standard constructor []" do
|
207
|
-
Array.any_instance.stub(:foo).and_return(1)
|
208
|
-
[].foo.should eq(1)
|
209
|
-
end
|
210
|
-
|
211
|
-
it "works with the non-standard constructor {}" do
|
212
|
-
Hash.any_instance.stub(:foo).and_return(1)
|
213
|
-
{}.foo.should eq(1)
|
214
|
-
end
|
215
|
-
|
216
|
-
it "works with the non-standard constructor \"\"" do
|
217
|
-
String.any_instance.stub(:foo).and_return(1)
|
218
|
-
"".foo.should eq(1)
|
219
|
-
end
|
220
|
-
|
221
|
-
it "works with the non-standard constructor \'\'" do
|
222
|
-
String.any_instance.stub(:foo).and_return(1)
|
223
|
-
''.foo.should eq(1)
|
224
|
-
end
|
225
|
-
|
226
|
-
it "works with the non-standard constructor module" do
|
227
|
-
Module.any_instance.stub(:foo).and_return(1)
|
228
|
-
module RSpec::SampleRspecTestModule;end
|
229
|
-
RSpec::SampleRspecTestModule.foo.should eq(1)
|
230
|
-
end
|
231
|
-
|
232
|
-
it "works with the non-standard constructor class" do
|
233
|
-
Class.any_instance.stub(:foo).and_return(1)
|
234
|
-
class RSpec::SampleRspecTestClass;end
|
235
|
-
RSpec::SampleRspecTestClass.foo.should eq(1)
|
236
|
-
end
|
237
|
-
end
|
238
|
-
end
|
239
|
-
|
240
|
-
context "with #stub!" do
|
241
|
-
it "raises with a message instructing the user to use stub instead" do
|
242
|
-
expect do
|
243
|
-
klass.any_instance.stub!(:foo)
|
244
|
-
end.to raise_error(/Use stub instead/)
|
245
|
-
end
|
246
|
-
end
|
247
|
-
|
248
|
-
context "unstub implementation" do
|
249
|
-
it "replaces the stubbed method with the original method" do
|
250
|
-
klass.any_instance.stub(:existing_method)
|
251
|
-
klass.any_instance.unstub(:existing_method)
|
252
|
-
klass.new.existing_method.should eq(:existing_method_return_value)
|
253
|
-
end
|
254
|
-
|
255
|
-
it "removes all stubs with the supplied method name" do
|
256
|
-
klass.any_instance.stub(:existing_method).with(1)
|
257
|
-
klass.any_instance.stub(:existing_method).with(2)
|
258
|
-
klass.any_instance.unstub(:existing_method)
|
259
|
-
klass.new.existing_method.should eq(:existing_method_return_value)
|
260
|
-
end
|
261
|
-
|
262
|
-
it "does not remove any expectations with the same method name" do
|
263
|
-
klass.any_instance.should_receive(:existing_method_with_arguments).with(3).and_return(:three)
|
264
|
-
klass.any_instance.stub(:existing_method_with_arguments).with(1)
|
265
|
-
klass.any_instance.stub(:existing_method_with_arguments).with(2)
|
266
|
-
klass.any_instance.unstub(:existing_method_with_arguments)
|
267
|
-
klass.new.existing_method_with_arguments(3).should eq(:three)
|
268
|
-
end
|
269
|
-
|
270
|
-
it "raises a MockExpectationError if the method has not been stubbed" do
|
271
|
-
lambda do
|
272
|
-
klass.any_instance.unstub(:existing_method)
|
273
|
-
end.should raise_error(RSpec::Mocks::MockExpectationError, 'The method `existing_method` was not stubbed or was already unstubbed')
|
274
|
-
end
|
275
|
-
end
|
276
|
-
|
277
|
-
context "with #should_not_receive" do
|
278
|
-
it "fails if the method is called" do
|
279
|
-
klass.any_instance.should_not_receive(:existing_method)
|
280
|
-
lambda { klass.new.existing_method }.should raise_error(RSpec::Mocks::MockExpectationError)
|
281
|
-
end
|
282
|
-
|
283
|
-
it "passes if no method is called" do
|
284
|
-
lambda { klass.any_instance.should_not_receive(:existing_method) }.should_not raise_error
|
285
|
-
end
|
286
|
-
|
287
|
-
it "passes if only a different method is called" do
|
288
|
-
klass.any_instance.should_not_receive(:existing_method)
|
289
|
-
lambda { klass.new.another_existing_method }.should_not raise_error
|
290
|
-
end
|
291
|
-
|
292
|
-
context "with constraints" do
|
293
|
-
it "fails if the method is called with the specified parameters" do
|
294
|
-
klass.any_instance.should_not_receive(:existing_method_with_arguments).with(:argument_one, :argument_two)
|
295
|
-
lambda do
|
296
|
-
klass.new.existing_method_with_arguments(:argument_one, :argument_two)
|
297
|
-
end.should raise_error(RSpec::Mocks::MockExpectationError)
|
298
|
-
end
|
299
|
-
|
300
|
-
it "passes if the method is called with different parameters" do
|
301
|
-
klass.any_instance.should_not_receive(:existing_method_with_arguments).with(:argument_one, :argument_two)
|
302
|
-
lambda { klass.new.existing_method_with_arguments(:argument_three, :argument_four) }.should_not raise_error
|
303
|
-
end
|
304
|
-
end
|
305
|
-
end
|
306
|
-
|
307
|
-
context "with #should_receive" do
|
308
|
-
let(:foo_expectation_error_message) { 'Exactly one instance should have received the following message(s) but didn\'t: foo' }
|
309
|
-
let(:existing_method_expectation_error_message) { 'Exactly one instance should have received the following message(s) but didn\'t: existing_method' }
|
310
|
-
|
311
|
-
context "with an expectation is set on a method which does not exist" do
|
312
|
-
it "returns the expected value" do
|
313
|
-
klass.any_instance.should_receive(:foo).and_return(1)
|
314
|
-
klass.new.foo(1).should eq(1)
|
315
|
-
end
|
316
|
-
|
317
|
-
it "fails if an instance is created but no invocation occurs" do
|
318
|
-
expect do
|
319
|
-
klass.any_instance.should_receive(:foo)
|
320
|
-
klass.new
|
321
|
-
klass.rspec_verify
|
322
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError, foo_expectation_error_message)
|
323
|
-
end
|
324
|
-
|
325
|
-
it "fails if no instance is created" do
|
326
|
-
expect do
|
327
|
-
klass.any_instance.should_receive(:foo).and_return(1)
|
328
|
-
klass.rspec_verify
|
329
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError, foo_expectation_error_message)
|
330
|
-
end
|
331
|
-
|
332
|
-
it "fails if no instance is created and there are multiple expectations" do
|
333
|
-
expect do
|
334
|
-
klass.any_instance.should_receive(:foo)
|
335
|
-
klass.any_instance.should_receive(:bar)
|
336
|
-
klass.rspec_verify
|
337
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError, 'Exactly one instance should have received the following message(s) but didn\'t: bar, foo')
|
338
|
-
end
|
339
|
-
|
340
|
-
it "allows expectations on instances to take priority" do
|
341
|
-
klass.any_instance.should_receive(:foo)
|
342
|
-
klass.new.foo
|
343
|
-
|
344
|
-
instance = klass.new
|
345
|
-
instance.should_receive(:foo).and_return(result = Object.new)
|
346
|
-
instance.foo.should eq(result)
|
347
|
-
end
|
348
|
-
|
349
|
-
context "behaves as 'exactly one instance'" do
|
350
|
-
it "passes if subsequent invocations do not receive that message" do
|
351
|
-
klass.any_instance.should_receive(:foo)
|
352
|
-
klass.new.foo
|
353
|
-
klass.new
|
354
|
-
end
|
355
|
-
|
356
|
-
it "fails if the method is invoked on a second instance" do
|
357
|
-
instance_one = klass.new
|
358
|
-
instance_two = klass.new
|
359
|
-
expect do
|
360
|
-
klass.any_instance.should_receive(:foo)
|
361
|
-
|
362
|
-
instance_one.foo
|
363
|
-
instance_two.foo
|
364
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError, "The message 'foo' was received by #{instance_two.inspect} but has already been received by #{instance_one.inspect}")
|
365
|
-
end
|
366
|
-
end
|
367
|
-
|
368
|
-
context "normal expectations on the class object" do
|
369
|
-
it "fail when unfulfilled" do
|
370
|
-
expect do
|
371
|
-
klass.any_instance.should_receive(:foo)
|
372
|
-
klass.should_receive(:woot)
|
373
|
-
klass.new.foo
|
374
|
-
klass.rspec_verify
|
375
|
-
end.to(raise_error(RSpec::Mocks::MockExpectationError) do |error|
|
376
|
-
error.message.should_not eq(existing_method_expectation_error_message)
|
377
|
-
end)
|
378
|
-
end
|
379
|
-
|
380
|
-
|
381
|
-
it "pass when expectations are met" do
|
382
|
-
klass.any_instance.should_receive(:foo)
|
383
|
-
klass.should_receive(:woot).and_return(result = Object.new)
|
384
|
-
klass.new.foo
|
385
|
-
klass.woot.should eq(result)
|
386
|
-
end
|
387
|
-
end
|
388
|
-
end
|
389
|
-
|
390
|
-
context "with an expectation is set on a method that exists" do
|
391
|
-
it "returns the expected value" do
|
392
|
-
klass.any_instance.should_receive(:existing_method).and_return(1)
|
393
|
-
klass.new.existing_method(1).should eq(1)
|
394
|
-
end
|
395
|
-
|
396
|
-
it "fails if an instance is created but no invocation occurs" do
|
397
|
-
expect do
|
398
|
-
klass.any_instance.should_receive(:existing_method)
|
399
|
-
klass.new
|
400
|
-
klass.rspec_verify
|
401
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError, existing_method_expectation_error_message)
|
402
|
-
end
|
403
|
-
|
404
|
-
it "fails if no instance is created" do
|
405
|
-
expect do
|
406
|
-
klass.any_instance.should_receive(:existing_method)
|
407
|
-
klass.rspec_verify
|
408
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError, existing_method_expectation_error_message)
|
409
|
-
end
|
410
|
-
|
411
|
-
it "fails if no instance is created and there are multiple expectations" do
|
412
|
-
expect do
|
413
|
-
klass.any_instance.should_receive(:existing_method)
|
414
|
-
klass.any_instance.should_receive(:another_existing_method)
|
415
|
-
klass.rspec_verify
|
416
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError, 'Exactly one instance should have received the following message(s) but didn\'t: another_existing_method, existing_method')
|
417
|
-
end
|
418
|
-
|
419
|
-
context "after any one instance has received a message" do
|
420
|
-
it "passes if subsequent invocations do not receive that message" do
|
421
|
-
klass.any_instance.should_receive(:existing_method)
|
422
|
-
klass.new.existing_method
|
423
|
-
klass.new
|
424
|
-
end
|
425
|
-
|
426
|
-
it "fails if the method is invoked on a second instance" do
|
427
|
-
instance_one = klass.new
|
428
|
-
instance_two = klass.new
|
429
|
-
expect do
|
430
|
-
klass.any_instance.should_receive(:existing_method)
|
431
|
-
|
432
|
-
instance_one.existing_method
|
433
|
-
instance_two.existing_method
|
434
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError, "The message 'existing_method' was received by #{instance_two.inspect} but has already been received by #{instance_one.inspect}")
|
435
|
-
end
|
436
|
-
end
|
437
|
-
end
|
438
|
-
|
439
|
-
context "with argument matching" do
|
440
|
-
before do
|
441
|
-
klass.any_instance.should_receive(:foo).with(:param_one, :param_two).and_return(:result_one)
|
442
|
-
klass.any_instance.should_receive(:foo).with(:param_three, :param_four).and_return(:result_two)
|
443
|
-
end
|
444
|
-
|
445
|
-
it "returns the expected value when arguments match" do
|
446
|
-
instance = klass.new
|
447
|
-
instance.foo(:param_one, :param_two).should eq(:result_one)
|
448
|
-
instance.foo(:param_three, :param_four).should eq(:result_two)
|
449
|
-
end
|
450
|
-
|
451
|
-
it "fails when the arguments match but different instances are used" do
|
452
|
-
instances = Array.new(2) { klass.new }
|
453
|
-
expect do
|
454
|
-
instances[0].foo(:param_one, :param_two).should eq(:result_one)
|
455
|
-
instances[1].foo(:param_three, :param_four).should eq(:result_two)
|
456
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError)
|
457
|
-
|
458
|
-
# ignore the fact that should_receive expectations were not met
|
459
|
-
instances.each { |instance| instance.rspec_reset }
|
460
|
-
end
|
461
|
-
|
462
|
-
it "is not affected by the invocation of existing methods on other instances" do
|
463
|
-
klass.new.existing_method_with_arguments(:param_one, :param_two).should eq(:existing_method_with_arguments_return_value)
|
464
|
-
instance = klass.new
|
465
|
-
instance.foo(:param_one, :param_two).should eq(:result_one)
|
466
|
-
instance.foo(:param_three, :param_four).should eq(:result_two)
|
467
|
-
end
|
468
|
-
|
469
|
-
it "fails when arguments do not match" do
|
470
|
-
instance = klass.new
|
471
|
-
expect do
|
472
|
-
instance.foo(:param_one, :param_three)
|
473
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError)
|
474
|
-
|
475
|
-
# ignore the fact that should_receive expectations were not met
|
476
|
-
instance.rspec_reset
|
477
|
-
end
|
478
|
-
end
|
479
|
-
|
480
|
-
context "message count" do
|
481
|
-
context "the 'once' constraint" do
|
482
|
-
it "passes for one invocation" do
|
483
|
-
klass.any_instance.should_receive(:foo).once
|
484
|
-
klass.new.foo
|
485
|
-
end
|
486
|
-
|
487
|
-
it "fails when no instances are declared" do
|
488
|
-
expect do
|
489
|
-
klass.any_instance.should_receive(:foo).once
|
490
|
-
klass.rspec_verify
|
491
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError, foo_expectation_error_message)
|
492
|
-
end
|
493
|
-
|
494
|
-
it "fails when an instance is declared but there are no invocations" do
|
495
|
-
expect do
|
496
|
-
klass.any_instance.should_receive(:foo).once
|
497
|
-
klass.new
|
498
|
-
klass.rspec_verify
|
499
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError, foo_expectation_error_message)
|
500
|
-
end
|
501
|
-
|
502
|
-
it "fails for more than one invocation" do
|
503
|
-
expect do
|
504
|
-
klass.any_instance.should_receive(:foo).once
|
505
|
-
instance = klass.new
|
506
|
-
2.times { instance.foo }
|
507
|
-
instance.rspec_verify
|
508
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError)
|
509
|
-
end
|
510
|
-
end
|
511
|
-
|
512
|
-
context "the 'twice' constraint" do
|
513
|
-
it "passes for two invocations" do
|
514
|
-
klass.any_instance.should_receive(:foo).twice
|
515
|
-
instance = klass.new
|
516
|
-
2.times { instance.foo }
|
517
|
-
end
|
518
|
-
|
519
|
-
it "fails for more than two invocations" do
|
520
|
-
expect do
|
521
|
-
klass.any_instance.should_receive(:foo).twice
|
522
|
-
instance = klass.new
|
523
|
-
3.times { instance.foo }
|
524
|
-
instance.rspec_verify
|
525
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError)
|
526
|
-
end
|
527
|
-
end
|
528
|
-
|
529
|
-
context "the 'exactly(n)' constraint" do
|
530
|
-
it "passes for n invocations where n = 3" do
|
531
|
-
klass.any_instance.should_receive(:foo).exactly(3).times
|
532
|
-
instance = klass.new
|
533
|
-
3.times { instance.foo }
|
534
|
-
end
|
535
|
-
|
536
|
-
it "fails for n invocations where n < 3" do
|
537
|
-
expect do
|
538
|
-
klass.any_instance.should_receive(:foo).exactly(3).times
|
539
|
-
instance = klass.new
|
540
|
-
2.times { instance.foo }
|
541
|
-
instance.rspec_verify
|
542
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError)
|
543
|
-
end
|
544
|
-
|
545
|
-
it "fails for n invocations where n > 3" do
|
546
|
-
expect do
|
547
|
-
klass.any_instance.should_receive(:foo).exactly(3).times
|
548
|
-
instance = klass.new
|
549
|
-
4.times { instance.foo }
|
550
|
-
instance.rspec_verify
|
551
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError)
|
552
|
-
end
|
553
|
-
end
|
554
|
-
|
555
|
-
context "the 'at_least(n)' constraint" do
|
556
|
-
it "passes for n invocations where n = 3" do
|
557
|
-
klass.any_instance.should_receive(:foo).at_least(3).times
|
558
|
-
instance = klass.new
|
559
|
-
3.times { instance.foo }
|
560
|
-
end
|
561
|
-
|
562
|
-
it "fails for n invocations where n < 3" do
|
563
|
-
expect do
|
564
|
-
klass.any_instance.should_receive(:foo).at_least(3).times
|
565
|
-
instance = klass.new
|
566
|
-
2.times { instance.foo }
|
567
|
-
instance.rspec_verify
|
568
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError)
|
569
|
-
end
|
570
|
-
|
571
|
-
it "passes for n invocations where n > 3" do
|
572
|
-
klass.any_instance.should_receive(:foo).at_least(3).times
|
573
|
-
instance = klass.new
|
574
|
-
4.times { instance.foo }
|
575
|
-
end
|
576
|
-
end
|
577
|
-
|
578
|
-
context "the 'at_most(n)' constraint" do
|
579
|
-
it "passes for n invocations where n = 3" do
|
580
|
-
klass.any_instance.should_receive(:foo).at_most(3).times
|
581
|
-
instance = klass.new
|
582
|
-
3.times { instance.foo }
|
583
|
-
end
|
584
|
-
|
585
|
-
it "passes for n invocations where n < 3" do
|
586
|
-
klass.any_instance.should_receive(:foo).at_most(3).times
|
587
|
-
instance = klass.new
|
588
|
-
2.times { instance.foo }
|
589
|
-
end
|
590
|
-
|
591
|
-
it "fails for n invocations where n > 3" do
|
592
|
-
expect do
|
593
|
-
klass.any_instance.should_receive(:foo).at_most(3).times
|
594
|
-
instance = klass.new
|
595
|
-
4.times { instance.foo }
|
596
|
-
instance.rspec_verify
|
597
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError)
|
598
|
-
end
|
599
|
-
end
|
600
|
-
|
601
|
-
context "the 'never' constraint" do
|
602
|
-
it "passes for 0 invocations" do
|
603
|
-
klass.any_instance.should_receive(:foo).never
|
604
|
-
klass.rspec_verify
|
605
|
-
end
|
606
|
-
|
607
|
-
it "fails on the first invocation" do
|
608
|
-
expect do
|
609
|
-
klass.any_instance.should_receive(:foo).never
|
610
|
-
klass.new.foo
|
611
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError)
|
612
|
-
end
|
613
|
-
|
614
|
-
context "when combined with other expectations" do
|
615
|
-
it "passes when the other expecations are met" do
|
616
|
-
klass.any_instance.should_receive(:foo).never
|
617
|
-
klass.any_instance.should_receive(:existing_method).and_return(5)
|
618
|
-
klass.new.existing_method.should eq(5)
|
619
|
-
end
|
620
|
-
|
621
|
-
it "fails when the other expecations are not met" do
|
622
|
-
expect do
|
623
|
-
klass.any_instance.should_receive(:foo).never
|
624
|
-
klass.any_instance.should_receive(:existing_method).and_return(5)
|
625
|
-
klass.rspec_verify
|
626
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError, existing_method_expectation_error_message)
|
627
|
-
end
|
628
|
-
end
|
629
|
-
end
|
630
|
-
|
631
|
-
context "the 'any_number_of_times' constraint" do
|
632
|
-
it "passes for 0 invocations" do
|
633
|
-
klass.any_instance.should_receive(:foo).any_number_of_times
|
634
|
-
klass.new.rspec_verify
|
635
|
-
end
|
636
|
-
|
637
|
-
it "passes for a non-zero number of invocations" do
|
638
|
-
klass.any_instance.should_receive(:foo).any_number_of_times
|
639
|
-
instance = klass.new
|
640
|
-
instance.foo
|
641
|
-
instance.rspec_verify
|
642
|
-
end
|
643
|
-
|
644
|
-
it "does not interfere with other expectations" do
|
645
|
-
klass.any_instance.should_receive(:foo).any_number_of_times
|
646
|
-
klass.any_instance.should_receive(:existing_method).and_return(5)
|
647
|
-
klass.new.existing_method.should eq(5)
|
648
|
-
end
|
649
|
-
|
650
|
-
context "when combined with other expectations" do
|
651
|
-
it "passes when the other expecations are met" do
|
652
|
-
klass.any_instance.should_receive(:foo).any_number_of_times
|
653
|
-
klass.any_instance.should_receive(:existing_method).and_return(5)
|
654
|
-
klass.new.existing_method.should eq(5)
|
655
|
-
end
|
656
|
-
|
657
|
-
it "fails when the other expecations are not met" do
|
658
|
-
expect do
|
659
|
-
klass.any_instance.should_receive(:foo).any_number_of_times
|
660
|
-
klass.any_instance.should_receive(:existing_method).and_return(5)
|
661
|
-
klass.rspec_verify
|
662
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError, existing_method_expectation_error_message)
|
663
|
-
end
|
664
|
-
end
|
665
|
-
end
|
666
|
-
end
|
667
|
-
end
|
668
|
-
|
669
|
-
context "when resetting post-verification" do
|
670
|
-
let(:space) { RSpec::Mocks::Space.new }
|
671
|
-
|
672
|
-
context "existing method" do
|
673
|
-
before(:each) do
|
674
|
-
space.add(klass)
|
675
|
-
end
|
676
|
-
|
677
|
-
context "with stubbing" do
|
678
|
-
context "public methods" do
|
679
|
-
before(:each) do
|
680
|
-
klass.any_instance.stub(:existing_method).and_return(1)
|
681
|
-
klass.method_defined?(:__existing_method_without_any_instance__).should be_true
|
682
|
-
end
|
683
|
-
|
684
|
-
it "restores the class to its original state after each example when no instance is created" do
|
685
|
-
space.verify_all
|
686
|
-
|
687
|
-
klass.method_defined?(:__existing_method_without_any_instance__).should be_false
|
688
|
-
klass.new.existing_method.should eq(existing_method_return_value)
|
689
|
-
end
|
690
|
-
|
691
|
-
it "restores the class to its original state after each example when one instance is created" do
|
692
|
-
klass.new.existing_method
|
693
|
-
|
694
|
-
space.verify_all
|
695
|
-
|
696
|
-
klass.method_defined?(:__existing_method_without_any_instance__).should be_false
|
697
|
-
klass.new.existing_method.should eq(existing_method_return_value)
|
698
|
-
end
|
699
|
-
|
700
|
-
it "restores the class to its original state after each example when more than one instance is created" do
|
701
|
-
klass.new.existing_method
|
702
|
-
klass.new.existing_method
|
703
|
-
|
704
|
-
space.verify_all
|
705
|
-
|
706
|
-
klass.method_defined?(:__existing_method_without_any_instance__).should be_false
|
707
|
-
klass.new.existing_method.should eq(existing_method_return_value)
|
708
|
-
end
|
709
|
-
end
|
710
|
-
|
711
|
-
context "private methods" do
|
712
|
-
before :each do
|
713
|
-
klass.any_instance.stub(:private_method).and_return(:something)
|
714
|
-
space.verify_all
|
715
|
-
end
|
716
|
-
|
717
|
-
it "cleans up the backed up method" do
|
718
|
-
klass.method_defined?(:__existing_method_without_any_instance__).should be_false
|
719
|
-
end
|
720
|
-
|
721
|
-
it "restores a stubbed private method after the spec is run" do
|
722
|
-
klass.private_method_defined?(:private_method).should be_true
|
723
|
-
end
|
724
|
-
|
725
|
-
it "ensures that the restored method behaves as it originally did" do
|
726
|
-
klass.new.send(:private_method).should eq(:private_method_return_value)
|
727
|
-
end
|
728
|
-
end
|
729
|
-
end
|
730
|
-
|
731
|
-
context "with expectations" do
|
732
|
-
context "private methods" do
|
733
|
-
before :each do
|
734
|
-
klass.any_instance.should_receive(:private_method).and_return(:something)
|
735
|
-
klass.new.private_method
|
736
|
-
space.verify_all
|
737
|
-
end
|
738
|
-
|
739
|
-
it "cleans up the backed up method" do
|
740
|
-
klass.method_defined?(:__existing_method_without_any_instance__).should be_false
|
741
|
-
end
|
742
|
-
|
743
|
-
it "restores a stubbed private method after the spec is run" do
|
744
|
-
klass.private_method_defined?(:private_method).should be_true
|
745
|
-
end
|
746
|
-
|
747
|
-
it "ensures that the restored method behaves as it originally did" do
|
748
|
-
klass.new.send(:private_method).should eq(:private_method_return_value)
|
749
|
-
end
|
750
|
-
end
|
751
|
-
|
752
|
-
context "ensures that the subsequent specs do not see expectations set in previous specs" do
|
753
|
-
context "when the instance created after the expectation is set" do
|
754
|
-
it "first spec" do
|
755
|
-
klass.any_instance.should_receive(:existing_method).and_return(Object.new)
|
756
|
-
klass.new.existing_method
|
757
|
-
end
|
758
|
-
|
759
|
-
it "second spec" do
|
760
|
-
klass.new.existing_method.should eq(existing_method_return_value)
|
761
|
-
end
|
762
|
-
end
|
763
|
-
|
764
|
-
context "when the instance created before the expectation is set" do
|
765
|
-
before :each do
|
766
|
-
@instance = klass.new
|
767
|
-
end
|
768
|
-
|
769
|
-
it "first spec" do
|
770
|
-
klass.any_instance.should_receive(:existing_method).and_return(Object.new)
|
771
|
-
@instance.existing_method
|
772
|
-
end
|
773
|
-
|
774
|
-
it "second spec" do
|
775
|
-
@instance.existing_method.should eq(existing_method_return_value)
|
776
|
-
end
|
777
|
-
end
|
778
|
-
end
|
779
|
-
|
780
|
-
it "ensures that the next spec does not see that expectation" do
|
781
|
-
klass.any_instance.should_receive(:existing_method).and_return(Object.new)
|
782
|
-
klass.new.existing_method
|
783
|
-
space.verify_all
|
784
|
-
|
785
|
-
klass.new.existing_method.should eq(existing_method_return_value)
|
786
|
-
end
|
787
|
-
end
|
788
|
-
end
|
789
|
-
|
790
|
-
context "with multiple calls to any_instance in the same example" do
|
791
|
-
it "does not prevent the change from being rolled back" do
|
792
|
-
klass.any_instance.stub(:existing_method).and_return(false)
|
793
|
-
klass.any_instance.stub(:existing_method).and_return(true)
|
794
|
-
|
795
|
-
klass.rspec_verify
|
796
|
-
klass.new.should respond_to(:existing_method)
|
797
|
-
klass.new.existing_method.should eq(existing_method_return_value)
|
798
|
-
end
|
799
|
-
end
|
800
|
-
|
801
|
-
it "adds an class to the current space when #any_instance is invoked" do
|
802
|
-
klass.any_instance
|
803
|
-
RSpec::Mocks::space.send(:receivers).should include(klass)
|
804
|
-
end
|
805
|
-
|
806
|
-
it "adds an instance to the current space when stubbed method is invoked" do
|
807
|
-
klass.any_instance.stub(:foo)
|
808
|
-
instance = klass.new
|
809
|
-
instance.foo
|
810
|
-
RSpec::Mocks::space.send(:receivers).should include(instance)
|
811
|
-
end
|
812
|
-
end
|
813
|
-
|
814
|
-
context 'when used in conjunction with a `dup`' do
|
815
|
-
it "doesn't cause an infinite loop" do
|
816
|
-
Object.any_instance.stub(:some_method)
|
817
|
-
o = Object.new
|
818
|
-
o.some_method
|
819
|
-
lambda { o.dup.some_method }.should_not raise_error(SystemStackError)
|
820
|
-
end
|
821
|
-
|
822
|
-
it "doesn't bomb if the object doesn't support `dup`" do
|
823
|
-
klass = Class.new do
|
824
|
-
undef_method :dup
|
825
|
-
end
|
826
|
-
klass.any_instance
|
827
|
-
end
|
828
|
-
end
|
829
|
-
|
830
|
-
context "when directed at a method defined on a superclass" do
|
831
|
-
let(:sub_klass) { Class.new(klass) }
|
832
|
-
|
833
|
-
it "stubs the method correctly" do
|
834
|
-
klass.any_instance.stub(:existing_method).and_return("foo")
|
835
|
-
sub_klass.new.existing_method.should == "foo"
|
836
|
-
end
|
837
|
-
|
838
|
-
it "mocks the method correctly" do
|
839
|
-
instance_one = sub_klass.new
|
840
|
-
instance_two = sub_klass.new
|
841
|
-
expect do
|
842
|
-
klass.any_instance.should_receive(:existing_method)
|
843
|
-
instance_one.existing_method
|
844
|
-
instance_two.existing_method
|
845
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError, "The message 'existing_method' was received by #{instance_two.inspect} but has already been received by #{instance_one.inspect}")
|
846
|
-
end
|
847
|
-
end
|
848
|
-
|
849
|
-
context "when a class overrides Object#method" do
|
850
|
-
let(:http_request_class) { Struct.new(:method, :uri) }
|
851
|
-
|
852
|
-
it "stubs the method correctly" do
|
853
|
-
http_request_class.any_instance.stub(:existing_method).and_return("foo")
|
854
|
-
http_request_class.new.existing_method.should == "foo"
|
855
|
-
end
|
856
|
-
|
857
|
-
it "mocks the method correctly" do
|
858
|
-
http_request_class.any_instance.should_receive(:existing_method).and_return("foo")
|
859
|
-
http_request_class.new.existing_method.should == "foo"
|
860
|
-
end
|
861
|
-
end
|
862
|
-
|
863
|
-
context "when used after the test has finished" do
|
864
|
-
it "restores the original behavior of a stubbed method" do
|
865
|
-
klass.any_instance.stub(:existing_method).and_return(:stubbed_return_value)
|
866
|
-
|
867
|
-
instance = klass.new
|
868
|
-
instance.existing_method.should == :stubbed_return_value
|
869
|
-
|
870
|
-
RSpec::Mocks.verify
|
871
|
-
|
872
|
-
instance.existing_method.should == :existing_method_return_value
|
873
|
-
end
|
874
|
-
end
|
875
|
-
end
|
876
|
-
end
|
877
|
-
end
|