mocha 0.5.6 → 0.9.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 (120) hide show
  1. data/README +4 -4
  2. data/RELEASE +45 -0
  3. data/Rakefile +55 -33
  4. data/lib/mocha.rb +1 -0
  5. data/lib/mocha/any_instance_method.rb +24 -4
  6. data/lib/mocha/backtrace_filter.rb +17 -0
  7. data/lib/mocha/cardinality.rb +92 -0
  8. data/lib/mocha/central.rb +1 -9
  9. data/lib/mocha/change_state_side_effect.rb +19 -0
  10. data/lib/mocha/class_method.rb +25 -5
  11. data/lib/mocha/configuration.rb +60 -0
  12. data/lib/mocha/exception_raiser.rb +1 -1
  13. data/lib/mocha/expectation.rb +109 -48
  14. data/lib/mocha/expectation_error.rb +6 -6
  15. data/lib/mocha/expectation_list.rb +10 -14
  16. data/lib/mocha/in_state_ordering_constraint.rb +19 -0
  17. data/lib/mocha/instance_method.rb +9 -0
  18. data/lib/mocha/logger.rb +15 -0
  19. data/lib/mocha/mock.rb +19 -14
  20. data/lib/mocha/mockery.rb +166 -0
  21. data/lib/mocha/module_method.rb +17 -0
  22. data/lib/mocha/names.rb +53 -0
  23. data/lib/mocha/object.rb +26 -9
  24. data/lib/mocha/parameter_matchers.rb +2 -1
  25. data/lib/mocha/parameter_matchers/all_of.rb +3 -3
  26. data/lib/mocha/parameter_matchers/any_of.rb +3 -3
  27. data/lib/mocha/parameter_matchers/anything.rb +1 -1
  28. data/lib/mocha/parameter_matchers/has_entries.rb +4 -1
  29. data/lib/mocha/parameter_matchers/has_entry.rb +3 -2
  30. data/lib/mocha/parameter_matchers/has_key.rb +1 -1
  31. data/lib/mocha/parameter_matchers/has_value.rb +1 -1
  32. data/lib/mocha/parameter_matchers/not.rb +2 -2
  33. data/lib/mocha/parameter_matchers/object.rb +1 -1
  34. data/lib/mocha/parameter_matchers/optionally.rb +22 -0
  35. data/lib/mocha/parameter_matchers/regexp_matches.rb +2 -2
  36. data/lib/mocha/parameter_matchers/responds_with.rb +43 -0
  37. data/lib/mocha/parameter_matchers/yaml_equivalent.rb +43 -0
  38. data/lib/mocha/single_return_value.rb +2 -9
  39. data/lib/mocha/standalone.rb +151 -17
  40. data/lib/mocha/state_machine.rb +91 -0
  41. data/lib/mocha/stubbing_error.rb +16 -0
  42. data/lib/mocha/test_case_adapter.rb +76 -22
  43. data/lib/stubba.rb +2 -1
  44. data/test/acceptance/acceptance_test_helper.rb +38 -0
  45. data/test/acceptance/bug_18914_test.rb +43 -0
  46. data/test/acceptance/{expected_invocation_count_acceptance_test.rb → expected_invocation_count_test.rb} +29 -20
  47. data/test/acceptance/failure_messages_test.rb +64 -0
  48. data/test/acceptance/{mocha_acceptance_test.rb → mocha_example_test.rb} +5 -5
  49. data/test/{integration/mocha_test_result_integration_test.rb → acceptance/mocha_test_result_test.rb} +19 -40
  50. data/test/acceptance/mock_test.rb +100 -0
  51. data/test/acceptance/{mock_with_initializer_block_acceptance_test.rb → mock_with_initializer_block_test.rb} +12 -5
  52. data/test/acceptance/{mocked_methods_dispatch_acceptance_test.rb → mocked_methods_dispatch_test.rb} +12 -5
  53. data/test/acceptance/{optional_parameters_acceptance_test.rb → optional_parameters_test.rb} +11 -4
  54. data/test/acceptance/{parameter_matcher_acceptance_test.rb → parameter_matcher_test.rb} +67 -5
  55. data/test/acceptance/{partial_mocks_acceptance_test.rb → partial_mocks_test.rb} +12 -5
  56. data/test/acceptance/return_value_test.rb +52 -0
  57. data/test/acceptance/{sequence_acceptance_test.rb → sequence_test.rb} +13 -6
  58. data/test/acceptance/{standalone_acceptance_test.rb → standalone_test.rb} +19 -11
  59. data/test/acceptance/states_test.rb +70 -0
  60. data/test/acceptance/stub_any_instance_method_test.rb +195 -0
  61. data/test/acceptance/stub_class_method_test.rb +203 -0
  62. data/test/acceptance/stub_everything_test.rb +56 -0
  63. data/test/acceptance/stub_instance_method_test.rb +165 -0
  64. data/test/acceptance/stub_module_method_test.rb +163 -0
  65. data/test/acceptance/stub_test.rb +52 -0
  66. data/test/acceptance/{stubba_acceptance_test.rb → stubba_example_test.rb} +1 -1
  67. data/test/{integration/stubba_test_result_integration_test.rb → acceptance/stubba_test_result_test.rb} +17 -36
  68. data/test/acceptance/stubbing_error_backtrace_test.rb +64 -0
  69. data/test/acceptance/stubbing_method_unnecessarily_test.rb +65 -0
  70. data/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +130 -0
  71. data/test/acceptance/stubbing_non_existent_class_method_test.rb +155 -0
  72. data/test/acceptance/stubbing_non_existent_instance_method_test.rb +145 -0
  73. data/test/acceptance/stubbing_non_public_any_instance_method_test.rb +130 -0
  74. data/test/acceptance/stubbing_non_public_class_method_test.rb +161 -0
  75. data/test/acceptance/stubbing_non_public_instance_method_test.rb +141 -0
  76. data/test/acceptance/stubbing_on_non_mock_object_test.rb +64 -0
  77. data/test/execution_point.rb +3 -1
  78. data/test/simple_counter.rb +13 -0
  79. data/test/test_helper.rb +0 -1
  80. data/test/test_runner.rb +6 -4
  81. data/test/unit/any_instance_method_test.rb +1 -1
  82. data/test/unit/array_inspect_test.rb +1 -1
  83. data/test/unit/backtrace_filter_test.rb +19 -0
  84. data/test/unit/cardinality_test.rb +56 -0
  85. data/test/unit/central_test.rb +4 -63
  86. data/test/unit/change_state_side_effect_test.rb +41 -0
  87. data/test/unit/class_method_test.rb +38 -1
  88. data/test/unit/date_time_inspect_test.rb +1 -1
  89. data/test/unit/{expectation_raiser_test.rb → exception_raiser_test.rb} +14 -0
  90. data/test/unit/expectation_list_test.rb +4 -22
  91. data/test/unit/expectation_test.rb +70 -94
  92. data/test/unit/in_state_ordering_constraint_test.rb +43 -0
  93. data/test/unit/mock_test.rb +16 -37
  94. data/test/unit/mockery_test.rb +149 -0
  95. data/test/unit/{no_yield_test.rb → no_yields_test.rb} +0 -0
  96. data/test/unit/object_test.rb +6 -89
  97. data/test/unit/parameter_matchers/equals_test.rb +25 -0
  98. data/test/unit/parameter_matchers/has_entries_test.rb +22 -1
  99. data/test/unit/parameter_matchers/has_entry_test.rb +24 -2
  100. data/test/unit/parameter_matchers/has_key_test.rb +11 -0
  101. data/test/unit/parameter_matchers/has_value_test.rb +12 -0
  102. data/test/unit/parameter_matchers/regexp_matches_test.rb +1 -1
  103. data/test/unit/parameter_matchers/responds_with_test.rb +25 -0
  104. data/test/unit/parameter_matchers/stub_matcher.rb +4 -0
  105. data/test/unit/parameter_matchers/yaml_equivalent_test.rb +25 -0
  106. data/test/unit/single_return_value_test.rb +0 -19
  107. data/test/unit/state_machine_test.rb +98 -0
  108. metadata +108 -69
  109. data/lib/mocha/auto_verify.rb +0 -118
  110. data/lib/mocha/infinite_range.rb +0 -25
  111. data/lib/mocha/missing_expectation.rb +0 -17
  112. data/lib/mocha/setup_and_teardown.rb +0 -23
  113. data/lib/mocha/stub.rb +0 -18
  114. data/test/integration/stubba_integration_test.rb +0 -89
  115. data/test/unit/auto_verify_test.rb +0 -129
  116. data/test/unit/expectation_error_test.rb +0 -24
  117. data/test/unit/infinite_range_test.rb +0 -53
  118. data/test/unit/missing_expectation_test.rb +0 -42
  119. data/test/unit/setup_and_teardown_test.rb +0 -76
  120. data/test/unit/stub_test.rb +0 -24
@@ -3,12 +3,11 @@ require 'method_definer'
3
3
  require 'mocha/expectation'
4
4
  require 'mocha/sequence'
5
5
  require 'execution_point'
6
- require 'deprecation_disabler'
6
+ require 'simple_counter'
7
7
 
8
8
  class ExpectationTest < Test::Unit::TestCase
9
9
 
10
10
  include Mocha
11
- include DeprecationDisabler
12
11
 
13
12
  def new_expectation
14
13
  Expectation.new(nil, :expected_method)
@@ -184,24 +183,6 @@ class ExpectationTest < Test::Unit::TestCase
184
183
  assert_nil expectation.invoke
185
184
  end
186
185
 
187
- def test_should_return_evaluated_proc
188
- proc = lambda { 99 }
189
- expectation = new_expectation.returns(proc)
190
- result = nil
191
- disable_deprecations { result = expectation.invoke }
192
- assert_equal 99, result
193
- end
194
-
195
- def test_should_return_evaluated_proc_without_using_is_a_method
196
- proc = lambda { 99 }
197
- proc.define_instance_accessor(:called)
198
- proc.called = false
199
- proc.replace_instance_method(:is_a?) { self.called = true; true}
200
- expectation = new_expectation.returns(proc)
201
- disable_deprecations { expectation.invoke }
202
- assert_equal false, proc.called
203
- end
204
-
205
186
  def test_should_raise_runtime_exception
206
187
  expectation = new_expectation.raises
207
188
  assert_raise(RuntimeError) { expectation.invoke }
@@ -248,101 +229,74 @@ class ExpectationTest < Test::Unit::TestCase
248
229
  assert_equal 2, expectation.invoke
249
230
  end
250
231
 
251
- def test_should_not_raise_error_on_verify_if_expected_call_was_made
232
+ def test_should_verify_successfully_if_expected_call_was_made
252
233
  expectation = new_expectation
253
234
  expectation.invoke
254
- assert_nothing_raised(ExpectationError) {
255
- expectation.verify
256
- }
235
+ assert expectation.verified?
257
236
  end
258
237
 
259
- def test_should_raise_error_on_verify_if_call_expected_once_but_invoked_twice
238
+ def test_should_not_verify_successfully_if_call_expected_once_but_invoked_twice
260
239
  expectation = new_expectation.once
261
240
  expectation.invoke
262
241
  expectation.invoke
263
- assert_raises(ExpectationError) {
264
- expectation.verify
265
- }
242
+ assert !expectation.verified?
266
243
  end
267
244
 
268
- def test_should_raise_error_on_verify_if_call_expected_once_but_not_invoked
245
+ def test_should_not_verify_successfully_if_call_expected_once_but_not_invoked
269
246
  expectation = new_expectation.once
270
- assert_raises(ExpectationError) {
271
- expectation.verify
272
- }
247
+ assert !expectation.verified?
273
248
  end
274
249
 
275
- def test_should_not_raise_error_on_verify_if_call_expected_once_and_invoked_once
250
+ def test_should_verify_successfully_if_call_expected_once_and_invoked_once
276
251
  expectation = new_expectation.once
277
252
  expectation.invoke
278
- assert_nothing_raised(ExpectationError) {
279
- expectation.verify
280
- }
253
+ assert expectation.verified?
281
254
  end
282
255
 
283
- def test_should_not_raise_error_on_verify_if_expected_call_was_made_at_least_once
256
+ def test_should_verify_successfully_if_expected_call_was_made_at_least_once
284
257
  expectation = new_expectation.at_least_once
285
258
  3.times {expectation.invoke}
286
- assert_nothing_raised(ExpectationError) {
287
- expectation.verify
288
- }
259
+ assert expectation.verified?
289
260
  end
290
261
 
291
- def test_should_raise_error_on_verify_if_expected_call_was_not_made_at_least_once
262
+ def test_should_not_verify_successfully_if_expected_call_was_not_made_at_least_once
292
263
  expectation = new_expectation.with(1, 2, 3).at_least_once
293
- e = assert_raise(ExpectationError) {
294
- expectation.verify
295
- }
296
- assert_match(/expected calls: at least 1, actual calls: 0/i, e.message)
264
+ assert !expectation.verified?
265
+ assert_match(/expected at least once, never invoked/i, expectation.mocha_inspect)
297
266
  end
298
267
 
299
- def test_should_not_raise_error_on_verify_if_expected_call_was_made_expected_number_of_times
268
+ def test_should_verify_successfully_if_expected_call_was_made_expected_number_of_times
300
269
  expectation = new_expectation.times(2)
301
270
  2.times {expectation.invoke}
302
- assert_nothing_raised(ExpectationError) {
303
- expectation.verify
304
- }
271
+ assert expectation.verified?
305
272
  end
306
273
 
307
- def test_should_expect_call_not_to_be_made
308
- expectation = new_expectation
309
- expectation.define_instance_accessor(:how_many_times)
310
- expectation.replace_instance_method(:times) { |how_many_times| self.how_many_times = how_many_times }
311
- expectation.never
312
- assert_equal 0, expectation.how_many_times
313
- end
314
-
315
- def test_should_raise_error_on_verify_if_expected_call_was_made_too_few_times
274
+ def test_should_not_verify_successfully_if_expected_call_was_made_too_few_times
316
275
  expectation = new_expectation.times(2)
317
276
  1.times {expectation.invoke}
318
- e = assert_raise(ExpectationError) {
319
- expectation.verify
320
- }
321
- assert_match(/expected calls: 2, actual calls: 1/i, e.message)
277
+ assert !expectation.verified?
278
+ assert_match(/expected exactly 2 times, already invoked 1 time/i, expectation.mocha_inspect)
322
279
  end
323
280
 
324
- def test_should_raise_error_on_verify_if_expected_call_was_made_too_many_times
281
+ def test_should_not_verify_successfully_if_expected_call_was_made_too_many_times
325
282
  expectation = new_expectation.times(2)
326
283
  3.times {expectation.invoke}
327
- assert_raise(ExpectationError) {
328
- expectation.verify
329
- }
284
+ assert !expectation.verified?
330
285
  end
331
286
 
332
- def test_should_yield_self_to_block
287
+ def test_should_increment_assertion_counter_for_expectation_because_it_does_need_verifyng
333
288
  expectation = new_expectation
334
289
  expectation.invoke
335
- yielded_expectation = nil
336
- expectation.verify { |x| yielded_expectation = x }
337
- assert_equal expectation, yielded_expectation
290
+ assertion_counter = SimpleCounter.new
291
+ expectation.verified?(assertion_counter)
292
+ assert_equal 1, assertion_counter.count
338
293
  end
339
294
 
340
- def test_should_yield_to_block_before_raising_exception
341
- yielded = false
342
- assert_raise(ExpectationError) {
343
- new_expectation.verify { |x| yielded = true }
344
- }
345
- assert yielded
295
+ def test_should_not_increment_assertion_counter_for_stub_because_it_does_not_need_verifying
296
+ stub = Expectation.new(nil, :expected_method).at_least(0)
297
+ assertion_counter = SimpleCounter.new
298
+ stub.verified?(assertion_counter)
299
+ assert_equal 0, assertion_counter.count
346
300
  end
347
301
 
348
302
  def test_should_store_backtrace_from_point_where_expectation_was_created
@@ -350,21 +304,6 @@ class ExpectationTest < Test::Unit::TestCase
350
304
  assert_equal execution_point, ExecutionPoint.new(expectation.backtrace)
351
305
  end
352
306
 
353
- def test_should_set_backtrace_on_assertion_failed_error_to_point_where_expectation_was_created
354
- execution_point = ExecutionPoint.current; expectation = Expectation.new(nil, :expected_method)
355
- error = assert_raise(ExpectationError) {
356
- expectation.verify
357
- }
358
- assert_equal execution_point, ExecutionPoint.new(error.backtrace)
359
- end
360
-
361
- def test_should_display_expectation_in_exception_message
362
- options = [:a, :b, {:c => 1, :d => 2}]
363
- expectation = new_expectation.with(*options)
364
- exception = assert_raise(ExpectationError) { expectation.verify }
365
- assert exception.message.include?(expectation.method_signature)
366
- end
367
-
368
307
  class FakeMock
369
308
 
370
309
  def initialize(name)
@@ -381,9 +320,9 @@ class ExpectationTest < Test::Unit::TestCase
381
320
  mock = FakeMock.new('mock')
382
321
  sequence_one = Sequence.new('one')
383
322
  sequence_two = Sequence.new('two')
384
- expectation = Expectation.new(mock, :expected_method).with(1, 2, {'a' => true, :b => false}, [1, 2, 3]).in_sequence(sequence_one, sequence_two)
385
- e = assert_raise(ExpectationError) { expectation.verify }
386
- assert_match "mock.expected_method(1, 2, {'a' => true, :b => false}, [1, 2, 3]); in sequence 'one'; in sequence 'two'", e.message
323
+ expectation = Expectation.new(mock, :expected_method).with(1, 2, {'a' => true}, {:b => false}, [1, 2, 3]).in_sequence(sequence_one, sequence_two)
324
+ assert !expectation.verified?
325
+ assert_match "mock.expected_method(1, 2, {'a' => true}, {:b => false}, [1, 2, 3]); in sequence 'one'; in sequence 'two'", expectation.mocha_inspect
387
326
  end
388
327
 
389
328
  class FakeConstraint
@@ -479,5 +418,42 @@ class ExpectationTest < Test::Unit::TestCase
479
418
  assert_equal [expectation], sequence_one.expectations
480
419
  assert_equal [expectation], sequence_two.expectations
481
420
  end
421
+
422
+ class FakeState
423
+
424
+ def initialize
425
+ @active = false
426
+ end
427
+
428
+ def activate
429
+ @active = true
430
+ end
431
+
432
+ def active?
433
+ @active
434
+ end
435
+
436
+ end
437
+
438
+ def test_should_change_state_when_expectation_is_invoked
439
+ state = FakeState.new
440
+ expectation = Expectation.new(nil, :method_one)
441
+
442
+ expectation.then(state)
443
+
444
+ expectation.invoke
445
+ assert state.active?
446
+ end
447
+
448
+ def test_should_match_when_state_is_active
449
+ state = FakeState.new
450
+ expectation = Expectation.new(nil, :method_one)
482
451
 
452
+ expectation.when(state)
453
+ assert !expectation.match?(:method_one)
454
+
455
+ state.activate
456
+ assert expectation.match?(:method_one)
457
+ end
458
+
483
459
  end
@@ -0,0 +1,43 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+
3
+ require 'mocha/in_state_ordering_constraint'
4
+
5
+ class InStateOrderingConstraintTest < Test::Unit::TestCase
6
+
7
+ include Mocha
8
+
9
+ class FakeStatePredicate
10
+
11
+ attr_writer :active, :description
12
+
13
+ def active?
14
+ @active
15
+ end
16
+
17
+ def mocha_inspect
18
+ @description
19
+ end
20
+
21
+ end
22
+
23
+ def test_should_allow_invocation_when_state_is_active
24
+ state_predicate = FakeStatePredicate.new
25
+ ordering_constraint = InStateOrderingConstraint.new(state_predicate)
26
+
27
+ state_predicate.active = true
28
+ assert ordering_constraint.allows_invocation_now?
29
+
30
+ state_predicate.active = false
31
+ assert !ordering_constraint.allows_invocation_now?
32
+ end
33
+
34
+ def test_should_describe_itself_in_terms_of_the_state_predicates_description
35
+ state_predicate = FakeStatePredicate.new
36
+ ordering_constraint = InStateOrderingConstraint.new(state_predicate)
37
+
38
+ state_predicate.description = 'the-state-predicate'
39
+
40
+ assert_equal 'when the-state-predicate', ordering_constraint.mocha_inspect
41
+ end
42
+
43
+ end
@@ -2,6 +2,7 @@ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
2
  require 'mocha/mock'
3
3
  require 'mocha/expectation_error'
4
4
  require 'set'
5
+ require 'simple_counter'
5
6
 
6
7
  class MockTest < Test::Unit::TestCase
7
8
 
@@ -33,26 +34,6 @@ class MockTest < Test::Unit::TestCase
33
34
  assert_equal true, mock.everything_stubbed
34
35
  end
35
36
 
36
- def test_should_display_object_id_for_mocha_inspect_if_mock_has_no_name
37
- mock = Mock.new
38
- assert_match Regexp.new("^#<Mock:0x[0-9A-Fa-f]{1,12}>$"), mock.mocha_inspect
39
- end
40
-
41
- def test_should_display_name_for_mocha_inspect_if_mock_has_name
42
- mock = Mock.new('named_mock')
43
- assert_equal "#<Mock:named_mock>", mock.mocha_inspect
44
- end
45
-
46
- def test_should_display_object_id_for_inspect_if_mock_has_no_name
47
- mock = Mock.new
48
- assert_match Regexp.new("^#<Mock:0x[0-9A-Fa-f]{1,12}>$"), mock.inspect
49
- end
50
-
51
- def test_should_display_name_for_inspect_if_mock_has_name
52
- mock = Mock.new('named_mock')
53
- assert_equal "#<Mock:named_mock>", mock.inspect
54
- end
55
-
56
37
  def test_should_be_able_to_extend_mock_object_with_module
57
38
  mock = Mock.new
58
39
  assert_nothing_raised(ExpectationError) { mock.extend(Module.new) }
@@ -73,7 +54,7 @@ class MockTest < Test::Unit::TestCase
73
54
  mock = Mock.new
74
55
  OBJECT_METHODS.each { |method| mock.__expects__(method.to_sym).returns(method) }
75
56
  OBJECT_METHODS.each { |method| assert_equal method, mock.__send__(method.to_sym) }
76
- assert_nothing_raised(ExpectationError) { mock.verify }
57
+ assert mock.verified?
77
58
  end
78
59
 
79
60
  def test_should_be_able_to_stub_standard_object_methods
@@ -150,30 +131,28 @@ class MockTest < Test::Unit::TestCase
150
131
  assert_equal [:argument1, :argument2], mock.arguments
151
132
  end
152
133
 
153
- def test_should_verify_that_all_expectations_have_been_fulfilled
134
+ def test_should_not_verify_successfully_because_not_all_expectations_have_been_satisfied
154
135
  mock = Mock.new
155
136
  mock.expects(:method1)
156
137
  mock.expects(:method2)
157
138
  mock.method1
158
- assert_raise(ExpectationError) do
159
- mock.verify
160
- end
161
- end
162
-
163
- def test_should_report_possible_expectations
164
- mock = Mock.new
165
- mock.expects(:expected_method).with(1)
166
- exception = assert_raise(ExpectationError) { mock.expected_method(2) }
167
- assert_equal "#{mock.mocha_inspect}.expected_method(2) - expected calls: 0, actual calls: 1\nSimilar expectations:\n#{mock.mocha_inspect}.expected_method(1)", exception.message
139
+ assert !mock.verified?
168
140
  end
169
141
 
170
- def test_should_pass_block_through_to_expectations_verify_method
142
+ def test_should_increment_assertion_counter_for_every_verified_expectation
171
143
  mock = Mock.new
172
- expected_expectation = mock.expects(:method1)
144
+
145
+ mock.expects(:method1)
173
146
  mock.method1
174
- expectations = []
175
- mock.verify() { |expectation| expectations << expectation }
176
- assert_equal [expected_expectation], expectations
147
+
148
+ mock.expects(:method2)
149
+ mock.method2
150
+
151
+ assertion_counter = SimpleCounter.new
152
+
153
+ mock.verified?(assertion_counter)
154
+
155
+ assert_equal 2, assertion_counter.count
177
156
  end
178
157
 
179
158
  def test_should_yield_supplied_parameters_to_block
@@ -0,0 +1,149 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+ require 'mocha/mockery'
3
+ require 'mocha/state_machine'
4
+
5
+ class MockeryTest < Test::Unit::TestCase
6
+
7
+ include Mocha
8
+
9
+ def test_should_build_instance_of_mockery
10
+ mockery = Mockery.instance
11
+ assert_not_nil mockery
12
+ assert_kind_of Mockery, mockery
13
+ end
14
+
15
+ def test_should_cache_instance_of_mockery
16
+ mockery_1 = Mockery.instance
17
+ mockery_2 = Mockery.instance
18
+ assert_same mockery_1, mockery_2
19
+ end
20
+
21
+ def test_should_expire_mockery_instance_cache
22
+ mockery_1 = Mockery.instance
23
+ Mockery.reset_instance
24
+ mockery_2 = Mockery.instance
25
+ assert_not_same mockery_1, mockery_2
26
+ end
27
+
28
+ def test_should_raise_expectation_error_because_not_all_expectations_are_satisfied
29
+ mockery = Mockery.new
30
+ mock_1 = mockery.named_mock('mock-1') { expects(:method_1) }
31
+ mock_2 = mockery.named_mock('mock-2') { expects(:method_2) }
32
+ 1.times { mock_1.method_1 }
33
+ 0.times { mock_2.method_2 }
34
+ assert_raises(ExpectationError) { mockery.verify }
35
+ end
36
+
37
+ def test_should_reset_list_of_mocks_on_teardown
38
+ mockery = Mockery.new
39
+ mock = mockery.unnamed_mock { expects(:my_method) }
40
+ mockery.teardown
41
+ assert_nothing_raised(ExpectationError) { mockery.verify }
42
+ end
43
+
44
+ def test_should_build_instance_of_stubba_on_instantiation
45
+ mockery = Mockery.new
46
+ assert_not_nil mockery.stubba
47
+ assert_kind_of Central, mockery.stubba
48
+ end
49
+
50
+ def test_should_build_new_instance_of_stubba_on_teardown
51
+ mockery = Mockery.new
52
+ stubba_1 = mockery.stubba
53
+ mockery.teardown
54
+ stubba_2 = mockery.stubba
55
+ assert_not_same stubba_1, stubba_2
56
+ end
57
+
58
+ def test_should_build_and_store_new_state_machine
59
+ mockery = Mockery.new
60
+ mockery.new_state_machine('state-machine-name')
61
+ assert_equal 1, mockery.state_machines.length
62
+ assert_kind_of StateMachine, mockery.state_machines[0]
63
+ end
64
+
65
+ def test_should_reset_list_of_state_machines_on_teardown
66
+ mockery = Mockery.new
67
+ mockery.new_state_machine('state-machine-name')
68
+ mockery.teardown
69
+ assert_equal 0, mockery.state_machines.length
70
+ end
71
+
72
+ class FakeMethod
73
+ def stub; end
74
+ def unstub; end
75
+ end
76
+
77
+ def test_should_unstub_all_methods_on_teardown
78
+ mockery = Mockery.new
79
+ stubba = mockery.stubba
80
+ stubba.stub(FakeMethod.new)
81
+ mockery.teardown
82
+ assert stubba.stubba_methods.empty?
83
+ end
84
+
85
+ def test_should_display_object_id_for_mocha_inspect_if_mock_has_no_name
86
+ mockery = Mockery.new
87
+ mock = mockery.unnamed_mock
88
+ assert_match Regexp.new("^#<Mock:0x[0-9A-Fa-f]{1,12}>$"), mock.mocha_inspect
89
+ end
90
+
91
+ def test_should_display_object_id_for_inspect_if_mock_has_no_name
92
+ mockery = Mockery.new
93
+ mock = mockery.unnamed_mock
94
+ assert_match Regexp.new("^#<Mock:0x[0-9A-Fa-f]{1,12}>$"), mock.inspect
95
+ end
96
+
97
+ def test_should_display_name_for_mocha_inspect_if_mock_has_string_name
98
+ mockery = Mockery.new
99
+ mock = mockery.named_mock('named_mock')
100
+ assert_equal "#<Mock:named_mock>", mock.mocha_inspect
101
+ end
102
+
103
+ def test_should_display_name_for_mocha_inspect_if_mock_has_symbol_name
104
+ mockery = Mockery.new
105
+ mock = mockery.named_mock(:named_mock)
106
+ assert_equal "#<Mock:named_mock>", mock.mocha_inspect
107
+ end
108
+
109
+ def test_should_display_name_for_inspect_if_mock_has_string_name
110
+ mockery = Mockery.new
111
+ mock = mockery.named_mock('named_mock')
112
+ assert_equal "#<Mock:named_mock>", mock.inspect
113
+ end
114
+
115
+ def test_should_display_name_for_inspect_if_mock_has_symbol_name
116
+ mockery = Mockery.new
117
+ mock = mockery.named_mock(:named_mock)
118
+ assert_equal "#<Mock:named_mock>", mock.inspect
119
+ end
120
+
121
+ def test_should_display_impersonated_object_for_mocha_inspect
122
+ mockery = Mockery.new
123
+ instance = Object.new
124
+ mock = mockery.mock_impersonating(instance)
125
+ assert_equal "#{instance.mocha_inspect}", mock.mocha_inspect
126
+ end
127
+
128
+ def test_should_display_impersonated_object_for_inspect
129
+ mockery = Mockery.new
130
+ instance = Object.new
131
+ mock = mockery.mock_impersonating(instance)
132
+ assert_equal "#{instance.mocha_inspect}", mock.inspect
133
+ end
134
+
135
+ class FakeClass; end
136
+
137
+ def test_should_display_any_instance_prefix_followed_by_class_whose_instances_are_being_impersonated_for_mocha_inspect
138
+ mockery = Mockery.new
139
+ mock = mockery.mock_impersonating_any_instance_of(FakeClass)
140
+ assert_equal "#<AnyInstance:MockeryTest::FakeClass>", mock.mocha_inspect
141
+ end
142
+
143
+ def test_should_display_any_instance_prefix_followed_by_class_whose_instances_are_being_impersonated_for_inspect
144
+ mockery = Mockery.new
145
+ mock = mockery.mock_impersonating_any_instance_of(FakeClass)
146
+ assert_equal "#<AnyInstance:MockeryTest::FakeClass>", mock.inspect
147
+ end
148
+
149
+ end