rspec 1.2.4 → 1.2.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. data.tar.gz.sig +0 -0
  2. data/History.rdoc +59 -32
  3. data/Manifest.txt +6 -0
  4. data/Rakefile +2 -2
  5. data/examples/passing/options_example.rb +31 -0
  6. data/examples/passing/options_formatter.rb +20 -0
  7. data/features/expectations/expect_change.feature +65 -0
  8. data/features/expectations/expect_error.feature +44 -0
  9. data/lib/spec/example/example_group_factory.rb +4 -2
  10. data/lib/spec/example/example_group_methods.rb +5 -1
  11. data/lib/spec/example/example_group_proxy.rb +8 -0
  12. data/lib/spec/example/example_matcher.rb +2 -3
  13. data/lib/spec/example/example_methods.rb +14 -1
  14. data/lib/spec/example/example_proxy.rb +3 -4
  15. data/lib/spec/example/subject.rb +7 -7
  16. data/lib/spec/interop/test.rb +2 -1
  17. data/lib/spec/matchers/be_close.rb +5 -5
  18. data/lib/spec/matchers/be_instance_of.rb +2 -2
  19. data/lib/spec/matchers/be_kind_of.rb +2 -2
  20. data/lib/spec/matchers/compatibility.rb +2 -2
  21. data/lib/spec/matchers/eql.rb +4 -4
  22. data/lib/spec/matchers/equal.rb +18 -8
  23. data/lib/spec/matchers/extensions/instance_exec.rb +1 -3
  24. data/lib/spec/matchers/include.rb +4 -4
  25. data/lib/spec/matchers/match.rb +2 -2
  26. data/lib/spec/matchers/operator_matcher.rb +3 -2
  27. data/lib/spec/mocks/methods.rb +9 -0
  28. data/lib/spec/mocks/mock.rb +34 -24
  29. data/lib/spec/mocks/proxy.rb +1 -0
  30. data/lib/spec/mocks/spec_methods.rb +5 -5
  31. data/lib/spec/runner.rb +1 -2
  32. data/lib/spec/runner/configuration.rb +0 -0
  33. data/lib/spec/runner/drb_command_line.rb +2 -0
  34. data/lib/spec/runner/formatter/base_text_formatter.rb +1 -1
  35. data/lib/spec/runner/formatter/no_op_method_missing.rb +1 -1
  36. data/lib/spec/version.rb +1 -1
  37. data/spec/spec/example/example_group_proxy_spec.rb +24 -0
  38. data/spec/spec/example/example_methods_spec.rb +12 -0
  39. data/spec/spec/example/predicate_matcher_spec.rb +0 -0
  40. data/spec/spec/example/subject_spec.rb +16 -6
  41. data/spec/spec/matchers/change_spec.rb +85 -85
  42. data/spec/spec/matchers/compatibility_spec.rb +16 -22
  43. data/spec/spec/matchers/equal_spec.rb +33 -9
  44. data/spec/spec/matchers/have_spec.rb +1 -8
  45. data/spec/spec/mocks/mock_ordering_spec.rb +10 -0
  46. data/spec/spec/mocks/mock_spec.rb +106 -93
  47. data/spec/spec/mocks/stub_chain_spec.rb +27 -0
  48. data/spec/spec/mocks/stub_spec.rb +9 -5
  49. data/spec/spec/runner/configuration_spec.rb +0 -0
  50. data/spec/spec/runner/drb_command_line_spec.rb +1 -1
  51. data/spec/spec/runner/formatter/html_formatted-1.8.7.html +31 -46
  52. data/spec/spec/runner/formatter/html_formatted-1.9.1.html +25 -28
  53. data/spec/spec/runner/formatter/html_formatter_spec.rb +3 -19
  54. data/spec/spec/runner/formatter/profile_formatter_spec.rb +3 -18
  55. data/spec/spec/runner/formatter/progress_bar_formatter_spec.rb +4 -28
  56. data/spec/spec/runner/formatter/text_mate_formatted-1.8.7.html +31 -46
  57. data/spec/spec/runner/formatter/text_mate_formatted-1.9.1.html +7 -10
  58. data/spec/spec/runner/reporter_spec.rb +1 -1
  59. data/spec/spec_helper.rb +4 -0
  60. data/spec/support/macros.rb +29 -0
  61. metadata +36 -8
  62. metadata.gz.sig +1 -0
@@ -2,31 +2,25 @@ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
2
 
3
3
  Spec::Matchers.define :have_public_instance_method do |method|
4
4
  match do |klass|
5
- klass.public_instance_methods.include?(method)
5
+ klass.public_instance_methods.any? {|m| [method, method.to_sym].include?(m)}
6
6
  end
7
7
  end
8
8
 
9
- module Spec
10
- module Matchers
11
- describe Spec::Matchers do
12
- (Spec::Matchers.constants.sort).each do |c|
13
- if (Class === (klass = Spec::Matchers.const_get(c)))
14
- describe klass do
15
- if klass.public_instance_methods.include?('failure_message_for_should')
16
- describe "called with should" do
17
- subject { klass }
18
- it { should have_public_instance_method('failure_message_for_should')}
19
- it { should have_public_instance_method('failure_message')}
20
- end
21
- end
22
- if klass.public_instance_methods.include?('failure_message_for_should_not')
23
- describe "called with should not" do
24
- subject { klass }
25
- it { should have_public_instance_method('failure_message_for_should_not')}
26
- it { should have_public_instance_method('negative_failure_message')}
27
- end
28
- end
29
- end
9
+ (Spec::Matchers.constants.sort).each do |c|
10
+ if (Class === (klass = Spec::Matchers.const_get(c)))
11
+ describe klass do
12
+ if klass.public_instance_methods.any? {|m| ['failure_message_for_should',:failure_message_for_should].include?(m)}
13
+ describe "called with should" do
14
+ subject { klass }
15
+ it { should have_public_instance_method('failure_message_for_should')}
16
+ it { should have_public_instance_method('failure_message')}
17
+ end
18
+ end
19
+ if klass.public_instance_methods.any? {|m| ['failure_message_for_should_not',:failure_message_for_should_not].include?(m)}
20
+ describe "called with should not" do
21
+ subject { klass }
22
+ it { should have_public_instance_method('failure_message_for_should_not')}
23
+ it { should have_public_instance_method('negative_failure_message')}
30
24
  end
31
25
  end
32
26
  end
@@ -1,8 +1,12 @@
1
1
  require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
-
3
2
  module Spec
4
3
  module Matchers
5
4
  describe "equal" do
5
+
6
+ def inspect_object(o)
7
+ "#<#{o.class}:#{o.object_id}> => #{o.inspect}"
8
+ end
9
+
6
10
  it "should match when actual.equal?(expected)" do
7
11
  1.should equal(1)
8
12
  end
@@ -17,16 +21,36 @@ module Spec
17
21
  matcher.description.should == "equal 1"
18
22
  end
19
23
 
20
- it "should provide message, expected and actual on #failure_message" do
21
- matcher = equal("1")
22
- matcher.matches?(1)
23
- matcher.failure_message_for_should.should == "\nexpected \"1\"\n got 1\n \n(compared using equal?)\n"
24
+ it "should provide message on #failure_message" do
25
+ expected, actual = "1", "1"
26
+ matcher = equal(expected)
27
+ matcher.matches?(actual)
28
+
29
+ matcher.failure_message_for_should.should == <<-MESSAGE
30
+
31
+ expected #{inspect_object(expected)}
32
+ got #{inspect_object(actual)}
33
+
34
+ Compared using equal?, which compares object identity,
35
+ but expected and actual are not the same object. Use
36
+ 'actual.should == expected' if you don't care about
37
+ object identity in this example.
38
+
39
+ MESSAGE
24
40
  end
25
41
 
26
- it "should provide message, expected and actual on #negative_failure_message" do
27
- matcher = equal(1)
28
- matcher.matches?(1)
29
- matcher.failure_message_for_should_not.should == "\nexpected 1 not to equal 1\n\n(compared using equal?)\n"
42
+ it "should provide message on #negative_failure_message" do
43
+ expected = actual = "1"
44
+ matcher = equal(expected)
45
+ matcher.matches?(actual)
46
+ matcher.failure_message_for_should_not.should == <<-MESSAGE
47
+
48
+ expected not #{inspect_object(expected)}
49
+ got #{inspect_object(actual)}
50
+
51
+ Compared using equal?, which compares object identity.
52
+
53
+ MESSAGE
30
54
  end
31
55
  end
32
56
  end
@@ -356,14 +356,7 @@ end
356
356
  module Spec
357
357
  module Matchers
358
358
  describe Have do
359
- it "should have method_missing as private" do
360
- with_ruby '1.8' do
361
- Have.private_instance_methods.should include("method_missing")
362
- end
363
- with_ruby '1.9' do
364
- Have.private_instance_methods.should include(:method_missing)
365
- end
366
- end
359
+ treats_method_missing_as_private :noop => false
367
360
 
368
361
  describe "respond_to?" do
369
362
  before :each do
@@ -78,6 +78,16 @@ module Spec
78
78
  @mock.ignored_1
79
79
  @mock.rspec_verify
80
80
  end
81
+
82
+ it "should pass when duplicates exist" do
83
+ @mock.should_receive(:a).ordered
84
+ @mock.should_receive(:b).ordered
85
+ @mock.should_receive(:a).ordered
86
+
87
+ @mock.a
88
+ @mock.b
89
+ @mock.a
90
+ end
81
91
 
82
92
  end
83
93
  end
@@ -3,29 +3,31 @@ require File.dirname(__FILE__) + '/../../spec_helper'
3
3
  module Spec
4
4
  module Mocks
5
5
  describe Mock do
6
+ treats_method_missing_as_private :subject => Mock.new, :noop => false
7
+
6
8
  before(:each) do
7
9
  @mock = mock("test mock")
8
10
  end
9
-
11
+
10
12
  after(:each) do
11
13
  @mock.rspec_reset
12
14
  end
13
-
15
+
14
16
  describe "deprecated #stub_everything method" do
15
17
  before(:each) do
16
18
  Kernel.stub!(:warn)
17
19
  end
18
-
20
+
19
21
  it "creates a mock that behaves as a null object" do
20
22
  stub_everything.should be_null_object
21
23
  end
22
-
24
+
23
25
  it "provides deprecation warning" do
24
26
  Kernel.should_receive(:warn).with(/DEPRECATION: stub_everything.* is deprecated./)
25
27
  stub_everything
26
28
  end
27
29
  end
28
-
30
+
29
31
  it "should report line number of expectation of unreceived message" do
30
32
  expected_error_line = __LINE__; @mock.should_receive(:wont_happen).with("x", 3)
31
33
  begin
@@ -36,7 +38,7 @@ module Spec
36
38
  e.backtrace[0].should match(/#{File.basename(__FILE__)}:#{expected_error_line}/)
37
39
  end
38
40
  end
39
-
41
+
40
42
  it "should report line number of expectation of unreceived message after #should_receive after similar stub" do
41
43
  @mock.stub!(:wont_happen)
42
44
  expected_error_line = __LINE__; @mock.should_receive(:wont_happen).with("x", 3)
@@ -48,19 +50,19 @@ module Spec
48
50
  e.backtrace[0].should match(/#{File.basename(__FILE__)}:#{expected_error_line}/)
49
51
  end
50
52
  end
51
-
53
+
52
54
  it "should pass when not receiving message specified as not to be received" do
53
55
  @mock.should_not_receive(:not_expected)
54
56
  @mock.rspec_verify
55
57
  end
56
-
58
+
57
59
  it "should pass when receiving message specified as not to be received with different args" do
58
60
  @mock.should_not_receive(:message).with("unwanted text")
59
61
  @mock.should_receive(:message).with("other text")
60
62
  @mock.message "other text"
61
63
  @mock.rspec_verify
62
64
  end
63
-
65
+
64
66
  it "should fail when receiving message specified as not to be received" do
65
67
  @mock.should_not_receive(:not_expected)
66
68
  lambda {
@@ -68,7 +70,7 @@ module Spec
68
70
  violated
69
71
  }.should raise_error(MockExpectationError, "Mock 'test mock' expected :not_expected with (no args) 0 times, but received it once")
70
72
  end
71
-
73
+
72
74
  it "should fail when receiving message specified as not to be received with args" do
73
75
  @mock.should_not_receive(:not_expected).with("unexpected text")
74
76
  lambda {
@@ -76,31 +78,31 @@ module Spec
76
78
  violated
77
79
  }.should raise_error(MockExpectationError, "Mock 'test mock' expected :not_expected with (\"unexpected text\") 0 times, but received it once")
78
80
  end
79
-
81
+
80
82
  it "should pass when receiving message specified as not to be received with wrong args" do
81
83
  @mock.should_not_receive(:not_expected).with("unexpected text")
82
84
  @mock.not_expected "really unexpected text"
83
85
  @mock.rspec_verify
84
86
  end
85
-
87
+
86
88
  it "should allow block to calculate return values" do
87
89
  @mock.should_receive(:something).with("a","b","c").and_return { |a,b,c| c+b+a }
88
90
  @mock.something("a","b","c").should == "cba"
89
91
  @mock.rspec_verify
90
92
  end
91
-
93
+
92
94
  it "should allow parameter as return value" do
93
95
  @mock.should_receive(:something).with("a","b","c").and_return("booh")
94
96
  @mock.something("a","b","c").should == "booh"
95
97
  @mock.rspec_verify
96
98
  end
97
-
99
+
98
100
  it "should return nil if no return value set" do
99
101
  @mock.should_receive(:something).with("a","b","c")
100
102
  @mock.something("a","b","c").should be_nil
101
103
  @mock.rspec_verify
102
104
  end
103
-
105
+
104
106
  it "should raise exception if args don't match when method called" do
105
107
  @mock.should_receive(:something).with("a","b","c").and_return("booh")
106
108
  lambda {
@@ -108,7 +110,7 @@ module Spec
108
110
  violated
109
111
  }.should raise_error(MockExpectationError, "Mock 'test mock' expected :something with (\"a\", \"b\", \"c\") but received it with (\"a\", \"d\", \"c\")")
110
112
  end
111
-
113
+
112
114
  it "should raise exception if args don't match when method called even when the method is stubbed" do
113
115
  @mock.stub!(:something)
114
116
  @mock.should_receive(:something).with("a","b","c")
@@ -117,7 +119,7 @@ module Spec
117
119
  @mock.rspec_verify
118
120
  }.should raise_error(MockExpectationError, "Mock 'test mock' expected :something with (\"a\", \"b\", \"c\") but received it with ([\"a\", \"d\", \"c\"])")
119
121
  end
120
-
122
+
121
123
  it "should raise exception if args don't match when method called even when using null_object" do
122
124
  @mock = mock("test mock", :null_object => true)
123
125
  @mock.should_receive(:something).with("a","b","c")
@@ -126,14 +128,14 @@ module Spec
126
128
  @mock.rspec_verify
127
129
  }.should raise_error(MockExpectationError, "Mock 'test mock' expected :something with (\"a\", \"b\", \"c\") but received it with ([\"a\", \"d\", \"c\"])")
128
130
  end
129
-
131
+
130
132
  it "should fail if unexpected method called" do
131
133
  lambda {
132
134
  @mock.something("a","b","c")
133
135
  violated
134
136
  }.should raise_error(MockExpectationError, "Mock 'test mock' received unexpected message :something with (\"a\", \"b\", \"c\")")
135
137
  end
136
-
138
+
137
139
  it "should use block for expectation if provided" do
138
140
  @mock.should_receive(:something) do | a, b |
139
141
  a.should == "a"
@@ -143,35 +145,35 @@ module Spec
143
145
  @mock.something("a", "b").should == "booh"
144
146
  @mock.rspec_verify
145
147
  end
146
-
148
+
147
149
  it "should fail if expectation block fails" do
148
150
  @mock.should_receive(:something) {| bool | bool.should be_true}
149
151
  lambda {
150
152
  @mock.something false
151
153
  }.should raise_error(MockExpectationError, /Mock 'test mock' received :something but passed block failed with: expected true, got false/)
152
154
  end
153
-
155
+
154
156
  it "should fail right away when method defined as never is received" do
155
157
  @mock.should_receive(:not_expected).never
156
158
  lambda {
157
159
  @mock.not_expected
158
160
  }.should raise_error(MockExpectationError, "Mock 'test mock' expected :not_expected with (no args) 0 times, but received it once")
159
161
  end
160
-
162
+
161
163
  it "should eventually fail when method defined as never is received" do
162
164
  @mock.should_receive(:not_expected).never
163
165
  lambda {
164
166
  @mock.not_expected
165
167
  }.should raise_error(MockExpectationError, "Mock 'test mock' expected :not_expected with (no args) 0 times, but received it once")
166
168
  end
167
-
169
+
168
170
  it "should raise when told to" do
169
171
  @mock.should_receive(:something).and_raise(RuntimeError)
170
172
  lambda do
171
173
  @mock.something
172
174
  end.should raise_error(RuntimeError)
173
175
  end
174
-
176
+
175
177
  it "should raise passed an Exception instance" do
176
178
  error = RuntimeError.new("error message")
177
179
  @mock.should_receive(:something).and_raise(error)
@@ -179,28 +181,28 @@ module Spec
179
181
  @mock.something
180
182
  }.should raise_error(RuntimeError, "error message")
181
183
  end
182
-
184
+
183
185
  it "should raise RuntimeError with passed message" do
184
186
  @mock.should_receive(:something).and_raise("error message")
185
187
  lambda {
186
188
  @mock.something
187
189
  }.should raise_error(RuntimeError, "error message")
188
190
  end
189
-
191
+
190
192
  it "should not raise when told to if args dont match" do
191
193
  @mock.should_receive(:something).with(2).and_raise(RuntimeError)
192
194
  lambda {
193
195
  @mock.something 1
194
196
  }.should raise_error(MockExpectationError)
195
197
  end
196
-
198
+
197
199
  it "should throw when told to" do
198
200
  @mock.should_receive(:something).and_throw(:blech)
199
201
  lambda {
200
202
  @mock.something
201
203
  }.should throw_symbol(:blech)
202
204
  end
203
-
205
+
204
206
  it "should raise when explicit return and block constrained" do
205
207
  lambda {
206
208
  @mock.should_receive(:fruit) do |colour|
@@ -208,7 +210,7 @@ module Spec
208
210
  end.and_return :apple
209
211
  }.should raise_error(AmbiguousReturnError)
210
212
  end
211
-
213
+
212
214
  it "should ignore args on any args" do
213
215
  @mock.should_receive(:something).at_least(:once).with(any_args)
214
216
  @mock.something
@@ -217,27 +219,27 @@ module Spec
217
219
  @mock.something [], {}, "joe", 7
218
220
  @mock.rspec_verify
219
221
  end
220
-
222
+
221
223
  it "should fail on no args if any args received" do
222
224
  @mock.should_receive(:something).with(no_args())
223
225
  lambda {
224
226
  @mock.something 1
225
227
  }.should raise_error(MockExpectationError, "Mock 'test mock' expected :something with (no args) but received it with (1)")
226
228
  end
227
-
229
+
228
230
  it "should fail when args are expected but none are received" do
229
231
  @mock.should_receive(:something).with(1)
230
232
  lambda {
231
233
  @mock.something
232
234
  }.should raise_error(MockExpectationError, "Mock 'test mock' expected :something with (1) but received it with (no args)")
233
235
  end
234
-
236
+
235
237
  it "should return value from block by default" do
236
238
  @mock.stub!(:method_that_yields).and_yield
237
239
  @mock.method_that_yields { :returned_obj }.should == :returned_obj
238
240
  @mock.rspec_verify
239
241
  end
240
-
242
+
241
243
  it "should yield 0 args to blocks that take a variable number of arguments" do
242
244
  @mock.should_receive(:yield_back).with(no_args()).once.and_yield
243
245
  a = nil
@@ -245,7 +247,7 @@ module Spec
245
247
  a.should == []
246
248
  @mock.rspec_verify
247
249
  end
248
-
250
+
249
251
  it "should yield 0 args multiple times to blocks that take a variable number of arguments" do
250
252
  @mock.should_receive(:yield_back).once.with(no_args()).once.and_yield.
251
253
  and_yield
@@ -255,7 +257,7 @@ module Spec
255
257
  b.should == [ [], [] ]
256
258
  @mock.rspec_verify
257
259
  end
258
-
260
+
259
261
  it "should yield one arg to blocks that take a variable number of arguments" do
260
262
  @mock.should_receive(:yield_back).with(no_args()).once.and_yield(99)
261
263
  a = nil
@@ -263,7 +265,7 @@ module Spec
263
265
  a.should == [99]
264
266
  @mock.rspec_verify
265
267
  end
266
-
268
+
267
269
  it "should yield one arg 3 times consecutively to blocks that take a variable number of arguments" do
268
270
  @mock.should_receive(:yield_back).once.with(no_args()).once.and_yield(99).
269
271
  and_yield(43).
@@ -274,7 +276,7 @@ module Spec
274
276
  b.should == [[99], [43], ["something fruity"]]
275
277
  @mock.rspec_verify
276
278
  end
277
-
279
+
278
280
  it "should yield many args to blocks that take a variable number of arguments" do
279
281
  @mock.should_receive(:yield_back).with(no_args()).once.and_yield(99, 27, "go")
280
282
  a = nil
@@ -282,7 +284,7 @@ module Spec
282
284
  a.should == [99, 27, "go"]
283
285
  @mock.rspec_verify
284
286
  end
285
-
287
+
286
288
  it "should yield many args 3 times consecutively to blocks that take a variable number of arguments" do
287
289
  @mock.should_receive(:yield_back).once.with(no_args()).once.and_yield(99, :green, "go").
288
290
  and_yield("wait", :amber).
@@ -293,7 +295,7 @@ module Spec
293
295
  b.should == [[99, :green, "go"], ["wait", :amber], ["stop", 12, :red]]
294
296
  @mock.rspec_verify
295
297
  end
296
-
298
+
297
299
  it "should yield single value" do
298
300
  @mock.should_receive(:yield_back).with(no_args()).once.and_yield(99)
299
301
  a = nil
@@ -301,7 +303,7 @@ module Spec
301
303
  a.should == 99
302
304
  @mock.rspec_verify
303
305
  end
304
-
306
+
305
307
  it "should yield single value 3 times consecutively" do
306
308
  @mock.should_receive(:yield_back).once.with(no_args()).once.and_yield(99).
307
309
  and_yield(43).
@@ -312,7 +314,7 @@ module Spec
312
314
  b.should == [99, 43, "something fruity"]
313
315
  @mock.rspec_verify
314
316
  end
315
-
317
+
316
318
  it "should yield two values" do
317
319
  @mock.should_receive(:yield_back).with(no_args()).once.and_yield('wha', 'zup')
318
320
  a, b = nil
@@ -321,7 +323,7 @@ module Spec
321
323
  b.should == 'zup'
322
324
  @mock.rspec_verify
323
325
  end
324
-
326
+
325
327
  it "should yield two values 3 times consecutively" do
326
328
  @mock.should_receive(:yield_back).once.with(no_args()).once.and_yield('wha', 'zup').
327
329
  and_yield('not', 'down').
@@ -332,14 +334,14 @@ module Spec
332
334
  c.should == [['wha', 'zup'], ['not', 'down'], [14, 65]]
333
335
  @mock.rspec_verify
334
336
  end
335
-
337
+
336
338
  it "should fail when calling yielding method with wrong arity" do
337
339
  @mock.should_receive(:yield_back).with(no_args()).once.and_yield('wha', 'zup')
338
340
  lambda {
339
341
  @mock.yield_back {|a|}
340
342
  }.should raise_error(MockExpectationError, "Mock 'test mock' yielded |\"wha\", \"zup\"| to block with arity of 1")
341
343
  end
342
-
344
+
343
345
  it "should fail when calling yielding method consecutively with wrong arity" do
344
346
  @mock.should_receive(:yield_back).once.with(no_args()).once.and_yield('wha', 'zup').
345
347
  and_yield('down').
@@ -350,32 +352,32 @@ module Spec
350
352
  @mock.yield_back {|a,b| c << [a, b]}
351
353
  }.should raise_error(MockExpectationError, "Mock 'test mock' yielded |\"down\"| to block with arity of 2")
352
354
  end
353
-
355
+
354
356
  it "should fail when calling yielding method without block" do
355
357
  @mock.should_receive(:yield_back).with(no_args()).once.and_yield('wha', 'zup')
356
358
  lambda {
357
359
  @mock.yield_back
358
360
  }.should raise_error(MockExpectationError, "Mock 'test mock' asked to yield |[\"wha\", \"zup\"]| but no block was passed")
359
361
  end
360
-
362
+
361
363
  it "should be able to mock send" do
362
364
  @mock.should_receive(:send).with(any_args)
363
365
  @mock.send 'hi'
364
366
  @mock.rspec_verify
365
367
  end
366
-
368
+
367
369
  it "should be able to raise from method calling yielding mock" do
368
370
  @mock.should_receive(:yield_me).and_yield 44
369
-
371
+
370
372
  lambda {
371
373
  @mock.yield_me do |x|
372
374
  raise "Bang"
373
375
  end
374
376
  }.should raise_error(StandardError, "Bang")
375
-
377
+
376
378
  @mock.rspec_verify
377
379
  end
378
-
380
+
379
381
  it "should clear expectations after verify" do
380
382
  @mock.should_receive(:foobar)
381
383
  @mock.foobar
@@ -384,43 +386,43 @@ module Spec
384
386
  @mock.foobar
385
387
  }.should raise_error(MockExpectationError, "Mock 'test mock' received unexpected message :foobar with (no args)")
386
388
  end
387
-
389
+
388
390
  it "should restore objects to their original state on rspec_reset" do
389
391
  mock = mock("this is a mock")
390
392
  mock.should_receive(:blah)
391
393
  mock.rspec_reset
392
394
  mock.rspec_verify #should throw if reset didn't work
393
395
  end
394
-
396
+
395
397
  it "should work even after method_missing starts raising NameErrors instead of NoMethodErrors" do
396
398
  # Object#method_missing throws either NameErrors or NoMethodErrors.
397
399
  #
398
- # On a fresh ruby program Object#method_missing:
400
+ # On a fresh ruby program Object#method_missing:
399
401
  # * raises a NoMethodError when called directly
400
402
  # * raises a NameError when called indirectly
401
403
  #
402
404
  # Once Object#method_missing has been called at least once (on any object)
403
- # it starts behaving differently:
405
+ # it starts behaving differently:
404
406
  # * raises a NameError when called directly
405
407
  # * raises a NameError when called indirectly
406
408
  #
407
409
  # There was a bug in Mock#method_missing that relied on the fact
408
410
  # that calling Object#method_missing directly raises a NoMethodError.
409
411
  # This example tests that the bug doesn't exist anymore.
410
-
411
-
412
+
413
+
412
414
  # Ensures that method_missing always raises NameErrors.
413
415
  a_method_that_doesnt_exist rescue
414
-
415
-
416
+
417
+
416
418
  @mock.should_receive(:foobar)
417
419
  @mock.foobar
418
420
  @mock.rspec_verify
419
-
421
+
420
422
  lambda { @mock.foobar }.should_not raise_error(NameError)
421
423
  lambda { @mock.foobar }.should raise_error(MockExpectationError)
422
424
  end
423
-
425
+
424
426
  it "should temporarily replace a method stub on a mock" do
425
427
  @mock.stub!(:msg).and_return(:stub_value)
426
428
  @mock.should_receive(:msg).with(:arg).and_return(:mock_value)
@@ -484,80 +486,80 @@ module Spec
484
486
  mock = Mock.new('name', :message => :response)
485
487
  mock.message.should == :response
486
488
  end
487
-
489
+
488
490
  end
489
-
491
+
490
492
  describe "a mock message receiving a block" do
491
493
  before(:each) do
492
494
  @mock = mock("mock")
493
495
  @calls = 0
494
496
  end
495
-
497
+
496
498
  def add_call
497
499
  @calls = @calls + 1
498
500
  end
499
-
501
+
500
502
  it "should call the block after #should_receive" do
501
503
  @mock.should_receive(:foo) { add_call }
502
-
504
+
503
505
  @mock.foo
504
-
506
+
505
507
  @calls.should == 1
506
508
  end
507
-
509
+
508
510
  it "should call the block after #should_receive after a similar stub" do
509
511
  @mock.stub!(:foo).and_return(:bar)
510
512
  @mock.should_receive(:foo) { add_call }
511
-
513
+
512
514
  @mock.foo
513
-
515
+
514
516
  @calls.should == 1
515
517
  end
516
-
518
+
517
519
  it "should call the block after #once" do
518
520
  @mock.should_receive(:foo).once { add_call }
519
-
521
+
520
522
  @mock.foo
521
-
523
+
522
524
  @calls.should == 1
523
525
  end
524
-
526
+
525
527
  it "should call the block after #twice" do
526
528
  @mock.should_receive(:foo).twice { add_call }
527
-
529
+
528
530
  @mock.foo
529
531
  @mock.foo
530
-
532
+
531
533
  @calls.should == 2
532
534
  end
533
-
535
+
534
536
  it "should call the block after #times" do
535
537
  @mock.should_receive(:foo).exactly(10).times { add_call }
536
-
538
+
537
539
  (1..10).each { @mock.foo }
538
-
540
+
539
541
  @calls.should == 10
540
542
  end
541
-
543
+
542
544
  it "should call the block after #any_number_of_times" do
543
545
  @mock.should_receive(:foo).any_number_of_times { add_call }
544
-
546
+
545
547
  (1..7).each { @mock.foo }
546
-
548
+
547
549
  @calls.should == 7
548
550
  end
549
-
551
+
550
552
  it "should call the block after #ordered" do
551
553
  @mock.should_receive(:foo).ordered { add_call }
552
554
  @mock.should_receive(:bar).ordered { add_call }
553
-
555
+
554
556
  @mock.foo
555
557
  @mock.bar
556
-
558
+
557
559
  @calls.should == 2
558
560
  end
559
561
  end
560
-
562
+
561
563
  describe 'string representation generated by #to_s' do
562
564
  it 'should not contain < because that might lead to invalid HTML in some situations' do
563
565
  mock = mock("Dog")
@@ -565,21 +567,32 @@ module Spec
565
567
  valid_html_str.should_not include('<')
566
568
  end
567
569
  end
568
-
570
+
571
+ describe "mock created with no name" do
572
+ it "should name itself 'mock'" do
573
+ mock.to_s.should include('mock')
574
+ end
575
+
576
+ it "should name itself after initially stubbed methods" do
577
+ string = mock(:foo => "woo", :bar => "car").to_s
578
+ string.should include('foo: \"woo\"', 'bar: \"car\"')
579
+ end
580
+
581
+ it "should respond to initially stubbed methods" do
582
+ mock = mock(:foo => "woo", :bar => "car")
583
+ mock.foo.should == "woo"
584
+ mock.bar.should == "car"
585
+ end
586
+ end
587
+
569
588
  describe "==" do
570
589
  it "sends '== self' to the comparison object" do
571
590
  first = mock('first')
572
591
  second = mock('second')
573
-
592
+
574
593
  first.should_receive(:==).with(second)
575
594
  second == first
576
595
  end
577
596
  end
578
-
579
- describe "method_missing" do
580
- it "should be private" do
581
- Mock.private_instance_methods.should include("method_missing")
582
- end
583
- end
584
597
  end
585
598
  end