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.
Files changed (36) hide show
  1. data/Changelog.md +36 -1
  2. data/README.md +14 -2
  3. data/features/stubbing_constants/README.md +62 -0
  4. data/features/stubbing_constants/stub_defined_constant.feature +79 -0
  5. data/features/stubbing_constants/stub_undefined_constant.feature +50 -0
  6. data/lib/rspec/mocks/any_instance/chain.rb +0 -81
  7. data/lib/rspec/mocks/any_instance/expectation_chain.rb +57 -0
  8. data/lib/rspec/mocks/any_instance/recorder.rb +6 -1
  9. data/lib/rspec/mocks/any_instance/stub_chain.rb +37 -0
  10. data/lib/rspec/mocks/any_instance/stub_chain_chain.rb +25 -0
  11. data/lib/rspec/mocks/any_instance.rb +37 -1
  12. data/lib/rspec/mocks/argument_list_matcher.rb +93 -0
  13. data/lib/rspec/mocks/argument_matchers.rb +39 -31
  14. data/lib/rspec/mocks/error_generator.rb +7 -0
  15. data/lib/rspec/mocks/example_methods.rb +41 -0
  16. data/lib/rspec/mocks/framework.rb +2 -1
  17. data/lib/rspec/mocks/message_expectation.rb +27 -15
  18. data/lib/rspec/mocks/methods.rb +4 -0
  19. data/lib/rspec/mocks/proxy.rb +10 -4
  20. data/lib/rspec/mocks/stub_const.rb +280 -0
  21. data/lib/rspec/mocks/test_double.rb +3 -2
  22. data/lib/rspec/mocks/version.rb +1 -1
  23. data/spec/rspec/mocks/any_instance/message_chains_spec.rb +1 -1
  24. data/spec/rspec/mocks/any_instance_spec.rb +66 -26
  25. data/spec/rspec/mocks/argument_expectation_spec.rb +7 -7
  26. data/spec/rspec/mocks/at_least_spec.rb +89 -50
  27. data/spec/rspec/mocks/block_return_value_spec.rb +8 -0
  28. data/spec/rspec/mocks/mock_spec.rb +261 -246
  29. data/spec/rspec/mocks/multiple_return_value_spec.rb +2 -2
  30. data/spec/rspec/mocks/null_object_mock_spec.rb +22 -0
  31. data/spec/rspec/mocks/stub_chain_spec.rb +47 -47
  32. data/spec/rspec/mocks/stub_const_spec.rb +309 -0
  33. data/spec/rspec/mocks/stub_spec.rb +2 -2
  34. data/spec/spec_helper.rb +3 -41
  35. metadata +18 -6
  36. 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) do
7
- @mock = double("test double")
8
- end
6
+ before(:each) { @double = double("test double") }
7
+ after(:each) { @double.rspec_reset }
9
8
 
10
- treats_method_missing_as_private :subject => RSpec::Mocks::Mock.new, :noop => false
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
- after(:each) do
13
- @mock.rspec_reset
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__; @mock.should_receive(:wont_happen).with("x", 3)
18
+ expected_error_line = __LINE__; @double.should_receive(:wont_happen).with("x", 3)
18
19
  begin
19
- @mock.rspec_verify
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
- @mock.stub(:wont_happen)
29
- expected_error_line = __LINE__; @mock.should_receive(:wont_happen).with("x", 3)
29
+ @double.stub(:wont_happen)
30
+ expected_error_line = __LINE__; @double.should_receive(:wont_happen).with("x", 3)
30
31
  begin
31
- @mock.rspec_verify
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
- @mock.should_not_receive(:not_expected)
41
- @mock.rspec_verify
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
- @mock.should_not_receive(:message).with("unwanted text")
46
- @mock.should_receive(:message).with("other text")
47
- @mock.message "other text"
48
- @mock.rspec_verify
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
- @mock.should_not_receive(:not_expected)
60
+ @double.should_not_receive(:not_expected)
53
61
  expect {
54
- @mock.not_expected
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
- @mock.should_not_receive(:not_expected).with("unexpected text")
71
+ @double.should_not_receive(:not_expected).with("unexpected text")
64
72
  expect {
65
- @mock.not_expected("unexpected text")
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
- @mock.should_not_receive(:not_expected).with("unexpected text")
75
- @mock.not_expected "really unexpected text"
76
- @mock.rspec_verify
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
- @mock.should_receive(:something).with("a","b","c").and_return { |a,b,c| c+b+a }
81
- @mock.something("a","b","c").should eq "cba"
82
- @mock.rspec_verify
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
- @mock.should_receive(:something).with("a","b","c").and_return("booh")
87
- @mock.something("a","b","c").should eq "booh"
88
- @mock.rspec_verify
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
- @mock.stub(:something).with("a","b","c").and_return(:stubbed_value)
93
- @mock.should_receive(:something).with("a","b","c")
94
- @mock.something("a","b","c").should eq :stubbed_value
95
- @mock.rspec_verify
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
- @mock.should_receive(:something).with("a","b","c")
100
- @mock.something("a","b","c").should be_nil
101
- @mock.rspec_verify
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
- @mock.should_receive(:something).with("a","b","c").and_return("booh")
113
+ @double.should_receive(:something).with("a","b","c").and_return("booh")
106
114
  lambda {
107
- @mock.something("a","d","c")
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
- @mock.should_receive(:something).with("a","b","c").once
115
- @mock.should_receive(:something).with("z","x","c").once
122
+ @double.should_receive(:something).with("a","b","c").once
123
+ @double.should_receive(:something).with("z","x","c").once
116
124
  lambda {
117
- @mock.something("a","b","c")
118
- @mock.something("z","x","g")
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
- @mock.stub(:something)
125
- @mock.should_receive(:something).with("a","b","c")
132
+ @double.stub(:something)
133
+ @double.should_receive(:something).with("a","b","c")
126
134
  lambda {
127
- @mock.something("a","d","c")
128
- @mock.rspec_verify
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
- @mock = double("test double").as_null_object
134
- @mock.should_receive(:something).with("a","b","c")
141
+ @double = double("test double").as_null_object
142
+ @double.should_receive(:something).with("a","b","c")
135
143
  lambda {
136
- @mock.something("a","d","c")
137
- @mock.rspec_verify
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 @mock.method_with_default_argument(arg={}); end
144
- @mock.should_receive(:method_with_default_argument).with({})
151
+ def @double.method_with_default_argument(arg={}); end
152
+ @double.should_receive(:method_with_default_argument).with({})
145
153
 
146
154
  expect {
147
- @mock.method_with_default_argument(nil)
148
- @mock.rspec_verify
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
- @mock.something("a","b","c")
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
- @mock.should_receive(:something) do | a, b |
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
- @mock.something("a", "b").should eq "booh"
167
- @mock.rspec_verify
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
- @mock.should_receive(:something) {| bool | bool.should be_true}
179
+ @double.should_receive(:something) {| bool | bool.should be_true}
172
180
  expect {
173
- @mock.something false
181
+ @double.something false
174
182
  }.to raise_error(RSpec::Expectations::ExpectationNotMetError)
175
183
  end
176
184
 
177
- it "passes proc to expectation block without an argument", :ruby => '> 1.8.6' do
178
- # We eval this because Ruby 1.8.6's syntax parser barfs on { |&block| ... }
179
- # and prevents the entire spec suite from running.
180
- eval("@mock.should_receive(:foo) {|&block| block.call.should eq(:bar)}")
181
- @mock.foo { :bar }
182
- end
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
- it "passes proc to expectation block with an argument", :ruby => '> 1.8.6' do
185
- eval("@mock.should_receive(:foo) {|arg, &block| block.call.should eq(:bar)}")
186
- @mock.foo(:arg) { :bar }
187
- end
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
- it "passes proc to stub block without an argurment", :ruby => '>1.8.6' do
190
- eval("@mock.stub(:foo) {|&block| block.call.should eq(:bar)}")
191
- @mock.foo { :bar }
192
- end
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
- it "passes proc to stub block with an argument", :ruby => '> 1.8.6' do
195
- eval("@mock.stub(:foo) {|arg, &block| block.call.should eq(:bar)}")
196
- @mock.foo(:arg) { :bar }
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
- @mock.should_receive(:not_expected).never
201
- expect { @mock.not_expected }.
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
- @mock.should_receive(:something).and_raise(RuntimeError)
209
- expect { @mock.something }.to raise_error(RuntimeError)
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
- @mock.should_receive(:something).and_raise(error)
229
+ @double.should_receive(:something).and_raise(error)
215
230
  lambda {
216
- @mock.something
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
- @mock.should_receive(:something).and_raise(error)
237
+ @double.should_receive(:something).and_raise(error)
223
238
  lambda {
224
- @mock.something
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
- @mock.stub(:something).and_raise(ErrorWithNonZeroArgConstructor)
249
+ @double.stub(:something).and_raise(ErrorWithNonZeroArgConstructor)
235
250
  lambda {
236
- @mock.something
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
- @mock.should_receive(:something).and_raise("error message")
256
+ @double.should_receive(:something).and_raise("error message")
242
257
  lambda {
243
- @mock.something
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
- @mock.should_receive(:something).with(2).and_raise(RuntimeError)
263
+ @double.should_receive(:something).with(2).and_raise(RuntimeError)
249
264
  lambda {
250
- @mock.something 1
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
- @mock.should_receive(:something).and_throw(:blech)
270
+ @double.should_receive(:something).and_throw(:blech)
256
271
  lambda {
257
- @mock.something
272
+ @double.something
258
273
  }.should throw_symbol(:blech)
259
274
  end
260
275
 
261
276
  it "ignores args on any args" do
262
- @mock.should_receive(:something).at_least(:once).with(any_args)
263
- @mock.something
264
- @mock.something 1
265
- @mock.something "a", 2
266
- @mock.something [], {}, "joe", 7
267
- @mock.rspec_verify
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
- @mock.should_receive(:something).with(no_args())
286
+ @double.should_receive(:something).with(no_args())
272
287
  lambda {
273
- @mock.something 1
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
- @mock.should_receive(:something).with(1)
293
+ @double.should_receive(:something).with(1)
279
294
  lambda {
280
- @mock.something
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
- @mock.stub(:method_that_yields).and_yield
286
- @mock.method_that_yields { :returned_obj }.should eq :returned_obj
287
- @mock.rspec_verify
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
- @mock.should_receive(:yield_back).with(no_args()).once.and_yield
306
+ @double.should_receive(:yield_back).with(no_args()).once.and_yield
292
307
  a = nil
293
- @mock.yield_back {|*x| a = x}
308
+ @double.yield_back {|*x| a = x}
294
309
  a.should eq []
295
- @mock.rspec_verify
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
- @mock.should_receive(:yield_back).once.with(no_args()).once.and_yield.
314
+ @double.should_receive(:yield_back).once.with(no_args()).once.and_yield.
300
315
  and_yield
301
316
  b = []
302
- @mock.yield_back {|*a| b << a}
317
+ @double.yield_back {|*a| b << a}
303
318
  b.should eq [ [], [] ]
304
- @mock.rspec_verify
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
- @mock.should_receive(:yield_back).with(no_args()).once.and_yield(99)
323
+ @double.should_receive(:yield_back).with(no_args()).once.and_yield(99)
309
324
  a = nil
310
- @mock.yield_back {|*x| a = x}
325
+ @double.yield_back {|*x| a = x}
311
326
  a.should eq [99]
312
- @mock.rspec_verify
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
- @mock.should_receive(:yield_back).once.with(no_args()).once.and_yield(99).
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
- @mock.yield_back {|*a| b << a}
335
+ @double.yield_back {|*a| b << a}
321
336
  b.should eq [[99], [43], ["something fruity"]]
322
- @mock.rspec_verify
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
- @mock.should_receive(:yield_back).with(no_args()).once.and_yield(99, 27, "go")
341
+ @double.should_receive(:yield_back).with(no_args()).once.and_yield(99, 27, "go")
327
342
  a = nil
328
- @mock.yield_back {|*x| a = x}
343
+ @double.yield_back {|*x| a = x}
329
344
  a.should eq [99, 27, "go"]
330
- @mock.rspec_verify
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
- @mock.should_receive(:yield_back).once.with(no_args()).once.and_yield(99, :green, "go").
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
- @mock.yield_back {|*a| b << a}
353
+ @double.yield_back {|*a| b << a}
339
354
  b.should eq [[99, :green, "go"], ["wait", :amber], ["stop", 12, :red]]
340
- @mock.rspec_verify
355
+ @double.rspec_verify
341
356
  end
342
357
 
343
358
  it "yields single value" do
344
- @mock.should_receive(:yield_back).with(no_args()).once.and_yield(99)
359
+ @double.should_receive(:yield_back).with(no_args()).once.and_yield(99)
345
360
  a = nil
346
- @mock.yield_back {|x| a = x}
361
+ @double.yield_back {|x| a = x}
347
362
  a.should eq 99
348
- @mock.rspec_verify
363
+ @double.rspec_verify
349
364
  end
350
365
 
351
366
  it "yields single value 3 times consecutively" do
352
- @mock.should_receive(:yield_back).once.with(no_args()).once.and_yield(99).
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
- @mock.yield_back {|a| b << a}
371
+ @double.yield_back {|a| b << a}
357
372
  b.should eq [99, 43, "something fruity"]
358
- @mock.rspec_verify
373
+ @double.rspec_verify
359
374
  end
360
375
 
361
376
  it "yields two values" do
362
- @mock.should_receive(:yield_back).with(no_args()).once.and_yield('wha', 'zup')
377
+ @double.should_receive(:yield_back).with(no_args()).once.and_yield('wha', 'zup')
363
378
  a, b = nil
364
- @mock.yield_back {|x,y| a=x; b=y}
379
+ @double.yield_back {|x,y| a=x; b=y}
365
380
  a.should eq 'wha'
366
381
  b.should eq 'zup'
367
- @mock.rspec_verify
382
+ @double.rspec_verify
368
383
  end
369
384
 
370
385
  it "yields two values 3 times consecutively" do
371
- @mock.should_receive(:yield_back).once.with(no_args()).once.and_yield('wha', 'zup').
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
- @mock.yield_back {|a,b| c << [a, b]}
390
+ @double.yield_back {|a,b| c << [a, b]}
376
391
  c.should eq [['wha', 'zup'], ['not', 'down'], [14, 65]]
377
- @mock.rspec_verify
392
+ @double.rspec_verify
378
393
  end
379
394
 
380
395
  it "fails when calling yielding method with wrong arity" do
381
- @mock.should_receive(:yield_back).with(no_args()).once.and_yield('wha', 'zup')
396
+ @double.should_receive(:yield_back).with(no_args()).once.and_yield('wha', 'zup')
382
397
  lambda {
383
- @mock.yield_back {|a|}
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
- @mock.should_receive(:yield_back).once.with(no_args()).once.and_yield('wha', 'zup').
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
- @mock.yield_back {|a,b| c << [a, b]}
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
- @mock.should_receive(:yield_back).with(no_args()).once.and_yield('wha', 'zup')
413
+ @double.should_receive(:yield_back).with(no_args()).once.and_yield('wha', 'zup')
399
414
  lambda {
400
- @mock.yield_back
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 mock send" do
405
- @mock.should_receive(:send).with(any_args)
406
- @mock.send 'hi'
407
- @mock.rspec_verify
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 mock" do
411
- @mock.should_receive(:yield_me).and_yield 44
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
- @mock.yield_me do |x|
429
+ @double.yield_me do |x|
415
430
  raise "Bang"
416
431
  end
417
432
  }.should raise_error(StandardError, "Bang")
418
433
 
419
- @mock.rspec_verify
434
+ @double.rspec_verify
420
435
  end
421
436
 
422
437
  it "clears expectations after verify" do
423
- @mock.should_receive(:foobar)
424
- @mock.foobar
425
- @mock.rspec_verify
438
+ @double.should_receive(:foobar)
439
+ @double.foobar
440
+ @double.rspec_verify
426
441
  lambda {
427
- @mock.foobar
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
- mock = double("this is a mock")
433
- mock.should_receive(:blah)
434
- mock.rspec_reset
435
- mock.rspec_verify #should throw if reset didn't work
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
- @mock.should_receive(:foobar)
460
- @mock.foobar
461
- @mock.rspec_verify
474
+ @double.should_receive(:foobar)
475
+ @double.foobar
476
+ @double.rspec_verify
462
477
 
463
- lambda { @mock.foobar }.should_not raise_error(NameError)
464
- lambda { @mock.foobar }.should raise_error(RSpec::Mocks::MockExpectationError)
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 mock" do
468
- @mock.stub(:msg).and_return(:stub_value)
469
- @mock.should_receive(:msg).with(:arg).and_return(:mock_value)
470
- @mock.msg(:arg).should equal(:mock_value)
471
- @mock.msg.should equal(:stub_value)
472
- @mock.msg.should equal(:stub_value)
473
- @mock.rspec_verify
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
- @mock.stub(:msg).and_return(:stub_value)
478
- @mock.should_receive(:msg).and_return(:mock_value)
479
- @mock.msg(:arg).should equal(:mock_value)
480
- @mock.msg.should equal(:stub_value)
481
- @mock.msg.should equal(:stub_value)
482
- @mock.rspec_verify
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
- @mock.stub(:msg).and_return(:stub_value)
487
- @mock.should_not_receive(:msg)
488
- expect { @mock.msg(:arg) }.to raise_error(RSpec::Mocks::MockExpectationError)
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-mock" do
492
- non_mock = Object.new
493
- non_mock.stub(:msg).and_return(:stub_value)
494
- non_mock.should_receive(:msg).with(:arg).and_return(:mock_value)
495
- non_mock.msg(:arg).should equal(:mock_value)
496
- non_mock.msg.should equal(:stub_value)
497
- non_mock.msg.should equal(:stub_value)
498
- non_mock.rspec_verify
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
- @mock.stub(:msg).and_return(:stub_value)
503
- @mock.should_receive(:msg)
504
- @mock.msg.should equal(:stub_value)
505
- @mock.rspec_verify
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
- @mock.stub(:msg).with(:arg).and_return(:stub_value)
510
- @mock.should_receive(:msg).with(:arg)
511
- @mock.msg(:arg).should equal(:stub_value)
512
- @mock.rspec_verify
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 mocked" do
516
- @mock.stub(:yield_back).and_yield(:stub_value)
517
- @mock.should_receive(:yield_back).and_yield(:mock_value)
518
- @mock.yield_back{|v| v.should eq :mock_value }
519
- @mock.yield_back{|v| v.should eq :stub_value }
520
- @mock.rspec_verify
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
- mock = RSpec::Mocks::Mock.new('name', :message => :response)
535
- mock.message.should eq :response
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 mock message receiving a block" do
555
+ describe "a double message receiving a block" do
541
556
  before(:each) do
542
- @mock = double("mock")
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
- @mock.should_receive(:foo) { add_call }
566
+ @double.should_receive(:foo) { add_call }
552
567
 
553
- @mock.foo
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
- @mock.stub(:foo).and_return(:bar)
560
- @mock.should_receive(:foo) { add_call }
574
+ @double.stub(:foo).and_return(:bar)
575
+ @double.should_receive(:foo) { add_call }
561
576
 
562
- @mock.foo
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
- @mock.should_receive(:foo).once { add_call }
583
+ @double.should_receive(:foo).once { add_call }
569
584
 
570
- @mock.foo
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
- @mock.should_receive(:foo).twice { add_call }
591
+ @double.should_receive(:foo).twice { add_call }
577
592
 
578
- @mock.foo
579
- @mock.foo
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
- @mock.should_receive(:foo).exactly(10).times { add_call }
600
+ @double.should_receive(:foo).exactly(10).times { add_call }
586
601
 
587
- (1..10).each { @mock.foo }
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
- @mock.should_receive(:foo).any_number_of_times { add_call }
608
+ @double.should_receive(:foo).any_number_of_times { add_call }
594
609
 
595
- (1..7).each { @mock.foo }
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
- @mock.should_receive(:foo).ordered { add_call }
602
- @mock.should_receive(:bar).ordered { add_call }
616
+ @double.should_receive(:foo).ordered { add_call }
617
+ @double.should_receive(:bar).ordered { add_call }
603
618
 
604
- @mock.foo
605
- @mock.bar
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
- mock = double("Dog")
614
- valid_html_str = "#{mock}"
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 "mock created with no name" do
641
+ describe "double created with no name" do
627
642
  it "does not use a name in a failure message" do
628
- mock = double()
629
- expect {mock.foo}.to raise_error(/Double received/)
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 { @mock = double('double') }
665
+ before { @double = double('double') }
651
666
  context "with args" do
652
667
  context "with matching args" do
653
668
  it "passes" do
654
- @mock.should_receive(:foo).with('bar')
655
- @mock.foo('bar')
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
- @mock.should_receive(:foo).with('bar')
676
+ @double.should_receive(:foo).with('bar')
662
677
  expect do
663
- @mock.foo('baz')
678
+ @double.foo('baz')
664
679
  end.to raise_error
665
- @mock.rspec_reset
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
- @mock.should_receive(:foo).with(d1)
688
+ @double.should_receive(:foo).with(d1)
674
689
  expect do
675
- @mock.foo(d2)
690
+ @double.foo(d2)
676
691
  end.to raise_error
677
- @mock.rspec_reset
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
- @mock.should_receive(:foo).with(d1)
700
+ @double.should_receive(:foo).with(d1)
686
701
  expect do
687
- @mock.foo(d2)
702
+ @double.foo(d2)
688
703
  end.to raise_error
689
- @mock.rspec_reset
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
- @mock.should_receive(:foo).with('bar') { 'baz' }
698
- @mock.foo('bar').should eq('baz')
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
- @mock.should_receive(:foo).with('bar') { 'baz' }
719
+ @double.should_receive(:foo).with('bar') { 'baz' }
705
720
  expect do
706
- @mock.foo('wrong').should eq('baz')
721
+ @double.foo('wrong').should eq('baz')
707
722
  end.to raise_error(/received :foo with unexpected arguments/)
708
- @mock.rspec_reset
723
+ @double.rspec_reset
709
724
  end
710
725
  end
711
726
  end