rspec 0.5.16 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. data/CHANGES +12 -0
  2. data/EXAMPLES.rd +2 -2
  3. data/examples/custom_method_spec.rb +2 -2
  4. data/examples/mocking_spec.rb +2 -2
  5. data/lib/spec/api/helper.rb +0 -6
  6. data/lib/spec/api/helper/diff.rb +1 -0
  7. data/lib/spec/api/helper/have_helper.rb +7 -23
  8. data/lib/spec/api/helper/should_helper.rb +23 -19
  9. data/lib/spec/api/helper/should_negator.rb +10 -18
  10. data/lib/spec/api/mocks/argument_expectation.rb +2 -2
  11. data/lib/spec/api/mocks/message_expectation.rb +28 -53
  12. data/lib/spec/api/mocks/mock.rb +4 -5
  13. data/lib/spec/api/sugar.rb +6 -23
  14. data/lib/spec/runner/instance_exec.rb +11 -5
  15. data/lib/spec/version.rb +2 -2
  16. data/test/spec/api/helper/arbitrary_predicate_test.rb +16 -16
  17. data/test/spec/api/helper/containment_test.rb +16 -16
  18. data/test/spec/api/helper/identity_test.rb +8 -8
  19. data/test/spec/api/helper/object_equality_test.rb +13 -13
  20. data/test/spec/api/helper/raising_test.rb +14 -14
  21. data/test/spec/api/helper/regex_matching_test.rb +4 -4
  22. data/test/spec/api/helper/should_have_test.rb +26 -20
  23. data/test/spec/api/helper/should_satisfy_test.rb +5 -5
  24. data/test/spec/api/helper/throwing_test.rb +7 -7
  25. data/test/spec/api/helper/true_false_special_case_test.rb +12 -12
  26. data/test/spec/api/helper/typing_test.rb +14 -14
  27. data/test/spec/api/mocks/mock_arg_constraints_test.rb +26 -20
  28. data/test/spec/api/mocks/mock_counts_test.rb +431 -0
  29. data/test/spec/api/mocks/mock_ordering_test.rb +61 -42
  30. data/test/spec/api/mocks/mock_test.rb +68 -230
  31. data/test/spec/api/sugar_test.rb +1 -1
  32. data/test/spec/runner/backtrace_tweaker_test.rb +2 -2
  33. data/test/spec/runner/context_runner_test.rb +4 -4
  34. data/test/spec/runner/context_test.rb +22 -22
  35. data/test/spec/runner/execution_context_test.rb +1 -1
  36. data/test/spec/runner/reporter_test.rb +6 -6
  37. data/test/spec/runner/specification_test.rb +30 -30
  38. data/test/test_classes.rb +13 -4
  39. metadata +6 -11
  40. data/lib/spec/api/helper/instance_helper.rb +0 -15
  41. data/lib/spec/api/helper/instance_negator.rb +0 -15
  42. data/lib/spec/api/helper/kind_helper.rb +0 -15
  43. data/lib/spec/api/helper/kind_negator.rb +0 -15
  44. data/lib/spec/api/helper/respond_helper.rb +0 -15
  45. data/lib/spec/api/helper/respond_negator.rb +0 -15
  46. data/test/spec/api/duck_type_test.rb +0 -19
@@ -2,76 +2,82 @@ require File.dirname(__FILE__) + '/../../../test_helper'
2
2
 
3
3
  module Spec
4
4
  module Api
5
- class PassingConstraintsToMockTest < Test::Unit::TestCase
5
+ class PassingMockArgumentConstraintsTest < Test::Unit::TestCase
6
6
 
7
7
  def setup
8
8
  @mock = Mock.new("test mock")
9
9
  end
10
10
 
11
- def test_should_handle_any_arg
12
- @mock.should.receive(:random_call).with("a", :anything, "c")
11
+ def test_should_accept_string_as_anything
12
+ @mock.should_receive(:random_call).with("a", :anything, "c")
13
13
  @mock.random_call("a", "whatever", "c")
14
14
  @mock.__verify
15
15
  end
16
16
 
17
17
  def test_should_accept_fixnum_as_numeric
18
- @mock.should.receive(:random_call).with(:numeric)
18
+ @mock.should_receive(:random_call).with(:numeric)
19
19
  @mock.random_call(1)
20
20
  @mock.__verify
21
21
  end
22
22
 
23
23
  def test_should_accept_float_as_numeric
24
- @mock.should.receive(:random_call).with(:numeric)
24
+ @mock.should_receive(:random_call).with(:numeric)
25
25
  @mock.random_call(1.5)
26
26
  @mock.__verify
27
27
  end
28
28
 
29
- def test_should_match_true_for_boolean
30
- @mock.should.receive(:random_call).with(:boolean)
29
+ def test_should_accept_true_as_boolean
30
+ @mock.should_receive(:random_call).with(:boolean)
31
31
  @mock.random_call(true)
32
32
  @mock.__verify
33
33
  end
34
34
 
35
- def test_should_match_false_for_boolean
36
- @mock.should.receive(:random_call).with(:boolean)
35
+ def test_should_accept_false_as_boolean
36
+ @mock.should_receive(:random_call).with(:boolean)
37
37
  @mock.random_call(false)
38
38
  @mock.__verify
39
39
  end
40
40
 
41
41
  def test_should_match_string
42
- @mock.should.receive(:random_call).with(:string)
42
+ @mock.should_receive(:random_call).with(:string)
43
43
  @mock.random_call("a string")
44
44
  @mock.__verify
45
45
  end
46
46
 
47
47
  def test_should_match_non_special_symbol
48
- @mock.should.receive(:random_call).with(:some_symbol)
48
+ @mock.should_receive(:random_call).with(:some_symbol)
49
49
  @mock.random_call(:some_symbol)
50
50
  @mock.__verify
51
51
  end
52
52
 
53
- def test_should_process_duck_type_with_one_method
54
- @mock.should.receive(:random_call).with(DuckTypeArgConstraint.new(:length))
53
+ def test_should_match_duck_type_with_one_method
54
+ @mock.should_receive(:random_call).with(DuckTypeArgConstraint.new(:length))
55
55
  @mock.random_call([])
56
56
  @mock.__verify
57
57
  end
58
58
 
59
- def test_should_process_duck_type_with_two_methods
60
- @mock.should.receive(:random_call).with(DuckTypeArgConstraint.new(:abs, :div))
59
+ def test_should_match_duck_type_with_two_methods
60
+ @mock.should_receive(:random_call).with(DuckTypeArgConstraint.new(:abs, :div))
61
61
  @mock.random_call(1)
62
62
  @mock.__verify
63
63
  end
64
64
 
65
+ def test_should_match_user_defined_type
66
+ @mock.should_receive(:random_call).with(Person.new("David"))
67
+ @mock.random_call(Person.new("David"))
68
+ @mock.__verify
69
+ end
70
+
65
71
  end
66
72
 
67
- class FailingConstraintsTest < Test::Unit::TestCase
73
+ class FailingMockArgumentConstraintsTest < Test::Unit::TestCase
68
74
 
69
75
  def setup
70
76
  @mock = Mock.new("test mock")
71
77
  end
72
78
 
73
79
  def test_should_reject_non_numeric
74
- @mock.should.receive(:random_call).with(:numeric)
80
+ @mock.should_receive(:random_call).with(:numeric)
75
81
  assert_raise(MockExpectationError) do
76
82
  @mock.random_call("1")
77
83
  @mock.__verify
@@ -79,7 +85,7 @@ module Spec
79
85
  end
80
86
 
81
87
  def test_should_reject_non_boolean
82
- @mock.should.receive(:random_call).with(:boolean)
88
+ @mock.should_receive(:random_call).with(:boolean)
83
89
  assert_raise(MockExpectationError) do
84
90
  @mock.random_call("false")
85
91
  @mock.__verify
@@ -87,7 +93,7 @@ module Spec
87
93
  end
88
94
 
89
95
  def test_should_reject_non_string
90
- @mock.should.receive(:random_call).with(:string)
96
+ @mock.should_receive(:random_call).with(:string)
91
97
  assert_raise(MockExpectationError) do
92
98
  @mock.random_call(123)
93
99
  @mock.__verify
@@ -95,7 +101,7 @@ module Spec
95
101
  end
96
102
 
97
103
  def test_should_reject_goose_when_expecting_a_duck
98
- @mock.should.receive(:random_call).with(DuckTypeArgConstraint.new(:abs, :div))
104
+ @mock.should_receive(:random_call).with(DuckTypeArgConstraint.new(:abs, :div))
99
105
  assert_raise(MockExpectationError) do
100
106
  @mock.random_call("I don't respond to :abs or :div")
101
107
  @mock.__verify
@@ -0,0 +1,431 @@
1
+ require File.dirname(__FILE__) + '/../../../test_helper'
2
+
3
+ module Spec
4
+ module Api
5
+ class OnceCountsTest < Test::Unit::TestCase
6
+
7
+ def setup
8
+ @mock = Mock.new("test mock")
9
+ end
10
+
11
+ def test_once_should_pass_when_called_once
12
+ @mock.should_receive(:random_call).once
13
+ @mock.random_call
14
+ assert_nothing_raised do
15
+ @mock.__verify
16
+ end
17
+ end
18
+
19
+ def test_once_should_pass_when_called_once_with_unspecified_args
20
+ @mock.should_receive(:random_call).once
21
+ @mock.random_call("a","b","c")
22
+ assert_nothing_raised do
23
+ @mock.__verify
24
+ end
25
+ end
26
+
27
+ def test_once_should_pass_when_called_once_with_specified_args
28
+ @mock.should_receive(:random_call).once.with("a","b","c")
29
+ @mock.random_call("a","b","c")
30
+ assert_nothing_raised do
31
+ @mock.__verify
32
+ end
33
+ end
34
+
35
+ def test_once_should_fail_when_not_called
36
+ @mock.should_receive(:random_call).once
37
+ assert_raise(MockExpectationError) do
38
+ @mock.__verify
39
+ end
40
+ end
41
+
42
+ def test_once_should_fail_when_called_twice
43
+ @mock.should_receive(:random_call).once
44
+ @mock.random_call
45
+ @mock.random_call
46
+ assert_raise(MockExpectationError) do
47
+ @mock.__verify
48
+ end
49
+ end
50
+
51
+ def test_once_should_fail_when_called_once_with_wrong_args
52
+ @mock.should_receive(:random_call).once.with("a","b","c")
53
+ assert_raise(MockExpectationError) do
54
+ @mock.random_call "d","e","f"
55
+ end
56
+ end
57
+ end
58
+
59
+ class TwiceCountsTest < Test::Unit::TestCase
60
+
61
+ def setup
62
+ @mock = Mock.new("test mock")
63
+ end
64
+
65
+ def test_twice_should_pass_when_called_twice
66
+ @mock.should_receive(:random_call).twice
67
+ @mock.random_call
68
+ @mock.random_call
69
+ assert_nothing_raised do
70
+ @mock.__verify
71
+ end
72
+ end
73
+
74
+ def test_twice_should_pass_when_called_twice_with_unspecified_args
75
+ @mock.should_receive(:random_call).twice
76
+ @mock.random_call "1"
77
+ @mock.random_call 1
78
+ assert_nothing_raised do
79
+ @mock.__verify
80
+ end
81
+ end
82
+
83
+ def test_twice_should_pass_when_called_twice_with_specified_args
84
+ @mock.should_receive(:random_call).twice.with("1", 1)
85
+ @mock.random_call "1", 1
86
+ @mock.random_call "1", 1
87
+ assert_nothing_raised do
88
+ @mock.__verify
89
+ end
90
+ end
91
+
92
+ def test_twice_should_fail_when_called_twice_with_wrong_args_on_the_second_call
93
+ @mock.should_receive(:random_call).twice.with("1", 1)
94
+ @mock.random_call "1", 1
95
+ assert_raise(MockExpectationError) do
96
+ @mock.random_call 1, "1"
97
+ end
98
+ end
99
+
100
+ def test_twice_should_fail_when_called_twice_with_wrong_args_on_the_first_call
101
+ @mock.should_receive(:random_call).twice.with("1", 1)
102
+ assert_raise(MockExpectationError) do
103
+ @mock.random_call 1, "1"
104
+ end
105
+ end
106
+
107
+ def test_twice_should_fail_when_call_count_is_lower_than_expected
108
+ @mock.should_receive(:random_call).twice
109
+ @mock.random_call
110
+ assert_raise(MockExpectationError) do
111
+ @mock.__verify
112
+ end
113
+ end
114
+
115
+ def test_twice_should_fail_when_call_count_is_higher_than_expected
116
+ @mock.should_receive(:random_call).twice
117
+ @mock.random_call
118
+ @mock.random_call
119
+ @mock.random_call
120
+ assert_raise(MockExpectationError) do
121
+ @mock.__verify
122
+ end
123
+ end
124
+ end
125
+
126
+ class AnyNumberOfTimesTest < Test::Unit::TestCase
127
+
128
+ def setup
129
+ @mock = Mock.new("test mock")
130
+ end
131
+
132
+ def test_should_pass_if_any_number_of_times_method_is_not_called
133
+ @mock.should_receive(:random_call).any_number_of_times
134
+ assert_nothing_raised do
135
+ @mock.__verify
136
+ end
137
+ end
138
+
139
+ def test_should_pass_if_any_number_of_times_method_is_called_once
140
+ @mock.should_receive(:random_call).any_number_of_times
141
+ @mock.random_call
142
+ assert_nothing_raised do
143
+ @mock.__verify
144
+ end
145
+ end
146
+
147
+ def test_should_pass_if_any_number_of_times_method_is_called_many_times
148
+ @mock.should_receive(:random_call).any_number_of_times
149
+ (1..10).each { @mock.random_call }
150
+ assert_nothing_raised do
151
+ @mock.__verify
152
+ end
153
+ end
154
+ end
155
+
156
+ class PreciseCountsTest < Test::Unit::TestCase
157
+
158
+ def setup
159
+ @mock = Mock.new("test mock")
160
+ end
161
+
162
+ def test_should_pass_mutiple_calls_with_different_args
163
+ @mock.should_receive(:random_call).once.with(1)
164
+ @mock.should_receive(:random_call).once.with(2)
165
+ @mock.random_call(1)
166
+ @mock.random_call(2)
167
+ assert_nothing_raised do
168
+ @mock.__verify
169
+ end
170
+ end
171
+
172
+ def test_should_pass_multiple_calls_with_different_args_and_counts
173
+ @mock.should_receive(:random_call).twice.with(1)
174
+ @mock.should_receive(:random_call).once.with(2)
175
+ @mock.random_call(1)
176
+ @mock.random_call(2)
177
+ @mock.random_call(1)
178
+ assert_nothing_raised do
179
+ @mock.__verify
180
+ end
181
+ end
182
+
183
+ def test_should_fail_when_exactly_n_times_method_is_never_called
184
+ @mock.should_receive(:random_call).exactly(3).times
185
+ assert_raise(MockExpectationError) do
186
+ @mock.__verify
187
+ end
188
+ end
189
+
190
+ def test_should_fail_when_exactly_n_times_method_is_called_less_than_n_times
191
+ @mock.should_receive(:random_call).exactly(3).times
192
+ @mock.random_call
193
+ @mock.random_call
194
+ assert_raise(MockExpectationError) do
195
+ @mock.__verify
196
+ end
197
+ end
198
+
199
+ def test_should_pass_if_exactly_n_times_method_is_called_exactly_n_times
200
+ @mock.should_receive(:random_call).exactly(3).times
201
+ @mock.random_call
202
+ @mock.random_call
203
+ @mock.random_call
204
+ assert_nothing_raised do
205
+ @mock.__verify
206
+ end
207
+ end
208
+
209
+ end
210
+
211
+ class AtLeastTest < Test::Unit::TestCase
212
+
213
+ def setup
214
+ @mock = Mock.new("test mock")
215
+ end
216
+
217
+ def test_should_fail_when_at_least_once_method_is_never_called
218
+ @mock.should_receive(:random_call).at_least(:once)
219
+ assert_raise(MockExpectationError) do
220
+ @mock.__verify
221
+ end
222
+ end
223
+
224
+ def test_should_pass_when_at_least_once_method_is_called_once
225
+ @mock.should_receive(:random_call).at_least(:once)
226
+ @mock.random_call
227
+ assert_nothing_raised do
228
+ @mock.__verify
229
+ end
230
+ end
231
+
232
+ def test_should_pass_when_at_least_once_method_is_called_twice
233
+ @mock.should_receive(:random_call).at_least(:once)
234
+ @mock.random_call
235
+ @mock.random_call
236
+ assert_nothing_raised do
237
+ @mock.__verify
238
+ end
239
+ end
240
+
241
+ def test_should_fail_when_at_least_twice_method_is_never_called
242
+ @mock.should_receive(:random_call).at_least(:twice)
243
+ assert_raise(MockExpectationError) do
244
+ @mock.__verify
245
+ end
246
+ end
247
+
248
+ def test_should_fail_when_at_least_twice_method_is_called_once
249
+ @mock.should_receive(:random_call).at_least(:twice)
250
+ @mock.random_call
251
+ assert_raise(MockExpectationError) do
252
+ @mock.__verify
253
+ end
254
+ end
255
+
256
+ def test_should_pass_when_at_least_twice_method_is_called_twice
257
+ @mock.should_receive(:random_call).at_least(:twice)
258
+ @mock.random_call
259
+ @mock.random_call
260
+ assert_nothing_raised do
261
+ @mock.__verify
262
+ end
263
+ end
264
+
265
+ def test_should_pass_when_at_least_twice_method_is_called_three_times
266
+ @mock.should_receive(:random_call).at_least(:twice)
267
+ @mock.random_call
268
+ @mock.random_call
269
+ @mock.random_call
270
+ assert_nothing_raised do
271
+ @mock.__verify
272
+ end
273
+ end
274
+
275
+ def test_should_fail_at_least_n_times_method_is_never_called
276
+ @mock.should_receive(:random_call).at_least(4).times
277
+ assert_raise(MockExpectationError) do
278
+ @mock.__verify
279
+ end
280
+ end
281
+
282
+ def test_should_fail_when_at_least_n_times_method_is_called_less_than_n_times
283
+ @mock.should_receive(:random_call).at_least(4).times
284
+ @mock.random_call
285
+ @mock.random_call
286
+ @mock.random_call
287
+ assert_raise(MockExpectationError) do
288
+ @mock.__verify
289
+ end
290
+ end
291
+
292
+ def test_should_pass_when_at_least_n_times_method_is_called_exactly_n_times
293
+ @mock.should_receive(:random_call).at_least(4).times
294
+ @mock.random_call
295
+ @mock.random_call
296
+ @mock.random_call
297
+ @mock.random_call
298
+ assert_nothing_raised do
299
+ @mock.__verify
300
+ end
301
+ end
302
+
303
+ def test_should_pass_when_at_least_n_times_method_is_called_n_plus_1_times
304
+ @mock.should_receive(:random_call).at_least(4).times
305
+ @mock.random_call
306
+ @mock.random_call
307
+ @mock.random_call
308
+ @mock.random_call
309
+ @mock.random_call
310
+ assert_nothing_raised do
311
+ @mock.__verify
312
+ end
313
+ end
314
+
315
+ def test_should_use_last_return_value_for_subsequent_calls
316
+ @mock.should_receive(:multi_call).at_least(:once).with(:no_args).and_return([11, 22])
317
+ assert_equal(11, @mock.multi_call)
318
+ assert_equal(22, @mock.multi_call)
319
+ assert_equal(22, @mock.multi_call)
320
+ assert_nothing_raised do
321
+ @mock.__verify
322
+ end
323
+ end
324
+ end
325
+
326
+ class AtMostTest < Test::Unit::TestCase
327
+
328
+ def setup
329
+ @mock = Mock.new("test mock")
330
+ end
331
+
332
+ def test_should_pass_when_at_most_once_method_is_never_called
333
+ @mock.should_receive(:random_call).at_most(:once)
334
+ assert_nothing_raised do
335
+ @mock.__verify
336
+ end
337
+ end
338
+
339
+ def test_should_pass_when_at_most_once_method_is_called_once
340
+ @mock.should_receive(:random_call).at_most(:once)
341
+ @mock.random_call
342
+ assert_nothing_raised do
343
+ @mock.__verify
344
+ end
345
+ end
346
+
347
+ def test_should_fail_when_at_most_once_method_is_called_twice
348
+ @mock.should_receive(:random_call).at_most(:once)
349
+ @mock.random_call
350
+ @mock.random_call
351
+ assert_raise(MockExpectationError) do
352
+ @mock.__verify
353
+ end
354
+ end
355
+
356
+ def test_should_pass_when_at_most_twice_method_is_never_called
357
+ @mock.should_receive(:random_call).at_most(:twice)
358
+ assert_nothing_raised do
359
+ @mock.__verify
360
+ end
361
+ end
362
+
363
+ def test_should_pass_when_at_most_twice_method_is_called_once
364
+ @mock.should_receive(:random_call).at_most(:twice)
365
+ @mock.random_call
366
+ assert_nothing_raised do
367
+ @mock.__verify
368
+ end
369
+ end
370
+
371
+ def test_should_pass_when_at_most_twice_method_is_called_twice
372
+ @mock.should_receive(:random_call).at_most(:twice)
373
+ @mock.random_call
374
+ @mock.random_call
375
+ assert_nothing_raised do
376
+ @mock.__verify
377
+ end
378
+ end
379
+
380
+ def test_should_fail_when_at_most_twice_method_is_called_three_times
381
+ @mock.should_receive(:random_call).at_most(:twice)
382
+ @mock.random_call
383
+ @mock.random_call
384
+ @mock.random_call
385
+ assert_raise(MockExpectationError) do
386
+ @mock.__verify
387
+ end
388
+ end
389
+
390
+ def test_should_pass_when_at_most_n_times_method_is_never_called
391
+ @mock.should_receive(:random_call).at_most(4).times
392
+ assert_nothing_raised do
393
+ @mock.__verify
394
+ end
395
+ end
396
+
397
+ def test_should_pass_when_at_most_n_times_method_is_called_less_than_n_times
398
+ @mock.should_receive(:random_call).at_most(4).times
399
+ @mock.random_call
400
+ @mock.random_call
401
+ @mock.random_call
402
+ assert_nothing_raised do
403
+ @mock.__verify
404
+ end
405
+ end
406
+
407
+ def test_should_pass_when_at_most_n_times_method_is_called_exactly_n_times
408
+ @mock.should_receive(:random_call).at_most(4).times
409
+ @mock.random_call
410
+ @mock.random_call
411
+ @mock.random_call
412
+ @mock.random_call
413
+ assert_nothing_raised do
414
+ @mock.__verify
415
+ end
416
+ end
417
+
418
+ def test_should_fail_when_at_most_n_times_method_is_called_n_plus_1_times
419
+ @mock.should_receive(:random_call).at_most(4).times
420
+ @mock.random_call
421
+ @mock.random_call
422
+ @mock.random_call
423
+ @mock.random_call
424
+ @mock.random_call
425
+ assert_raise(MockExpectationError) do
426
+ @mock.__verify
427
+ end
428
+ end
429
+ end
430
+ end
431
+ end