mocha 1.9.0 → 1.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (200) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +3 -0
  3. data/.rubocop_todo.yml +7 -30
  4. data/README.md +30 -11
  5. data/RELEASE.md +58 -0
  6. data/Rakefile +9 -3
  7. data/docs/CNAME +1 -0
  8. data/docs/Mocha.html +132 -5
  9. data/docs/Mocha/API.html +313 -216
  10. data/docs/Mocha/ClassMethods.html +13 -16
  11. data/docs/Mocha/Configuration.html +931 -174
  12. data/docs/Mocha/Expectation.html +200 -255
  13. data/docs/Mocha/ExpectationError.html +5 -10
  14. data/docs/Mocha/ExpectationErrorFactory.html +9 -18
  15. data/docs/Mocha/Hooks.html +12 -27
  16. data/docs/Mocha/Integration.html +3 -3
  17. data/docs/Mocha/Integration/MiniTest.html +3 -3
  18. data/docs/Mocha/Integration/MiniTest/Adapter.html +5 -6
  19. data/docs/Mocha/Integration/TestUnit.html +3 -3
  20. data/docs/Mocha/Integration/TestUnit/Adapter.html +5 -6
  21. data/docs/Mocha/Mock.html +202 -162
  22. data/docs/Mocha/ObjectMethods.html +121 -68
  23. data/docs/Mocha/ParameterMatchers.html +21 -109
  24. data/docs/Mocha/ParameterMatchers/AllOf.html +4 -5
  25. data/docs/Mocha/ParameterMatchers/AnyOf.html +4 -5
  26. data/docs/Mocha/ParameterMatchers/AnyParameters.html +3 -3
  27. data/docs/Mocha/ParameterMatchers/Anything.html +3 -3
  28. data/docs/Mocha/ParameterMatchers/Base.html +8 -15
  29. data/docs/Mocha/ParameterMatchers/Equals.html +4 -5
  30. data/docs/Mocha/ParameterMatchers/EquivalentUri.html +3 -3
  31. data/docs/Mocha/ParameterMatchers/HasEntries.html +4 -5
  32. data/docs/Mocha/ParameterMatchers/HasEntry.html +4 -5
  33. data/docs/Mocha/ParameterMatchers/HasKey.html +4 -5
  34. data/docs/Mocha/ParameterMatchers/HasValue.html +4 -5
  35. data/docs/Mocha/ParameterMatchers/Includes.html +4 -5
  36. data/docs/Mocha/ParameterMatchers/InstanceOf.html +4 -5
  37. data/docs/Mocha/ParameterMatchers/IsA.html +3 -3
  38. data/docs/Mocha/ParameterMatchers/KindOf.html +4 -5
  39. data/docs/Mocha/ParameterMatchers/Not.html +4 -5
  40. data/docs/Mocha/ParameterMatchers/Optionally.html +3 -3
  41. data/docs/Mocha/ParameterMatchers/RegexpMatches.html +4 -5
  42. data/docs/Mocha/ParameterMatchers/RespondsWith.html +4 -5
  43. data/docs/Mocha/ParameterMatchers/YamlEquivalent.html +4 -5
  44. data/docs/Mocha/Sequence.html +3 -3
  45. data/docs/Mocha/StateMachine.html +13 -25
  46. data/docs/Mocha/StateMachine/State.html +4 -5
  47. data/docs/Mocha/StateMachine/StatePredicate.html +4 -5
  48. data/docs/Mocha/StubbingError.html +3 -3
  49. data/docs/_index.html +4 -22
  50. data/docs/class_list.html +1 -1
  51. data/docs/file.COPYING.html +3 -3
  52. data/docs/file.MIT-LICENSE.html +3 -3
  53. data/docs/file.README.html +36 -14
  54. data/docs/file.RELEASE.html +72 -3
  55. data/docs/frames.html +1 -1
  56. data/docs/index.html +36 -14
  57. data/docs/method_list.html +96 -40
  58. data/docs/top-level-namespace.html +3 -3
  59. data/gemfiles/Gemfile.minitest.5.11.3 +7 -0
  60. data/init.rb +1 -3
  61. data/lib/mocha.rb +8 -0
  62. data/lib/mocha/any_instance_method.rb +9 -25
  63. data/lib/mocha/api.rb +65 -66
  64. data/lib/mocha/cardinality.rb +26 -11
  65. data/lib/mocha/class_methods.rb +17 -15
  66. data/lib/mocha/configuration.rb +295 -67
  67. data/lib/mocha/detection/test_unit.rb +1 -3
  68. data/lib/mocha/exception_raiser.rb +2 -1
  69. data/lib/mocha/expectation.rb +56 -58
  70. data/lib/mocha/expectation_error.rb +1 -3
  71. data/lib/mocha/expectation_list.rb +6 -6
  72. data/lib/mocha/inspect.rb +28 -26
  73. data/lib/mocha/instance_method.rb +19 -2
  74. data/lib/mocha/integration.rb +1 -3
  75. data/lib/mocha/integration/mini_test.rb +7 -0
  76. data/lib/mocha/integration/test_unit.rb +7 -0
  77. data/lib/mocha/invocation.rb +61 -0
  78. data/lib/mocha/macos_version.rb +5 -0
  79. data/lib/mocha/minitest.rb +6 -1
  80. data/lib/mocha/mock.rb +46 -31
  81. data/lib/mocha/mockery.rb +28 -61
  82. data/lib/mocha/names.rb +1 -1
  83. data/lib/mocha/object_methods.rb +13 -19
  84. data/lib/mocha/parameter_matchers.rb +1 -1
  85. data/lib/mocha/parameter_matchers/all_of.rb +1 -1
  86. data/lib/mocha/parameter_matchers/any_of.rb +1 -1
  87. data/lib/mocha/parameter_matchers/equivalent_uri.rb +0 -9
  88. data/lib/mocha/parameter_matchers/includes.rb +2 -0
  89. data/lib/mocha/parameter_matchers/instance_methods.rb +18 -0
  90. data/lib/mocha/raised_exception.rb +11 -0
  91. data/lib/mocha/return_values.rb +3 -3
  92. data/lib/mocha/setup.rb +5 -0
  93. data/lib/mocha/single_return_value.rb +2 -1
  94. data/lib/mocha/singleton_class.rb +9 -0
  95. data/lib/mocha/{class_method.rb → stubbed_method.rb} +23 -43
  96. data/lib/mocha/test_unit.rb +6 -1
  97. data/lib/mocha/thrower.rb +2 -1
  98. data/lib/mocha/thrown_object.rb +12 -0
  99. data/lib/mocha/version.rb +1 -1
  100. data/mocha.gemspec +1 -3
  101. data/test/acceptance/acceptance_test_helper.rb +6 -0
  102. data/test/acceptance/bug_18914_test.rb +0 -1
  103. data/test/acceptance/bug_21465_test.rb +0 -1
  104. data/test/acceptance/bug_21563_test.rb +0 -1
  105. data/test/acceptance/display_matching_invocations_alongside_expectations_test.rb +69 -0
  106. data/test/acceptance/exception_rescue_test.rb +1 -2
  107. data/test/acceptance/expectations_on_multiple_methods_test.rb +0 -1
  108. data/test/acceptance/expected_invocation_count_test.rb +2 -3
  109. data/test/acceptance/failure_messages_test.rb +0 -1
  110. data/test/acceptance/issue_272_test.rb +1 -2
  111. data/test/acceptance/issue_65_test.rb +0 -1
  112. data/test/acceptance/issue_70_test.rb +0 -1
  113. data/test/acceptance/mocha_example_test.rb +0 -1
  114. data/test/acceptance/mocha_test_result_test.rb +0 -1
  115. data/test/acceptance/mock_test.rb +47 -6
  116. data/test/acceptance/mocked_methods_dispatch_test.rb +0 -1
  117. data/test/acceptance/multiple_expectations_failure_message_test.rb +0 -1
  118. data/test/acceptance/optional_parameters_test.rb +0 -1
  119. data/test/acceptance/parameter_matcher_test.rb +0 -1
  120. data/test/acceptance/partial_mocks_test.rb +0 -1
  121. data/test/acceptance/prepend_test.rb +0 -1
  122. data/test/acceptance/prevent_use_of_mocha_outside_test_test.rb +0 -1
  123. data/test/acceptance/raise_exception_test.rb +0 -1
  124. data/test/acceptance/return_value_test.rb +0 -1
  125. data/test/acceptance/sequence_test.rb +0 -1
  126. data/test/acceptance/states_test.rb +0 -1
  127. data/test/acceptance/stub_any_instance_method_defined_on_superclass_test.rb +1 -2
  128. data/test/acceptance/stub_any_instance_method_test.rb +20 -1
  129. data/test/acceptance/stub_class_method_defined_on_active_record_association_proxy_test.rb +0 -1
  130. data/test/acceptance/stub_class_method_defined_on_class_test.rb +0 -1
  131. data/test/acceptance/stub_class_method_defined_on_module_test.rb +0 -1
  132. data/test/acceptance/stub_class_method_defined_on_superclass_test.rb +1 -2
  133. data/test/acceptance/stub_everything_test.rb +0 -1
  134. data/test/acceptance/stub_instance_method_defined_on_active_record_association_proxy_test.rb +0 -1
  135. data/test/acceptance/stub_instance_method_defined_on_class_and_aliased_test.rb +0 -1
  136. data/test/acceptance/stub_instance_method_defined_on_class_test.rb +0 -1
  137. data/test/acceptance/stub_instance_method_defined_on_kernel_module_test.rb +0 -1
  138. data/test/acceptance/stub_instance_method_defined_on_module_test.rb +0 -1
  139. data/test/acceptance/stub_instance_method_defined_on_object_class_test.rb +0 -1
  140. data/test/acceptance/stub_instance_method_defined_on_singleton_class_test.rb +0 -1
  141. data/test/acceptance/stub_instance_method_defined_on_superclass_test.rb +0 -1
  142. data/test/acceptance/stub_method_defined_on_module_and_aliased_test.rb +0 -1
  143. data/test/acceptance/stub_module_method_test.rb +0 -1
  144. data/test/acceptance/stub_test.rb +0 -1
  145. data/test/acceptance/stubba_example_test.rb +0 -1
  146. data/test/acceptance/stubba_test_result_test.rb +0 -1
  147. data/test/acceptance/stubbing_error_backtrace_test.rb +4 -5
  148. data/test/acceptance/stubbing_frozen_object_test.rb +0 -1
  149. data/test/acceptance/stubbing_method_accepting_block_parameter_test.rb +0 -1
  150. data/test/acceptance/stubbing_method_unnecessarily_test.rb +5 -5
  151. data/test/acceptance/stubbing_nil_test.rb +5 -5
  152. data/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +11 -11
  153. data/test/acceptance/stubbing_non_existent_class_method_test.rb +11 -11
  154. data/test/acceptance/stubbing_non_existent_instance_method_test.rb +11 -11
  155. data/test/acceptance/stubbing_non_public_any_instance_method_test.rb +8 -8
  156. data/test/acceptance/stubbing_non_public_class_method_test.rb +9 -9
  157. data/test/acceptance/stubbing_non_public_instance_method_test.rb +9 -9
  158. data/test/acceptance/stubbing_on_non_mock_object_test.rb +5 -5
  159. data/test/acceptance/stubbing_same_class_method_on_parent_and_child_classes_test.rb +0 -1
  160. data/test/acceptance/throw_test.rb +0 -1
  161. data/test/acceptance/unexpected_invocation_test.rb +0 -1
  162. data/test/acceptance/unstubbing_test.rb +0 -1
  163. data/test/integration/shared_tests.rb +5 -3
  164. data/test/method_definer.rb +11 -17
  165. data/test/test_runner.rb +2 -0
  166. data/test/unit/any_instance_method_test.rb +41 -40
  167. data/test/unit/cardinality_test.rb +41 -23
  168. data/test/unit/central_test.rb +0 -1
  169. data/test/unit/class_methods_test.rb +1 -1
  170. data/test/unit/configuration_test.rb +12 -12
  171. data/test/unit/exception_raiser_test.rb +10 -5
  172. data/test/unit/expectation_list_test.rb +13 -11
  173. data/test/unit/expectation_test.rb +88 -103
  174. data/test/unit/instance_method_test.rb +282 -0
  175. data/test/unit/mock_test.rb +28 -19
  176. data/test/unit/mockery_test.rb +8 -11
  177. data/test/unit/module_methods_test.rb +2 -3
  178. data/test/unit/object_inspect_test.rb +6 -4
  179. data/test/unit/object_methods_test.rb +3 -2
  180. data/test/unit/parameter_matchers/equivalent_uri_test.rb +0 -9
  181. data/test/unit/parameter_matchers/has_entries_test.rb +1 -1
  182. data/test/unit/parameter_matchers/has_entry_test.rb +1 -1
  183. data/test/unit/parameter_matchers/has_key_test.rb +1 -1
  184. data/test/unit/parameter_matchers/has_value_test.rb +1 -1
  185. data/test/unit/parameter_matchers/includes_test.rb +1 -1
  186. data/test/unit/parameter_matchers/responds_with_test.rb +1 -1
  187. data/test/unit/return_values_test.rb +25 -20
  188. data/test/unit/single_return_value_test.rb +6 -1
  189. data/test/unit/thrower_test.rb +7 -2
  190. metadata +14 -28
  191. data/docs/Mocha/UnexpectedInvocation.html +0 -140
  192. data/lib/mocha/mini_test.rb +0 -5
  193. data/lib/mocha/module_method.rb +0 -6
  194. data/lib/mocha/module_methods.rb +0 -10
  195. data/lib/mocha/parameter_matchers/object.rb +0 -15
  196. data/lib/mocha/standalone.rb +0 -4
  197. data/lib/mocha/unexpected_invocation.rb +0 -24
  198. data/lib/mocha_standalone.rb +0 -4
  199. data/test/acceptance/mock_with_initializer_block_test.rb +0 -56
  200. data/test/unit/class_method_test.rb +0 -276
@@ -14,10 +14,8 @@ module Mocha
14
14
  if testcase
15
15
  begin
16
16
  require 'test/unit/version'
17
- # rubocop:disable Lint/HandleExceptions
18
- rescue LoadError
17
+ rescue LoadError # rubocop:disable Lint/HandleExceptions
19
18
  end
20
- # rubocop:enable Lint/HandleExceptions
21
19
  if defined?(::Test::Unit::VERSION)
22
20
  version = ::Test::Unit::VERSION
23
21
  end
@@ -5,7 +5,8 @@ module Mocha
5
5
  @message = message
6
6
  end
7
7
 
8
- def evaluate
8
+ def evaluate(invocation)
9
+ invocation.raised(@exception)
9
10
  raise @exception, @exception.to_s if @exception.is_a?(Module) && (@exception < Interrupt)
10
11
  raise @exception, @message if @message
11
12
  raise @exception
@@ -9,6 +9,7 @@ require 'mocha/is_a'
9
9
  require 'mocha/in_state_ordering_constraint'
10
10
  require 'mocha/change_state_side_effect'
11
11
  require 'mocha/cardinality'
12
+ require 'mocha/configuration'
12
13
 
13
14
  module Mocha
14
15
  # Methods on expectations returned from {Mock#expects}, {Mock#stubs}, {ObjectMethods#expects} and {ObjectMethods#stubs}.
@@ -223,7 +224,7 @@ module Mocha
223
224
  self
224
225
  end
225
226
 
226
- # Modifies expectation so that when the expected method is called, it yields with the specified +parameters+.
227
+ # Modifies expectation so that when the expected method is called, it yields with the specified +parameters+ (even if no block is provided, in which case yielding will result in a +LocalJumpError+).
227
228
  #
228
229
  # May be called multiple times on the same expectation for consecutive invocations.
229
230
  #
@@ -231,49 +232,56 @@ module Mocha
231
232
  # @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
232
233
  # @see #then
233
234
  #
235
+ # @example Yield when expected method is invoked.
236
+ # benchmark = mock()
237
+ # benchmark.expects(:measure).yields.then.returns('0.350000 0.400000 0.750000 ( 0.835234)')
238
+ # yielded = false
239
+ # returned_value = benchmark.measure { yielded = true }
240
+ # yielded # => true
241
+ # returned_value # => '0.350000 0.400000 0.750000 ( 0.835234)'
242
+ #
234
243
  # @example Yield parameters when expected method is invoked.
235
- # object = mock()
236
- # object.expects(:expected_method).yields('result')
237
- # yielded_value = nil
238
- # object.expected_method { |value| yielded_value = value }
239
- # yielded_value # => 'result'
244
+ # fibonacci = mock()
245
+ # fibonacci.expects(:next_pair).yields(0, 1)
246
+ # sum = 0
247
+ # fibonacci.next_pair { |first, second| sum = first + second }
248
+ # sum # => 1
240
249
  #
241
250
  # @example Yield different parameters on different invocations of the expected method.
242
- # object = mock()
243
- # object.stubs(:expected_method).yields(1).then.yields(2)
244
- # yielded_values_from_first_invocation = []
245
- # yielded_values_from_second_invocation = []
246
- # object.expected_method { |value| yielded_values_from_first_invocation << value } # first invocation
247
- # object.expected_method { |value| yielded_values_from_second_invocation << value } # second invocation
248
- # yielded_values_from_first_invocation # => [1]
249
- # yielded_values_from_second_invocation # => [2]
251
+ # fibonacci = mock()
252
+ # fibonacci.expects(:next_pair).yields(0, 1).then.yields(1, 1)
253
+ # sum = 0
254
+ # fibonacci.next_pair { |first, second| sum = first + second }
255
+ # sum # => 1
256
+ # fibonacci.next_pair { |first, second| sum = first + second }
257
+ # sum # => 2
250
258
  def yields(*parameters)
251
259
  @yield_parameters.add(*parameters)
252
260
  self
253
261
  end
254
262
 
255
- # Modifies expectation so that when the expected method is called, it yields multiple times per invocation with the specified +parameter_groups+.
263
+ # Modifies expectation so that when the expected method is called, it yields multiple times per invocation with the specified +parameter_groups+ (even if no block is provided, in which case yielding will result in a +LocalJumpError+).
256
264
  #
257
265
  # @param [*Array<Array>] parameter_groups each element of +parameter_groups+ should iself be an +Array+ representing the parameters to be passed to the block for a single yield.
258
266
  # @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
259
267
  # @see #then
260
268
  #
261
- # @example When the +expected_method+ is called, the stub will invoke the block twice, the first time it passes +'result_1'+, +'result_2'+ as the parameters, and the second time it passes 'result_3' as the parameters.
262
- # object = mock()
263
- # object.expects(:expected_method).multiple_yields(['result_1', 'result_2'], ['result_3'])
264
- # yielded_values = []
265
- # object.expected_method { |*values| yielded_values << values }
266
- # yielded_values # => [['result_1', 'result_2'], ['result_3]]
267
- #
268
- # @example Yield different groups of parameters on different invocations of the expected method.
269
- # object = mock()
270
- # object.stubs(:expected_method).multiple_yields([1, 2], [3]).then.multiple_yields([4], [5, 6])
271
- # yielded_values_from_first_invocation = []
272
- # yielded_values_from_second_invocation = []
273
- # object.expected_method { |*values| yielded_values_from_first_invocation << values } # first invocation
274
- # object.expected_method { |*values| yielded_values_from_second_invocation << values } # second invocation
275
- # yielded_values_from_first_invocation # => [[1, 2], [3]]
276
- # yielded_values_from_second_invocation # => [[4], [5, 6]]
269
+ # @example When +foreach+ is called, the stub will invoke the block twice, the first time it passes ['row1_col1', 'row1_col2'] as the parameters, and the second time it passes ['row2_col1', ''] as the parameters.
270
+ # csv = mock()
271
+ # csv.expects(:foreach).with("path/to/file.csv").multiple_yields(['row1_col1', 'row1_col2'], ['row2_col1', ''])
272
+ # rows = []
273
+ # csv.foreach { |row| rows << row }
274
+ # rows # => [['row1_col1', 'row1_col2'], ['row2_col1', '']]
275
+ #
276
+ # @example Yield different groups of parameters on different invocations of the expected method. Simulating a situation where the CSV file at 'path/to/file.csv' has been modified between the two calls to +foreach+.
277
+ # csv = mock()
278
+ # csv.stubs(:foreach).with("path/to/file.csv").multiple_yields(['old_row1_col1', 'old_row1_col2'], ['old_row2_col1', '']).then.multiple_yields(['new_row1_col1', ''], ['new_row2_col1', 'new_row2_col2'])
279
+ # rows_from_first_invocation = []
280
+ # rows_from_second_invocation = []
281
+ # csv.foreach { |row| rows_from_first_invocation << row } # first invocation
282
+ # csv.foreach { |row| rows_from_second_invocation << row } # second invocation
283
+ # rows_from_first_invocation # => [['old_row1_col1', 'old_row1_col2'], ['old_row2_col1', '']]
284
+ # rows_from_second_invocation # => [['new_row1_col1', ''], ['new_row2_col1', 'new_row2_col2']]
277
285
  def multiple_yields(*parameter_groups)
278
286
  @yield_parameters.multiple_add(*parameter_groups)
279
287
  self
@@ -477,7 +485,8 @@ module Mocha
477
485
  #
478
486
  # An expected method can appear in multiple sequences.
479
487
  #
480
- # @param [*Array<Sequence>] sequences sequences in which expected method should appear.
488
+ # @param [Sequence] sequence sequence in which expected method should appear.
489
+ # @param [*Array<Sequence>] sequences more sequences in which expected method should appear.
481
490
  # @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
482
491
  #
483
492
  # @see API#sequence
@@ -489,8 +498,8 @@ module Mocha
489
498
  # egg.expects(:crack).in_sequence(breakfast)
490
499
  # egg.expects(:fry).in_sequence(breakfast)
491
500
  # egg.expects(:eat).in_sequence(breakfast)
492
- def in_sequence(*sequences)
493
- sequences.each { |sequence| add_in_sequence_ordering_constraint(sequence) }
501
+ def in_sequence(sequence, *sequences)
502
+ sequences.unshift(sequence).each { |seq| add_in_sequence_ordering_constraint(seq) }
494
503
  self
495
504
  end
496
505
 
@@ -505,7 +514,6 @@ module Mocha
505
514
  @ordering_constraints = []
506
515
  @side_effects = []
507
516
  @cardinality = Cardinality.exactly(1)
508
- @invocation_count = 0
509
517
  @return_values = ReturnValues.new
510
518
  @yield_parameters = YieldParameters.new
511
519
  @backtrace = backtrace || caller
@@ -542,41 +550,36 @@ module Mocha
542
550
  end
543
551
 
544
552
  # @private
545
- def match?(actual_method_name, *actual_parameters)
546
- @method_matcher.match?(actual_method_name) && @parameters_matcher.match?(actual_parameters) && in_correct_order?
553
+ def match?(invocation)
554
+ @method_matcher.match?(invocation.method_name) && @parameters_matcher.match?(invocation.arguments) && in_correct_order?
547
555
  end
548
556
 
549
557
  # @private
550
558
  def invocations_allowed?
551
- @cardinality.invocations_allowed?(@invocation_count)
559
+ @cardinality.invocations_allowed?
552
560
  end
553
561
 
554
562
  # @private
555
563
  def satisfied?
556
- @cardinality.satisfied?(@invocation_count)
564
+ @cardinality.satisfied?
557
565
  end
558
566
 
559
567
  # @private
560
- def invoke
561
- @invocation_count += 1
568
+ def invoke(invocation)
562
569
  perform_side_effects
563
- if block_given?
564
- @yield_parameters.next_invocation.each do |yield_parameters|
565
- yield(*yield_parameters)
566
- end
567
- end
568
- @return_values.next
570
+ @cardinality << invocation
571
+ invocation.call(@yield_parameters, @return_values) { |*yield_args| yield(*yield_args) }
569
572
  end
570
573
 
571
574
  # @private
572
575
  def verified?(assertion_counter = nil)
573
576
  assertion_counter.increment if assertion_counter && @cardinality.needs_verifying?
574
- @cardinality.verified?(@invocation_count)
577
+ @cardinality.verified?
575
578
  end
576
579
 
577
580
  # @private
578
581
  def used?
579
- @cardinality.used?(@invocation_count)
582
+ @cardinality.used?
580
583
  end
581
584
 
582
585
  # @private
@@ -588,16 +591,11 @@ module Mocha
588
591
 
589
592
  # @private
590
593
  def mocha_inspect
591
- message = "#{@cardinality.mocha_inspect}, "
592
- message << case @invocation_count
593
- when 0 then 'not yet invoked'
594
- when 1 then 'invoked once'
595
- when 2 then 'invoked twice'
596
- else "invoked #{@invocation_count} times"
597
- end
598
- message << ': '
599
- message << method_signature
594
+ message = "#{@cardinality.anticipated_times}, #{@cardinality.invoked_times}: #{method_signature}"
600
595
  message << "; #{@ordering_constraints.map(&:mocha_inspect).join('; ')}" unless @ordering_constraints.empty?
596
+ if Mocha.configuration.display_matching_invocations_on_failure?
597
+ message << @cardinality.actual_invocations
598
+ end
601
599
  message
602
600
  end
603
601
 
@@ -4,7 +4,5 @@ module Mocha
4
4
  # Authors of test libraries may use +Mocha::ExpectationErrorFactory+ to have Mocha raise a different exception.
5
5
  #
6
6
  # @see Mocha::ExpectationErrorFactory
7
- # rubocop:disable Lint/InheritException
8
- class ExpectationError < Exception; end
9
- # rubocop:enable Lint/InheritException
7
+ class ExpectationError < Exception; end # rubocop:disable Lint/InheritException
10
8
  end
@@ -17,12 +17,12 @@ module Mocha
17
17
  @expectations.any? { |expectation| expectation.matches_method?(method_name) }
18
18
  end
19
19
 
20
- def match(method_name, *arguments)
21
- matching_expectations(method_name, *arguments).first
20
+ def match(invocation)
21
+ matching_expectations(invocation).first
22
22
  end
23
23
 
24
- def match_allowing_invocation(method_name, *arguments)
25
- matching_expectations(method_name, *arguments).detect(&:invocations_allowed?)
24
+ def match_allowing_invocation(invocation)
25
+ matching_expectations(invocation).detect(&:invocations_allowed?)
26
26
  end
27
27
 
28
28
  def verified?(assertion_counter = nil)
@@ -51,8 +51,8 @@ module Mocha
51
51
 
52
52
  private
53
53
 
54
- def matching_expectations(method_name, *arguments)
55
- @expectations.select { |e| e.match?(method_name, *arguments) }
54
+ def matching_expectations(invocation)
55
+ @expectations.select { |e| e.match?(invocation) }
56
56
  end
57
57
  end
58
58
  end
@@ -1,55 +1,57 @@
1
1
  require 'date'
2
2
 
3
3
  module Mocha
4
- module ObjectMethods
5
- def mocha_inspect
6
- address = __id__ * 2
7
- address += 0x100000000 if address < 0
8
- inspect =~ /#</ ? "#<#{self.class}:0x#{Kernel.format('%x', address)}>" : inspect
4
+ module Inspect
5
+ module ObjectMethods
6
+ def mocha_inspect
7
+ address = __id__ * 2
8
+ address += 0x100000000 if address < 0
9
+ inspect =~ /#</ ? "#<#{self.class}:0x#{Kernel.format('%x', address)}>" : inspect
10
+ end
9
11
  end
10
- end
11
12
 
12
- module ArrayMethods
13
- def mocha_inspect
14
- "[#{collect(&:mocha_inspect).join(', ')}]"
13
+ module ArrayMethods
14
+ def mocha_inspect
15
+ "[#{collect(&:mocha_inspect).join(', ')}]"
16
+ end
15
17
  end
16
- end
17
18
 
18
- module HashMethods
19
- def mocha_inspect
20
- "{#{collect { |key, value| "#{key.mocha_inspect} => #{value.mocha_inspect}" }.join(', ')}}"
19
+ module HashMethods
20
+ def mocha_inspect
21
+ "{#{collect { |key, value| "#{key.mocha_inspect} => #{value.mocha_inspect}" }.join(', ')}}"
22
+ end
21
23
  end
22
- end
23
24
 
24
- module TimeMethods
25
- def mocha_inspect
26
- "#{inspect} (#{to_f} secs)"
25
+ module TimeMethods
26
+ def mocha_inspect
27
+ "#{inspect} (#{to_f} secs)"
28
+ end
27
29
  end
28
- end
29
30
 
30
- module DateMethods
31
- def mocha_inspect
32
- to_s
31
+ module DateMethods
32
+ def mocha_inspect
33
+ to_s
34
+ end
33
35
  end
34
36
  end
35
37
  end
36
38
 
37
39
  class Object
38
- include Mocha::ObjectMethods
40
+ include Mocha::Inspect::ObjectMethods
39
41
  end
40
42
 
41
43
  class Array
42
- include Mocha::ArrayMethods
44
+ include Mocha::Inspect::ArrayMethods
43
45
  end
44
46
 
45
47
  class Hash
46
- include Mocha::HashMethods
48
+ include Mocha::Inspect::HashMethods
47
49
  end
48
50
 
49
51
  class Time
50
- include Mocha::TimeMethods
52
+ include Mocha::Inspect::TimeMethods
51
53
  end
52
54
 
53
55
  class Date
54
- include Mocha::DateMethods
56
+ include Mocha::Inspect::DateMethods
55
57
  end
@@ -1,6 +1,23 @@
1
- require 'mocha/class_method'
1
+ require 'mocha/stubbed_method'
2
2
 
3
3
  module Mocha
4
- class InstanceMethod < ClassMethod
4
+ class InstanceMethod < StubbedMethod
5
+ private
6
+
7
+ def mock_owner
8
+ stubbee
9
+ end
10
+
11
+ def method_body(method)
12
+ PRE_RUBY_V19 ? proc { |*args, &block| method.call(*args, &block) } : method
13
+ end
14
+
15
+ def stubbee_method(method_name)
16
+ stubbee._method(method_name)
17
+ end
18
+
19
+ def original_method_owner
20
+ stubbee.singleton_class
21
+ end
5
22
  end
6
23
  end
@@ -1,4 +1,3 @@
1
- require 'mocha/deprecation'
2
1
  require 'mocha/integration/test_unit'
3
2
  require 'mocha/integration/mini_test'
4
3
 
@@ -6,8 +5,7 @@ module Mocha
6
5
  module Integration
7
6
  def self.activate
8
7
  return unless [Integration::TestUnit, Integration::MiniTest].map(&:activate).none?
9
- Deprecation.warning("Test::Unit or MiniTest must be loaded *before* `require 'mocha/setup'`.")
10
- Deprecation.warning("If you're integrating with a test library other than Test::Unit or MiniTest, you should use `require 'mocha/api'` instead of `require 'mocha/setup'`.")
8
+ raise "Test::Unit or Minitest must be loaded *before* `require 'mocha/setup'`."
11
9
  end
12
10
  end
13
11
  end
@@ -14,6 +14,8 @@ require 'mocha/integration/mini_test/version_2110_to_2111'
14
14
  require 'mocha/integration/mini_test/version_2112_to_320'
15
15
  require 'mocha/integration/mini_test/adapter'
16
16
 
17
+ require 'mocha/deprecation'
18
+
17
19
  module Mocha
18
20
  module Integration
19
21
  module MiniTest
@@ -39,6 +41,11 @@ module Mocha
39
41
 
40
42
  target = Detection::MiniTest.testcase
41
43
  unless target < integration_module
44
+ unless integration_module == MiniTest::Adapter
45
+ Deprecation.warning(
46
+ 'Versions of minitest earlier than v3.3.0 will not be supported in future versions of Mocha.'
47
+ )
48
+ end
42
49
  Debug.puts "Applying #{integration_module.description}"
43
50
  target.send(:include, integration_module)
44
51
  end
@@ -11,6 +11,8 @@ require 'mocha/integration/test_unit/gem_version_203_to_220'
11
11
  require 'mocha/integration/test_unit/gem_version_230_to_250'
12
12
  require 'mocha/integration/test_unit/adapter'
13
13
 
14
+ require 'mocha/deprecation'
15
+
14
16
  module Mocha
15
17
  module Integration
16
18
  module TestUnit
@@ -34,6 +36,11 @@ module Mocha
34
36
  ].detect { |m| m.applicable_to?(test_unit_version, ruby_version) }
35
37
 
36
38
  unless ::Test::Unit::TestCase < integration_module
39
+ unless integration_module == TestUnit::Adapter
40
+ Deprecation.warning(
41
+ 'Versions of test-unit earlier than v2.5.1 will not be supported in future versions of Mocha.'
42
+ )
43
+ end
37
44
  Debug.puts "Applying #{integration_module.description}"
38
45
  ::Test::Unit::TestCase.send(:include, integration_module)
39
46
  end
@@ -0,0 +1,61 @@
1
+ require 'mocha/parameters_matcher'
2
+ require 'mocha/raised_exception'
3
+ require 'mocha/return_values'
4
+ require 'mocha/thrown_object'
5
+ require 'mocha/yield_parameters'
6
+
7
+ module Mocha
8
+ class Invocation
9
+ attr_reader :method_name
10
+
11
+ def initialize(mock, method_name, *arguments)
12
+ @mock = mock
13
+ @method_name = method_name
14
+ @arguments = arguments
15
+ @yields = []
16
+ @result = nil
17
+ end
18
+
19
+ def call(yield_parameters = YieldParameters.new, return_values = ReturnValues.new)
20
+ yield_parameters.next_invocation.each do |yield_args|
21
+ @yields << ParametersMatcher.new(yield_args)
22
+ yield(*yield_args)
23
+ end
24
+ return_values.next(self)
25
+ end
26
+
27
+ def returned(value)
28
+ @result = value
29
+ end
30
+
31
+ def raised(exception)
32
+ @result = RaisedException.new(exception)
33
+ end
34
+
35
+ def threw(tag, value)
36
+ @result = ThrownObject.new(tag, value)
37
+ end
38
+
39
+ def arguments
40
+ @arguments.dup
41
+ end
42
+
43
+ def call_description
44
+ "#{@mock.mocha_inspect}.#{@method_name}#{ParametersMatcher.new(@arguments).mocha_inspect}"
45
+ end
46
+
47
+ def short_call_description
48
+ "#{@method_name}(#{@arguments.join(', ')})"
49
+ end
50
+
51
+ def result_description
52
+ desc = "# => #{@result.mocha_inspect}"
53
+ desc << " after yielding #{@yields.map(&:mocha_inspect).join(', then ')}" if @yields.any?
54
+ desc
55
+ end
56
+
57
+ def full_description
58
+ "\n - #{call_description} #{result_description}"
59
+ end
60
+ end
61
+ end