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