flexmock 1.3.3 → 2.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. checksums.yaml +4 -4
  2. data/.autotest +3 -0
  3. data/.gitignore +14 -0
  4. data/.togglerc +7 -0
  5. data/.travis.yml +5 -0
  6. data/.yardopts +2 -0
  7. data/CHANGES +11 -0
  8. data/Gemfile +1 -4
  9. data/README.md +39 -11
  10. data/Rakefile +6 -217
  11. data/doc/examples/rspec_examples_spec.rb +244 -0
  12. data/doc/examples/test_unit_examples_test.rb +240 -0
  13. data/doc/jamis.rb +591 -0
  14. data/flexmock.gemspec +33 -0
  15. data/lib/flexmock.rb +0 -1
  16. data/lib/flexmock/composite_expectation.rb +1 -1
  17. data/lib/flexmock/core.rb +3 -7
  18. data/lib/flexmock/core_class_methods.rb +5 -1
  19. data/lib/flexmock/default_framework_adapter.rb +2 -2
  20. data/lib/flexmock/expectation.rb +29 -3
  21. data/lib/flexmock/expectation_director.rb +1 -1
  22. data/lib/flexmock/minitest.rb +13 -0
  23. data/lib/flexmock/minitest_extensions.rb +26 -0
  24. data/lib/flexmock/minitest_integration.rb +111 -0
  25. data/lib/flexmock/mock_container.rb +1 -2
  26. data/lib/flexmock/partial_mock.rb +61 -104
  27. data/lib/flexmock/recorder.rb +1 -2
  28. data/lib/flexmock/rspec.rb +6 -3
  29. data/lib/flexmock/test_unit_integration.rb +14 -0
  30. data/lib/flexmock/validators.rb +5 -4
  31. data/lib/flexmock/version.rb +1 -9
  32. data/rakelib/metrics.rake +40 -0
  33. data/rakelib/preview.rake +4 -0
  34. data/rakelib/tags.rake +18 -0
  35. data/todo.txt +20 -0
  36. metadata +61 -86
  37. data/Gemfile.lock +0 -20
  38. data/doc/examples/rspec_examples_spec.rdoc +0 -245
  39. data/doc/examples/test_unit_examples_test.rdoc +0 -241
  40. data/test/aliasing_test.rb +0 -66
  41. data/test/assert_spy_called_test.rb +0 -119
  42. data/test/base_class_test.rb +0 -71
  43. data/test/based_partials_test.rb +0 -51
  44. data/test/container_methods_test.rb +0 -118
  45. data/test/default_framework_adapter_test.rb +0 -38
  46. data/test/demeter_mocking_test.rb +0 -191
  47. data/test/deprecated_methods_test.rb +0 -225
  48. data/test/examples_from_readme_test.rb +0 -157
  49. data/test/expectation_description_test.rb +0 -80
  50. data/test/extended_should_receive_test.rb +0 -69
  51. data/test/flexmodel_test.rb +0 -54
  52. data/test/mock_builder_test.rb +0 -68
  53. data/test/naming_test.rb +0 -84
  54. data/test/new_instances_test.rb +0 -215
  55. data/test/object_extensions_test.rb +0 -25
  56. data/test/partial_mock_test.rb +0 -458
  57. data/test/record_mode_test.rb +0 -158
  58. data/test/redirect_error.rb +0 -16
  59. data/test/rspec_integration/integration_spec.rb +0 -56
  60. data/test/rspec_integration/spy_example_spec.rb +0 -207
  61. data/test/samples_test.rb +0 -283
  62. data/test/should_ignore_missing_test.rb +0 -84
  63. data/test/should_receive_test.rb +0 -1155
  64. data/test/spys_test.rb +0 -215
  65. data/test/symbol_extensions_test.rb +0 -8
  66. data/test/test_class_extensions.rb +0 -34
  67. data/test/test_setup.rb +0 -92
  68. data/test/test_unit_integration/auto_test_unit_test.rb +0 -42
  69. data/test/test_unit_integration/minitest_teardown_test.rb +0 -14
  70. data/test/tu_integration_test.rb +0 -99
  71. data/test/undefined_test.rb +0 -87
@@ -1,84 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- #---
4
- # Copyright 2003-2013 by Jim Weirich (jim.weirich@gmail.com).
5
- # All rights reserved.
6
-
7
- # Permission is granted for use, copying, modification, distribution,
8
- # and distribution of modified versions of this work as long as the
9
- # above copyright notice is included.
10
- #+++
11
-
12
- require 'test/test_setup'
13
-
14
- class TestShouldIgnoreMissing < Test::Unit::TestCase
15
- include FlexMock::TestCase
16
-
17
- def setup
18
- @mock = flexmock("mock")
19
- end
20
-
21
- def test_mocks_do_not_respond_to_undefined_methods
22
- assert !@mock.respond_to?(:unknown_foo)
23
- end
24
-
25
- def test_mocks_do_respond_to_defined_methods
26
- @mock.should_receive(:known_foo => :bar)
27
- assert @mock.respond_to?(:known_foo)
28
- end
29
-
30
- def test_mocks_do_respond_to_any_method_when_ignoring_missing
31
- @mock.should_ignore_missing
32
- assert @mock.respond_to?(:unknown_foo)
33
- end
34
-
35
- def test_should_ignore_missing_returns_mock
36
- result = @mock.should_ignore_missing
37
- assert_equal result, @mock
38
- end
39
-
40
- def test_ignored_methods_return_undefined
41
- @mock.should_ignore_missing
42
- assert_equal FlexMock.undefined, @mock.unknown_foo
43
- @mock.unknown_foo.bar.baz.bleep
44
- end
45
-
46
- def test_undefined_mocking_with_arguments
47
- @mock.should_ignore_missing
48
- assert_equal FlexMock.undefined, @mock.xyzzy(1,:two,"three")
49
- end
50
-
51
- def test_method_chains_with_undefined_are_self_preserving
52
- @mock.should_ignore_missing
53
- assert_equal FlexMock.undefined, @mock.a.b.c.d.e.f(1).g.h.i.j
54
- end
55
-
56
- def test_method_proc_raises_error_on_unknown
57
- assert_raises(NameError) {
58
- @mock.method(:unknown_foo)
59
- }
60
- end
61
-
62
- def test_method_returns_callable_proc
63
- @mock.should_receive(:known_foo).once
64
- method_proc = @mock.method(:known_foo)
65
- assert_not_nil method_proc
66
- method_proc.call([])
67
- end
68
-
69
- def test_not_calling_method_proc_will_fail_count_constraints
70
- @mock.should_receive(:known_foo).once
71
- method_proc = @mock.method(:known_foo)
72
- assert_not_nil method_proc
73
- assert_raises assertion_failed_error do
74
- flexmock_teardown
75
- end
76
- end
77
-
78
- def test_method_returns_do_nothing_proc_for_missing_methods
79
- @mock.should_ignore_missing
80
- method_proc = @mock.method(:plugh)
81
- assert_not_nil method_proc
82
- assert_equal FlexMock.undefined, method_proc.call
83
- end
84
- end
@@ -1,1155 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- #---
4
- # Copyright 2003-2013 by Jim Weirich (jim.weirich@gmail.com).
5
- # All rights reserved.
6
-
7
- # Permission is granted for use, copying, modification, distribution,
8
- # and distribution of modified versions of this work as long as the
9
- # above copyright notice is included.
10
- #+++
11
-
12
- require 'test/test_setup'
13
-
14
- def mock_top_level_function
15
- :mtlf
16
- end
17
-
18
- module Kernel
19
- def mock_kernel_function
20
- :mkf
21
- end
22
- end
23
-
24
- # Used for testing
25
- class Cat
26
- def purr
27
- end
28
- def meow
29
- end
30
- end
31
-
32
- class TestFlexMockShoulds < Test::Unit::TestCase
33
- include FlexMock::TestCase
34
-
35
- # Expected error messages on failures
36
- COUNT_ERROR_MESSAGE = /\bcalled\s+incorrect\s+number\s+of\s+times\b/
37
- NO_MATCH_ERROR_MESSAGE = /\bno\s+matching\s+handler\b/
38
- AT_LEAST_ERROR_MESSAGE = COUNT_ERROR_MESSAGE
39
- AT_MOST_ERROR_MESSAGE = COUNT_ERROR_MESSAGE
40
- OUT_OF_ORDER_ERROR_MESSAGE = /\bcalled\s+out\s+of\s+order\b/
41
- NON_CONTAINER_MESSAGE = /\bis\s+not\s+in\s+a\s+container\b/
42
-
43
- def test_defaults
44
- FlexMock.use do |m|
45
- m.should_receive(:hi)
46
- assert_nil m.hi
47
- assert_nil m.hi(1)
48
- assert_nil m.hi("hello", 2)
49
- end
50
- end
51
-
52
- def test_returns_with_value
53
- FlexMock.use do |m|
54
- m.should_receive(:hi).returns(1)
55
- assert_equal 1, m.hi
56
- assert_equal 1, m.hi(123)
57
- end
58
- end
59
-
60
- def test_returns_with_multiple_values
61
- FlexMock.use do |m|
62
- m.should_receive(:hi).and_return(1,2,3)
63
- assert_equal 1, m.hi
64
- assert_equal 2, m.hi
65
- assert_equal 3, m.hi
66
- assert_equal 3, m.hi
67
- assert_equal 3, m.hi
68
- end
69
- end
70
-
71
- def test_multiple_returns
72
- FlexMock.use do |m|
73
- m.should_receive(:hi).and_return(1).and_return(2,3)
74
- assert_equal 1, m.hi
75
- assert_equal 2, m.hi
76
- assert_equal 3, m.hi
77
- assert_equal 3, m.hi
78
- assert_equal 3, m.hi
79
- end
80
- end
81
-
82
- def test_returns_with_block
83
- FlexMock.use do |m|
84
- result = nil
85
- m.should_receive(:hi).with(Object).returns { |obj| result = obj }
86
- m.hi(3)
87
- assert_equal 3, result
88
- end
89
- end
90
-
91
- def test_block_example_from_readme
92
- FlexMock.use do |m|
93
- m.should_receive(:foo).with(Integer,Proc).and_return(:got_block)
94
- m.should_receive(:foo).with(Integer).and_return(:no_block)
95
-
96
- assert_equal :no_block, m.foo(1)
97
- assert_equal :got_block, m.foo(1) { }
98
- end
99
- end
100
-
101
- def test_return_with_and_without_block_interleaved
102
- FlexMock.use do |m|
103
- m.should_receive(:hi).and_return(:a).and_return { :b }.and_return(:c)
104
- assert_equal :a, m.hi
105
- assert_equal :b, m.hi
106
- assert_equal :c, m.hi
107
- assert_equal :c, m.hi
108
- end
109
- end
110
-
111
- def test_and_returns_alias
112
- FlexMock.use do |m|
113
- m.should_receive(:hi).and_return(4)
114
- assert_equal 4, m.hi
115
- end
116
- end
117
-
118
- def test_and_return_undefined
119
- FlexMock.use do |m|
120
- m.should_receive(:foo).and_return_undefined
121
- m.should_receive(:phoo).returns_undefined
122
- assert_equal FlexMock.undefined, m.foo
123
- assert_equal FlexMock.undefined, m.foo.bar.baz.bing.ka_ching
124
- assert_equal FlexMock.undefined, m.phoo.bar.baz.bing.ka_ching
125
- end
126
- end
127
-
128
- def test_and_yield_will_continue_to_yield_the_same_value
129
- FlexMock.use do |m|
130
- m.should_receive(:hi).and_yield(:yield_value)
131
- assert_equal :yield_value, m.hi { |v| v }
132
- assert_equal :yield_value, m.hi { |v| v }
133
- end
134
- end
135
-
136
- def test_and_yield_with_multiple_values_yields_the_values
137
- FlexMock.use do |m|
138
- m.should_receive(:hi).and_yield(:one, :two).once
139
- assert_equal [:one, :two], m.hi { |a, b| [a, b] }
140
- end
141
- end
142
-
143
- def test_multiple_yields_are_done_sequentially
144
- FlexMock.use do |m|
145
- m.should_receive(:msg).and_yield(:one).and_yield(:two)
146
- assert_equal :one, m.msg { |a| a }
147
- assert_equal :two, m.msg { |a| a }
148
- assert_equal :two, m.msg { |a| a }
149
- end
150
- end
151
-
152
- def test_multiple_yields_and_multiple_returns_are_synced
153
- FlexMock.use do |m|
154
- m.should_receive(:msg).and_yield(:one).and_return(1).and_yield(:two).and_return(2)
155
- yielded_values = []
156
- returned_values = []
157
- returned_values << m.msg { |a| yielded_values << a }
158
- returned_values << m.msg { |a| yielded_values << a }
159
- returned_values << m.msg { |a| yielded_values << a }
160
- assert_equal [:one, :two, :two], yielded_values
161
- assert_equal [1, 2, 2], returned_values
162
- end
163
- end
164
-
165
- def test_failure_if_no_block_given
166
- FlexMock.use do |m|
167
- m.should_receive(:hi).and_yield(:one, :two).once
168
- assert_raise(FlexMock::MockError) do m.hi end
169
- end
170
- end
171
-
172
- def test_failure_different_return_value_than_yield_return
173
- FlexMock.use do |m|
174
- m.should_receive(:hi).and_yield(:yld).once.and_return(:ret)
175
- yielded_value = nil
176
- assert_equal :ret, m.hi { |v| yielded_value = v }
177
- assert_equal :yld, yielded_value
178
- end
179
- end
180
-
181
- def test_multiple_yields
182
- FlexMock.use do |m|
183
- m.should_receive(:hi).and_yield(:one, :two).and_yield(1, 2)
184
- assert_equal [:one, :two], m.hi { |a, b| [a, b] }
185
- assert_equal [1, 2], m.hi { |a, b| [a, b] }
186
- end
187
- end
188
-
189
- def test_multiple_yields_will_yield_the_last_value_set
190
- FlexMock.use do |m|
191
- m.should_receive(:hi).and_yield(:a).and_yield(:b)
192
- assert_equal [:a], m.hi { |a, b| [a] }
193
- assert_equal [:b], m.hi { |a, b| [a] }
194
- assert_equal [:b], m.hi { |a, b| [a] }
195
- assert_equal [:b], m.hi { |a, b| [a] }
196
- assert_equal [:b], m.hi { |a, b| [a] }
197
- end
198
- end
199
-
200
- def test_yielding_then_not_yielding_and_then_yielding_again
201
- FlexMock.use do |m|
202
- m.should_receive(:hi).and_yield(:a).once
203
- m.should_receive(:hi).and_return(:b).once
204
- m.should_receive(:hi).and_yield(:c).once
205
- assert_equal :a, m.hi { |v| v }
206
- assert_equal :b, m.hi
207
- assert_equal :c, m.hi { |v| v }
208
- end
209
- end
210
-
211
- def test_yields_syntax
212
- FlexMock.use do |m|
213
- m.should_receive(:hi).yields(:one)
214
- assert_equal :one, m.hi { |a| a }
215
- end
216
- end
217
-
218
- class MyError < RuntimeError
219
- end
220
-
221
- def test_and_raises_with_exception_class_throws_exception
222
- FlexMock.use do |m|
223
- m.should_receive(:failure).and_raise(MyError)
224
- assert_raise MyError do
225
- m.failure
226
- end
227
- end
228
- end
229
-
230
- def test_and_raises_with_arguments_throws_exception_made_with_args
231
- FlexMock.use do |m|
232
- m.should_receive(:failure).and_raise(MyError, "my message")
233
- ex = assert_raise MyError do
234
- m.failure
235
- end
236
- assert_equal "my message", ex.message
237
- end
238
- end
239
-
240
- def test_and_raises_with_a_specific_exception_throws_the_exception
241
- FlexMock.use do |m|
242
- err = MyError.new
243
- m.should_receive(:failure).and_raise(err)
244
- ex = assert_raise MyError do
245
- m.failure
246
- end
247
- assert_equal err, ex
248
- end
249
- end
250
-
251
- def test_raises_is_an_alias_for_and_raise
252
- FlexMock.use do |m|
253
- m.should_receive(:failure).raises(RuntimeError)
254
- assert_raise RuntimeError do
255
- m.failure
256
- end
257
- end
258
- end
259
-
260
- def test_multiple_and_raise_clauses_will_be_done_sequentially
261
- FlexMock.use do |m|
262
- m.should_receive(:failure).
263
- and_raise(RuntimeError, "ONE").
264
- and_raise(RuntimeError, "TWO")
265
- ex = assert_raise RuntimeError do m.failure end
266
- assert_equal "ONE", ex.message
267
- ex = assert_raise RuntimeError do m.failure end
268
- assert_equal "TWO", ex.message
269
- end
270
- end
271
-
272
- def test_and_throw_will_throw_a_symbol
273
- FlexMock.use do |m|
274
- m.should_receive(:msg).and_throw(:sym)
275
- value = catch(:sym) do
276
- m.msg
277
- fail "Should not reach this line"
278
- end
279
- assert_nil value
280
- end
281
- end
282
-
283
- def test_and_throw_with_expression_will_throw
284
- FlexMock.use do |m|
285
- m.should_receive(:msg).and_throw(:sym, :return_value)
286
- value = catch(:sym) do
287
- m.msg
288
- fail "Should not reach this line"
289
- end
290
- assert_equal :return_value, value
291
- end
292
- end
293
-
294
- def test_throws_is_an_alias_for_and_throw
295
- FlexMock.use do |m|
296
- m.should_receive(:msg).throws(:sym, :return_value)
297
- value = catch(:sym) do
298
- m.msg
299
- fail "Should not reach this line"
300
- end
301
- assert_equal :return_value, value
302
- end
303
- end
304
-
305
- def test_multiple_throws_will_be_done_sequentially
306
- FlexMock.use do |m|
307
- m.should_receive(:toss).
308
- and_throw(:sym, "ONE").
309
- and_throw(:sym, "TWO")
310
- value = catch(:sym) do m.toss end
311
- assert_equal "ONE", value
312
- value = catch(:sym) do m.toss end
313
- assert_equal "TWO", value
314
- end
315
- end
316
-
317
- def test_pass_thru_just_returns_undefined_on_mocks
318
- FlexMock.use do |m|
319
- m.should_receive(:hi).pass_thru
320
- assert_equal FlexMock.undefined, m.hi
321
- end
322
- end
323
-
324
- def test_multiple_expectations
325
- FlexMock.use do |m|
326
- m.should_receive(:hi).with(1).returns(10)
327
- m.should_receive(:hi).with(2).returns(20)
328
-
329
- assert_equal 10, m.hi(1)
330
- assert_equal 20, m.hi(2)
331
- end
332
- end
333
-
334
- def test_with_no_args_with_no_args
335
- FlexMock.use do |m|
336
- m.should_receive(:hi).with_no_args
337
- m.hi
338
- end
339
- end
340
-
341
- def test_with_no_args_but_with_args
342
- assert_mock_failure(:message =>NO_MATCH_ERROR_MESSAGE, :deep => true) do
343
- FlexMock.use do |m|
344
- m.should_receive(:hi).with_no_args
345
- m.hi(1)
346
- end
347
- end
348
- end
349
-
350
- def test_with_any_args
351
- FlexMock.use do |m|
352
- m.should_receive(:hi).with_any_args
353
- m.hi
354
- m.hi(1)
355
- m.hi(1,2,3)
356
- m.hi("this is a test")
357
- end
358
- end
359
-
360
- def test_with_any_single_arg_matching
361
- FlexMock.use('greeter') do |m|
362
- m.should_receive(:hi).with(1,FlexMock.any).twice
363
- m.hi(1,2)
364
- m.hi(1, "this is a test")
365
- end
366
- end
367
-
368
- def test_with_any_single_arg_nonmatching
369
- FlexMock.use('greeter') do |m|
370
- m.should_receive(:hi).times(3)
371
- m.should_receive(:hi).with(1,FlexMock.any).never
372
- m.hi
373
- m.hi(1)
374
- m.hi(1, "hi", nil)
375
- end
376
- end
377
-
378
- def test_with_equal_arg_matching
379
- FlexMock.use('greeter') do |m|
380
- m.should_receive(:hi).with(FlexMock.eq(Object)).once
381
- m.hi(Object)
382
- end
383
- end
384
-
385
- def test_with_ducktype_arg_matching
386
- FlexMock.use('greeter') do |m|
387
- m.should_receive(:hi).with(FlexMock.ducktype(:purr, :meow)).once
388
- m.hi(Cat.new)
389
- end
390
- end
391
-
392
- def test_with_ducktype_arg_matching_no_match
393
- FlexMock.use('greeter') do |m|
394
- m.should_receive(:hi).with(FlexMock.ducktype(:purr, :meow, :growl))
395
- assert_mock_failure(:deep => true, :line => __LINE__+1) {
396
- m.hi(Cat.new)
397
- }
398
- end
399
- end
400
-
401
- def test_with_hash_matching
402
- FlexMock.use('greeter') do |m|
403
- m.should_receive(:hi).with(FlexMock.hsh(:a => 1, :b => 2)).once
404
- m.hi(:a => 1, :b => 2, :c => 3)
405
- end
406
- end
407
-
408
- def test_with_hash_non_matching
409
- FlexMock.use('greeter') do |m|
410
- m.should_receive(:hi).with(FlexMock.hsh(:a => 1, :b => 2))
411
- assert_mock_failure(:deep => true, :line => __LINE__+1) {
412
- m.hi(:a => 1, :b => 4, :c => 3)
413
- }
414
- end
415
- end
416
-
417
- def test_with_equal_arg_nonmatching
418
- FlexMock.use('greeter') do |m|
419
- m.should_receive(:hi).with(FlexMock.eq(Object)).never
420
- m.should_receive(:hi).never
421
- m.should_receive(:hi).with(1).once
422
- m.hi(1)
423
- end
424
- end
425
-
426
- def test_with_optional_proc
427
- FlexMock.use('greeter') do |m|
428
- m.should_receive(:hi).with(optional_proc).once
429
- m.hi { }
430
- end
431
- end
432
-
433
- def test_with_optional_proc_and_missing_proc
434
- FlexMock.use('greeter') do |m|
435
- m.should_receive(:hi).with(optional_proc).once
436
- m.hi
437
- end
438
- end
439
-
440
- def test_with_optional_proc_distinquishes_between_nil_and_missing
441
- FlexMock.use('greeter') do |m|
442
- m.should_receive(:hi).with(optional_proc).never
443
- m.should_receive(:hi).with(nil).once
444
- m.hi(nil)
445
- end
446
- end
447
-
448
- def test_with_arbitrary_arg_matching
449
- FlexMock.use('greeter') do |m|
450
- m.should_receive(:hi).with(FlexMock.on { |arg| arg % 2 == 0 rescue nil }).twice
451
- m.should_receive(:hi).never
452
- m.should_receive(:hi).with(1).once
453
- m.should_receive(:hi).with(2).never
454
- m.should_receive(:hi).with(3).once
455
- m.should_receive(:hi).with(4).never
456
- m.hi(1)
457
- m.hi(2)
458
- m.hi(3)
459
- m.hi(4)
460
- end
461
- end
462
-
463
- def test_args_matching_with_regex
464
- FlexMock.use do |m|
465
- m.should_receive(:hi).with(/one/).returns(10)
466
- m.should_receive(:hi).with(/t/).returns(20)
467
-
468
- assert_equal 10, m.hi("one")
469
- assert_equal 10, m.hi("done")
470
- assert_equal 20, m.hi("two")
471
- assert_equal 20, m.hi("three")
472
- end
473
- end
474
-
475
- def test_arg_matching_with_regex_matching_non_string
476
- FlexMock.use do |m|
477
- m.should_receive(:hi).with(/1/).returns(10)
478
- assert_equal 10, m.hi(319)
479
- end
480
- end
481
-
482
- def test_arg_matching_with_class
483
- FlexMock.use do |m|
484
- m.should_receive(:hi).with(Fixnum).returns(10)
485
- m.should_receive(:hi).with(Object).returns(20)
486
-
487
- assert_equal 10, m.hi(319)
488
- assert_equal 10, m.hi(Fixnum)
489
- assert_equal 20, m.hi("hi")
490
- end
491
- end
492
-
493
- def test_arg_matching_with_no_match
494
- FlexMock.use do |m|
495
- m.should_receive(:hi).with(1).returns(10)
496
- assert_mock_failure(:message =>NO_MATCH_ERROR_MESSAGE, :deep => true, :line => __LINE__+1) {
497
- m.hi(2)
498
- }
499
- end
500
- end
501
-
502
- def test_arg_matching_with_string_doesnt_over_match
503
- FlexMock.use do |m|
504
- m.should_receive(:hi).with(String).returns(20)
505
- assert_mock_failure(:message =>NO_MATCH_ERROR_MESSAGE, :deep => true, :line => __LINE__+1) {
506
- m.hi(1.0)
507
- }
508
- end
509
- end
510
-
511
- def test_block_arg_given_to_no_args
512
- FlexMock.use do |m|
513
- m.should_receive(:hi).with_no_args.returns(20)
514
- assert_mock_failure(:message =>NO_MATCH_ERROR_MESSAGE, :deep => true, :line => __LINE__+1) {
515
- m.hi { 1 }
516
- }
517
- end
518
- end
519
-
520
- def test_block_arg_given_to_matching_proc
521
- FlexMock.use do |m|
522
- arg = nil
523
- m.should_receive(:hi).with(Proc).once.
524
- and_return { |block| arg = block; block.call }
525
- result = m.hi { 1 }
526
- assert_equal 1, arg.call
527
- assert_equal 1, result
528
- end
529
- end
530
-
531
- def test_arg_matching_precedence_when_best_first
532
- FlexMock.use("greeter") do |m|
533
- m.should_receive(:hi).with(1).once
534
- m.should_receive(:hi).with(FlexMock.any).never
535
- m.hi(1)
536
- end
537
- end
538
-
539
- def test_arg_matching_precedence_when_best_last_but_still_matches_first
540
- FlexMock.use("greeter") do |m|
541
- m.should_receive(:hi).with(FlexMock.any).once
542
- m.should_receive(:hi).with(1).never
543
- m.hi(1)
544
- end
545
- end
546
-
547
- def test_never_and_never_called
548
- FlexMock.use do |m|
549
- m.should_receive(:hi).with(1).never
550
- end
551
- end
552
-
553
- def test_never_and_called_once
554
- assert_mock_failure(:message =>COUNT_ERROR_MESSAGE, :deep => true, :line => __LINE__+2) do
555
- FlexMock.use do |m|
556
- m.should_receive(:hi).with(1).never
557
- m.hi(1)
558
- end
559
- end
560
- end
561
-
562
- def test_once_called_once
563
- FlexMock.use do |m|
564
- m.should_receive(:hi).with(1).returns(10).once
565
- m.hi(1)
566
- end
567
- end
568
-
569
- def test_once_but_never_called
570
- assert_mock_failure(:message =>COUNT_ERROR_MESSAGE, :line => __LINE__+2) do
571
- FlexMock.use do |m|
572
- m.should_receive(:hi).with(1).returns(10).once
573
- end
574
- end
575
- end
576
-
577
- def test_once_but_called_twice
578
- assert_mock_failure(:message =>COUNT_ERROR_MESSAGE, :line => __LINE__+2) do
579
- FlexMock.use do |m|
580
- m.should_receive(:hi).with(1).returns(10).once
581
- m.hi(1)
582
- m.hi(1)
583
- end
584
- end
585
- end
586
-
587
- def test_twice_and_called_twice
588
- FlexMock.use do |m|
589
- m.should_receive(:hi).with(1).returns(10).twice
590
- m.hi(1)
591
- m.hi(1)
592
- end
593
- end
594
-
595
- def test_zero_or_more_called_zero
596
- FlexMock.use do |m|
597
- m.should_receive(:hi).zero_or_more_times
598
- end
599
- end
600
-
601
- def test_zero_or_more_called_once
602
- FlexMock.use do |m|
603
- m.should_receive(:hi).zero_or_more_times
604
- m.hi
605
- end
606
- end
607
-
608
- def test_zero_or_more_called_100
609
- FlexMock.use do |m|
610
- m.should_receive(:hi).zero_or_more_times
611
- 100.times { m.hi }
612
- end
613
- end
614
-
615
- def test_times
616
- FlexMock.use do |m|
617
- m.should_receive(:hi).with(1).returns(10).times(10)
618
- 10.times { m.hi(1) }
619
- end
620
- end
621
-
622
- def test_at_least_called_once
623
- FlexMock.use do |m|
624
- m.should_receive(:hi).with(1).returns(10).at_least.once
625
- m.hi(1)
626
- end
627
- end
628
-
629
- def test_at_least_but_never_called
630
- assert_mock_failure(:message =>AT_LEAST_ERROR_MESSAGE, :line => __LINE__+2) do
631
- FlexMock.use do |m|
632
- m.should_receive(:hi).with(1).returns(10).at_least.once
633
- end
634
- end
635
- end
636
-
637
- def test_at_least_once_but_called_twice
638
- FlexMock.use do |m|
639
- m.should_receive(:hi).with(1).returns(10).at_least.once
640
- m.hi(1)
641
- m.hi(1)
642
- end
643
- end
644
-
645
- def test_at_least_and_exact
646
- assert_mock_failure(:message =>COUNT_ERROR_MESSAGE, :line => __LINE__+2) do
647
- FlexMock.use do |m|
648
- m.should_receive(:hi).with(1).returns(10).at_least.once.once
649
- m.hi(1)
650
- m.hi(1)
651
- end
652
- end
653
- end
654
-
655
- def test_at_most_but_never_called
656
- FlexMock.use do |m|
657
- m.should_receive(:hi).with(1).returns(10).at_most.once
658
- end
659
- end
660
-
661
- def test_at_most_called_once
662
- FlexMock.use do |m|
663
- m.should_receive(:hi).with(1).returns(10).at_most.once
664
- m.hi(1)
665
- end
666
- end
667
-
668
- def test_at_most_called_twice
669
- ex = assert_mock_failure(:message =>AT_MOST_ERROR_MESSAGE, :line => __LINE__+2) do
670
- FlexMock.use do |m|
671
- m.should_receive(:hi).with(1).returns(10).at_most.once
672
- m.hi(1)
673
- m.hi(1)
674
- end
675
- end
676
- assert_match(/at most 1/i, ex.message)
677
- end
678
-
679
- def test_at_most_and_at_least_called_never
680
- ex = assert_mock_failure(:message =>AT_LEAST_ERROR_MESSAGE, :line => __LINE__+2) do
681
- FlexMock.use do |m|
682
- m.should_receive(:hi).with(1).returns(10).at_least.once.at_most.twice
683
- end
684
- end
685
- assert_match(/at least 1/i, ex.message)
686
- end
687
-
688
- def test_at_most_and_at_least_called_once
689
- FlexMock.use do |m|
690
- m.should_receive(:hi).with(1).returns(10).at_least.once.at_most.twice
691
- m.hi(1)
692
- end
693
- end
694
-
695
- def test_at_most_and_at_least_called_twice
696
- FlexMock.use do |m|
697
- m.should_receive(:hi).with(1).returns(10).at_least.once.at_most.twice
698
- m.hi(1)
699
- m.hi(1)
700
- end
701
- end
702
-
703
- def test_at_most_and_at_least_called_three_times
704
- assert_mock_failure(:message =>AT_MOST_ERROR_MESSAGE, :line => __LINE__+2) do
705
- FlexMock.use do |m|
706
- m.should_receive(:hi).with(1).returns(10).at_least.once.at_most.twice
707
- m.hi(1)
708
- m.hi(1)
709
- m.hi(1)
710
- end
711
- end
712
- end
713
-
714
- def test_call_counts_only_apply_to_matching_args
715
- FlexMock.use do |m|
716
- m.should_receive(:hi).with(1).once
717
- m.should_receive(:hi).with(2).twice
718
- m.should_receive(:hi).with(3)
719
- m.hi(1)
720
- m.hi(2)
721
- m.hi(2)
722
- 20.times { m.hi(3) }
723
- end
724
- end
725
-
726
- def test_call_counts_only_apply_to_matching_args_with_mismatch
727
- ex = assert_mock_failure(:message =>COUNT_ERROR_MESSAGE, :line => __LINE__+3) do
728
- FlexMock.use do |m|
729
- m.should_receive(:hi).with(1).once
730
- m.should_receive(:hi).with(2).twice
731
- m.should_receive(:hi).with(3)
732
- m.should_receive(:lo)
733
- m.hi(1)
734
- m.hi(2)
735
- m.lo
736
- 20.times { m.hi(3) }
737
- end
738
- end
739
- assert_match(/hi\(2\)/, ex.message)
740
- end
741
-
742
- def test_ordered_calls_in_order_will_pass
743
- FlexMock.use 'm' do |m|
744
- m.should_receive(:hi).ordered
745
- m.should_receive(:lo).ordered
746
-
747
- m.hi
748
- m.lo
749
- end
750
- end
751
-
752
- def test_ordered_calls_out_of_order_will_fail
753
- assert_mock_failure(:message =>OUT_OF_ORDER_ERROR_MESSAGE, :deep => true, :line => __LINE__+6) do
754
- FlexMock.use 'm' do |m|
755
- m.should_receive(:hi).ordered
756
- m.should_receive(:lo).ordered
757
-
758
- m.lo
759
- m.hi
760
- end
761
- end
762
- end
763
-
764
- def test_order_calls_with_different_arg_lists_and_in_order_will_pass
765
- FlexMock.use 'm' do |m|
766
- m.should_receive(:hi).with("one").ordered
767
- m.should_receive(:hi).with("two").ordered
768
-
769
- m.hi("one")
770
- m.hi("two")
771
- end
772
- end
773
-
774
- def test_order_calls_with_different_arg_lists_and_out_of_order_will_fail
775
- assert_mock_failure(:message =>OUT_OF_ORDER_ERROR_MESSAGE, :deep => true, :line => __LINE__+6) do
776
- FlexMock.use 'm' do |m|
777
- m.should_receive(:hi).with("one").ordered
778
- m.should_receive(:hi).with("two").ordered
779
-
780
- m.hi("two")
781
- m.hi("one")
782
- end
783
- end
784
- end
785
-
786
- def test_unordered_calls_do_not_effect_ordered_testing
787
- FlexMock.use 'm' do |m|
788
- m.should_receive(:blah)
789
- m.should_receive(:hi).ordered
790
- m.should_receive(:lo).ordered
791
-
792
- m.blah
793
- m.hi
794
- m.blah
795
- m.lo
796
- m.blah
797
- end
798
- end
799
-
800
- def test_ordered_with_multiple_calls_will_pass
801
- FlexMock.use 'm' do |m|
802
- m.should_receive(:hi).ordered
803
- m.should_receive(:lo).ordered
804
-
805
- m.hi
806
- m.hi
807
- m.lo
808
- m.lo
809
- end
810
- end
811
-
812
- def test_grouped_ordering_with_numbers
813
- FlexMock.use 'm' do |m|
814
- m.should_receive(:start).ordered(1)
815
- m.should_receive(:flip).ordered(2)
816
- m.should_receive(:flop).ordered(2)
817
- m.should_receive(:final).ordered
818
-
819
- m.start
820
- m.flop
821
- m.flip
822
- m.flop
823
- m.final
824
- end
825
- end
826
-
827
- def test_grouped_ordering_with_symbols
828
- FlexMock.use 'm' do |m|
829
- m.should_receive(:start).ordered(:start_group)
830
- m.should_receive(:flip).ordered(:flip_flop_group)
831
- m.should_receive(:flop).ordered(:flip_flop_group)
832
- m.should_receive(:final).ordered
833
-
834
- m.start
835
- m.flop
836
- m.flip
837
- m.flop
838
- m.final
839
- end
840
- end
841
-
842
- def test_explicit_ordering_mixed_with_implicit_ordering_should_not_overlap
843
- FlexMock.use 'm' do |m|
844
- xstart = m.should_receive(:start).ordered
845
- xmid = m.should_receive(:mid).ordered(:group_name)
846
- xend = m.should_receive(:end).ordered
847
- assert xstart.order_number < xmid.order_number
848
- assert xmid.order_number < xend.order_number
849
- end
850
- end
851
-
852
- def test_explicit_ordering_with_explicit_misorders
853
- assert_mock_failure(:message =>OUT_OF_ORDER_ERROR_MESSAGE, :deep => true, :line => __LINE__+6) do
854
- FlexMock.use 'm' do |m|
855
- m.should_receive(:hi).ordered(:first_group)
856
- m.should_receive(:lo).ordered(:second_group)
857
-
858
- m.lo
859
- m.hi
860
- end
861
- end
862
- # TODO: It would be nice to get the group names in the error message.
863
- # assert_match /first_group/, ex.message
864
- # assert_match /second_group/, ex.message
865
- end
866
-
867
- # Test submitted by Mikael Pahmp to correct expectation matching.
868
- def test_ordering_with_explicit_no_args_matches_correctly
869
- FlexMock.use("m") do |m|
870
- m.should_receive(:foo).with_no_args.once.ordered
871
- m.should_receive(:bar).with_no_args.once.ordered
872
- m.should_receive(:foo).with_no_args.once.ordered
873
- m.foo
874
- m.bar
875
- m.foo
876
- end
877
- end
878
-
879
- # Test submitted by Mikael Pahmp to correct expectation matching.
880
- def test_ordering_with_any_arg_matching_correctly_matches
881
- FlexMock.use("m") do |m|
882
- m.should_receive(:foo).with_any_args.once.ordered
883
- m.should_receive(:bar).with_any_args.once.ordered
884
- m.should_receive(:foo).with_any_args.once.ordered
885
- m.foo
886
- m.bar
887
- m.foo
888
- end
889
- end
890
-
891
- def test_ordering_between_mocks_is_not_normally_defined
892
- FlexMock.use("x", "y") do |x, y|
893
- x.should_receive(:one).ordered
894
- y.should_receive(:two).ordered
895
-
896
- assert_nothing_raised do
897
- y.two
898
- x.one
899
- end
900
- end
901
- end
902
-
903
- def test_ordering_between_mocks_is_honored_for_global_ordering
904
- assert_mock_failure(:message =>OUT_OF_ORDER_ERROR_MESSAGE, :deep => true, :line => __LINE__+6) do
905
- FlexMock.use("x", "y") do |x, y|
906
- x.should_receive(:one).globally.ordered
907
- y.should_receive(:two).globally.ordered
908
-
909
- y.two
910
- x.one
911
- end
912
- end
913
- end
914
-
915
- def test_expectation_formating
916
- mock = flexmock("m")
917
- exp = mock.should_receive(:f).with(1,"two", /^3$/).
918
- and_return(0).at_least.once
919
-
920
- mock.f(1, "two", 3)
921
- assert_equal 'f(1, "two", /^3$/)', exp.to_s
922
- end
923
-
924
- def test_multi_expectation_formatting
925
- mock = flexmock("mock")
926
- exp = mock.should_receive(:f, :g).with(1)
927
- assert_equal "[f(1), g(1)]", exp.to_s
928
- end
929
-
930
- def test_explicit_ordering_with_limits_allow_multiple_return_values
931
- FlexMock.use('mock') do |m|
932
- m.should_receive(:f).with(2).once.and_return { :first_time }
933
- m.should_receive(:f).with(2).twice.and_return { :second_or_third_time }
934
- m.should_receive(:f).with(2).and_return { :forever }
935
-
936
- assert_equal :first_time, m.f(2)
937
- assert_equal :second_or_third_time, m.f(2)
938
- assert_equal :second_or_third_time, m.f(2)
939
- assert_equal :forever, m.f(2)
940
- assert_equal :forever, m.f(2)
941
- assert_equal :forever, m.f(2)
942
- assert_equal :forever, m.f(2)
943
- assert_equal :forever, m.f(2)
944
- assert_equal :forever, m.f(2)
945
- assert_equal :forever, m.f(2)
946
- end
947
- end
948
-
949
- def test_global_methods_can_be_mocked
950
- m = flexmock("m")
951
- m.should_receive(:mock_top_level_function).and_return(:mock)
952
- assert_equal :mock, m.mock_top_level_function
953
- end
954
-
955
- def test_kernel_methods_can_be_mocked
956
- m = flexmock("m")
957
- m.should_receive(:mock_kernel_function).and_return(:mock)
958
- assert_equal :mock, m.mock_kernel_function
959
- end
960
-
961
- def test_undefing_kernel_methods_dont_effect_other_mocks
962
- m = flexmock("m")
963
- m2 = flexmock("m2")
964
- m.should_receive(:mock_kernel_function).and_return(:mock)
965
- assert_equal :mock, m.mock_kernel_function
966
- assert_equal :mkf, m2.mock_kernel_function
967
- end
968
-
969
- def test_expectations_can_by_marked_as_default
970
- m = flexmock("m")
971
- m.should_receive(:foo).and_return(:bar).by_default
972
- assert_equal :bar, m.foo
973
- end
974
-
975
- def test_default_expectations_are_search_in_the_proper_order
976
- m = flexmock("m")
977
- m.should_receive(:foo).with(Integer).once.and_return(:first).by_default
978
- m.should_receive(:foo).with(1).and_return(:second).by_default
979
- assert_equal :first, m.foo(1)
980
- assert_equal :second, m.foo(1)
981
- end
982
-
983
- def test_expectations_with_count_constraints_can_by_marked_as_default
984
- m = flexmock("m")
985
- m.should_receive(:foo).and_return(:bar).once.by_default
986
- assert_raise assertion_failed_error do
987
- flexmock_teardown
988
- end
989
- end
990
-
991
- def test_default_expectations_are_overridden_by_later_expectations
992
- m = flexmock("m")
993
- m.should_receive(:foo).and_return(:bar).once.by_default
994
- m.should_receive(:foo).and_return(:bar).twice
995
- m.foo
996
- m.foo
997
- end
998
-
999
- def test_default_expectations_can_be_changed_by_later_expectations
1000
- m = flexmock("m")
1001
- m.should_receive(:foo).with(1).and_return(:bar).once.by_default
1002
- m.should_receive(:foo).with(2).and_return(:baz).once
1003
- assert_raise assertion_failed_error do
1004
- # This expectation should be hidded by the non-result
1005
- m.foo(1)
1006
- end
1007
- m.foo(2)
1008
- end
1009
-
1010
- def test_ordered_default_expectations_can_be_specified
1011
- m = flexmock("m")
1012
- m.should_receive(:foo).ordered.by_default
1013
- m.should_receive(:bar).ordered.by_default
1014
- m.bar
1015
- assert_raise assertion_failed_error do m.foo end
1016
- end
1017
-
1018
- def test_ordered_default_expectations_can_be_overridden
1019
- m = flexmock("m")
1020
- m.should_receive(:foo).ordered.by_default
1021
- m.should_receive(:bar).ordered.by_default
1022
-
1023
- m.should_receive(:bar).ordered
1024
- m.should_receive(:foo).ordered
1025
-
1026
- m.bar
1027
- m.foo
1028
- end
1029
-
1030
- def test_by_default_works_at_mock_level
1031
- m = flexmock("m",
1032
- :foo => :bar,
1033
- :pooh => :bear,
1034
- :who => :dey).by_default
1035
- m.should_receive(:pooh => :winnie)
1036
- assert_equal :bar, m.foo
1037
- assert_equal :dey, m.who
1038
- assert_equal :winnie, m.pooh
1039
- end
1040
-
1041
- def test_by_default_at_mock_level_does_nothing_with_no_expectations
1042
- assert_nothing_raised do
1043
- flexmock("m").by_default
1044
- end
1045
- end
1046
-
1047
- def test_partial_mocks_can_have_default_expectations
1048
- obj = Object.new
1049
- flexmock(obj).should_receive(:foo).and_return(:bar).by_default
1050
- assert_equal :bar, obj.foo
1051
- end
1052
-
1053
- def test_partial_mocks_can_have_default_expectations_overridden
1054
- obj = Object.new
1055
- flexmock(obj).should_receive(:foo).and_return(:bar).by_default
1056
- flexmock(obj).should_receive(:foo).and_return(:baz)
1057
- assert_equal :baz, obj.foo
1058
- end
1059
-
1060
- def test_wicked_and_evil_tricks_with_by_default_are_thwarted
1061
- mock = flexmock("mock")
1062
- exp = mock.should_receive(:foo).and_return(:first).once
1063
- mock.should_receive(:foo).and_return(:second)
1064
- ex = assert_raise(FlexMock::UsageError) do
1065
- exp.by_default
1066
- end
1067
- assert_match %r(previously defined), ex.message
1068
- assert_equal :first, mock.foo
1069
- assert_equal :second, mock.foo
1070
- end
1071
-
1072
- def test_mocks_can_handle_multi_parameter_respond_tos
1073
- mock = flexmock("a mock", :foo => :bar)
1074
- assert mock.respond_to?(:foo)
1075
- assert mock.respond_to?(:foo, true)
1076
- assert mock.respond_to?(:foo, false)
1077
-
1078
- assert ! mock.respond_to?(:phoo)
1079
- assert ! mock.respond_to?(:phoo, false)
1080
- assert ! mock.respond_to?(:phoo, true)
1081
- end
1082
-
1083
- def test_backtraces_point_to_should_receive_line
1084
- mock = flexmock("a mock")
1085
- file_name_re = Regexp.quote(__FILE__)
1086
- line_no = __LINE__ + 1
1087
- mock.should_receive(:foo).and_return(:bar).once
1088
- begin
1089
- flexmock_verify
1090
- rescue Exception => ex
1091
- exception = ex
1092
- end
1093
- assert_not_nil exception
1094
- assert_match(/#{file_name_re}:#{line_no}/, exception.backtrace.first)
1095
- end
1096
-
1097
- def test_can_mock_operators
1098
- assert_operator(:[]) { |m| m[1] }
1099
- assert_operator(:[]=) { |m| m[1] = :value }
1100
- assert_operator(:**) { |m| m ** :x }
1101
- assert_operator(:+@) { |m| +m }
1102
- assert_operator(:-@) { |m| -m }
1103
- assert_operator(:+) { |m| m + :x }
1104
- assert_operator(:-) { |m| m - :x }
1105
- assert_operator(:*) { |m| m * :x }
1106
- assert_operator(:"/") { |m| m / :x }
1107
- assert_operator(:%) { |m| m % :x }
1108
- assert_operator(:~) { |m| ~m } # )
1109
- assert_operator(:&) { |m| m & :x }
1110
- assert_operator(:|) { |m| m | :x }
1111
- assert_operator(:^) { |m| m ^ :x }
1112
- assert_operator(:<) { |m| m < :x }
1113
- assert_operator(:>) { |m| m > :x }
1114
- assert_operator(:>=) { |m| m >= :x }
1115
- assert_operator(:<=) { |m| m <= :x }
1116
- assert_operator(:==) { |m| m == :x }
1117
- assert_operator(:===) { |m| m === :x }
1118
- assert_operator(:<<) { |m| m << :x }
1119
- assert_operator(:>>) { |m| m >> :x }
1120
- assert_operator(:<=>) { |m| m <=> :x }
1121
- assert_operator(:=~) { |m| m =~ :x }
1122
- assert_operator(:"`") { |m| m.`("command") } # `
1123
- end
1124
-
1125
- private
1126
-
1127
- def assert_operator(op, &block)
1128
- m = flexmock("mock")
1129
- m.should_receive(op).and_return(:value)
1130
- assert_equal :value, block.call(m)
1131
- end
1132
-
1133
- end
1134
-
1135
- class TestFlexMockShouldsWithInclude < Test::Unit::TestCase
1136
- include FlexMock::ArgumentTypes
1137
- def test_include_enables_unqualified_arg_type_references
1138
- FlexMock.use("x") do |m|
1139
- m.should_receive(:hi).with(any).once
1140
- m.hi(1)
1141
- end
1142
- end
1143
- end
1144
-
1145
- class TestFlexMockArgTypesDontLeak < Test::Unit::TestCase
1146
- def test_unqualified_arg_type_references_are_undefined_by_default
1147
- ex = assert_raise(NameError) do
1148
- FlexMock.use("x") do |m|
1149
- m.should_receive(:hi).with(any).once
1150
- m.hi(1)
1151
- end
1152
- end
1153
- assert_match(/\bany\b/, ex.message, "Error message should mention 'any'")
1154
- end
1155
- end