mocha 1.8.0 → 1.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (218) hide show
  1. checksums.yaml +4 -4
  2. data/.github/FUNDING.yml +1 -0
  3. data/.rubocop.yml +3 -0
  4. data/.rubocop_todo.yml +7 -30
  5. data/.yardopts +1 -0
  6. data/README.md +40 -13
  7. data/RELEASE.md +97 -0
  8. data/Rakefile +10 -7
  9. data/bin/build-matrix +1 -2
  10. data/docs/CNAME +1 -1
  11. data/docs/Mocha.html +132 -5
  12. data/docs/Mocha/API.html +437 -206
  13. data/docs/Mocha/ClassMethods.html +13 -16
  14. data/docs/Mocha/Configuration.html +1126 -227
  15. data/docs/Mocha/Expectation.html +420 -267
  16. data/docs/Mocha/ExpectationError.html +5 -10
  17. data/docs/Mocha/ExpectationErrorFactory.html +9 -18
  18. data/docs/Mocha/Hooks.html +12 -27
  19. data/docs/Mocha/Integration.html +3 -3
  20. data/docs/Mocha/Integration/MiniTest.html +3 -3
  21. data/docs/Mocha/Integration/MiniTest/Adapter.html +5 -6
  22. data/docs/Mocha/Integration/TestUnit.html +3 -3
  23. data/docs/Mocha/Integration/TestUnit/Adapter.html +5 -6
  24. data/docs/Mocha/Mock.html +202 -162
  25. data/docs/Mocha/ObjectMethods.html +121 -68
  26. data/docs/Mocha/ParameterMatchers.html +21 -109
  27. data/docs/Mocha/ParameterMatchers/AllOf.html +4 -5
  28. data/docs/Mocha/ParameterMatchers/AnyOf.html +4 -5
  29. data/docs/Mocha/ParameterMatchers/AnyParameters.html +3 -3
  30. data/docs/Mocha/ParameterMatchers/Anything.html +3 -3
  31. data/docs/Mocha/ParameterMatchers/Base.html +8 -15
  32. data/docs/Mocha/ParameterMatchers/Equals.html +4 -5
  33. data/docs/Mocha/ParameterMatchers/EquivalentUri.html +3 -3
  34. data/docs/Mocha/ParameterMatchers/HasEntries.html +4 -5
  35. data/docs/Mocha/ParameterMatchers/HasEntry.html +4 -5
  36. data/docs/Mocha/ParameterMatchers/HasKey.html +4 -5
  37. data/docs/Mocha/ParameterMatchers/HasValue.html +4 -5
  38. data/docs/Mocha/ParameterMatchers/Includes.html +4 -5
  39. data/docs/Mocha/ParameterMatchers/InstanceOf.html +4 -5
  40. data/docs/Mocha/ParameterMatchers/IsA.html +3 -3
  41. data/docs/Mocha/ParameterMatchers/KindOf.html +4 -5
  42. data/docs/Mocha/ParameterMatchers/Not.html +4 -5
  43. data/docs/Mocha/ParameterMatchers/Optionally.html +3 -3
  44. data/docs/Mocha/ParameterMatchers/RegexpMatches.html +4 -5
  45. data/docs/Mocha/ParameterMatchers/RespondsWith.html +4 -5
  46. data/docs/Mocha/ParameterMatchers/YamlEquivalent.html +4 -5
  47. data/docs/Mocha/Sequence.html +3 -3
  48. data/docs/Mocha/StateMachine.html +13 -25
  49. data/docs/Mocha/StateMachine/State.html +4 -5
  50. data/docs/Mocha/StateMachine/StatePredicate.html +4 -5
  51. data/docs/Mocha/StubbingError.html +3 -3
  52. data/docs/_index.html +4 -22
  53. data/docs/class_list.html +1 -1
  54. data/docs/file.COPYING.html +3 -3
  55. data/docs/file.MIT-LICENSE.html +3 -3
  56. data/docs/file.README.html +46 -16
  57. data/docs/file.RELEASE.html +130 -3
  58. data/docs/frames.html +1 -1
  59. data/docs/index.html +46 -16
  60. data/docs/js/app.js +11 -0
  61. data/docs/method_list.html +117 -37
  62. data/docs/top-level-namespace.html +3 -3
  63. data/gemfiles/Gemfile.minitest.5.11.3 +7 -0
  64. data/init.rb +1 -3
  65. data/lib/mocha.rb +8 -0
  66. data/lib/mocha/any_instance_method.rb +11 -53
  67. data/lib/mocha/api.rb +120 -56
  68. data/lib/mocha/block_matcher.rb +31 -0
  69. data/lib/mocha/cardinality.rb +26 -11
  70. data/lib/mocha/class_methods.rb +17 -15
  71. data/lib/mocha/configuration.rb +351 -67
  72. data/lib/mocha/deprecation.rb +2 -1
  73. data/lib/mocha/detection/test_unit.rb +1 -3
  74. data/lib/mocha/exception_raiser.rb +2 -1
  75. data/lib/mocha/expectation.rb +102 -63
  76. data/lib/mocha/expectation_error.rb +1 -3
  77. data/lib/mocha/expectation_list.rb +6 -6
  78. data/lib/mocha/inspect.rb +28 -26
  79. data/lib/mocha/instance_method.rb +19 -2
  80. data/lib/mocha/integration.rb +1 -3
  81. data/lib/mocha/integration/mini_test.rb +7 -0
  82. data/lib/mocha/integration/test_unit.rb +7 -0
  83. data/lib/mocha/invocation.rb +77 -0
  84. data/lib/mocha/macos_version.rb +5 -0
  85. data/lib/mocha/minitest.rb +6 -1
  86. data/lib/mocha/mock.rb +46 -31
  87. data/lib/mocha/mockery.rb +25 -61
  88. data/lib/mocha/names.rb +1 -1
  89. data/lib/mocha/object_methods.rb +13 -19
  90. data/lib/mocha/parameter_matchers.rb +1 -1
  91. data/lib/mocha/parameter_matchers/all_of.rb +1 -1
  92. data/lib/mocha/parameter_matchers/any_of.rb +1 -1
  93. data/lib/mocha/parameter_matchers/equivalent_uri.rb +0 -9
  94. data/lib/mocha/parameter_matchers/includes.rb +2 -0
  95. data/lib/mocha/parameter_matchers/instance_methods.rb +18 -0
  96. data/lib/mocha/raised_exception.rb +11 -0
  97. data/lib/mocha/return_values.rb +3 -3
  98. data/lib/mocha/setup.rb +5 -0
  99. data/lib/mocha/single_return_value.rb +2 -1
  100. data/lib/mocha/singleton_class.rb +9 -0
  101. data/lib/mocha/stubbed_method.rb +127 -0
  102. data/lib/mocha/test_unit.rb +6 -1
  103. data/lib/mocha/thrower.rb +2 -1
  104. data/lib/mocha/thrown_object.rb +12 -0
  105. data/lib/mocha/version.rb +1 -1
  106. data/lib/mocha/yield_parameters.rb +5 -11
  107. data/mocha.gemspec +1 -3
  108. data/test/acceptance/acceptance_test_helper.rb +7 -0
  109. data/test/acceptance/bug_18914_test.rb +0 -1
  110. data/test/acceptance/bug_21465_test.rb +0 -1
  111. data/test/acceptance/bug_21563_test.rb +0 -1
  112. data/test/acceptance/display_matching_invocations_alongside_expectations_test.rb +69 -0
  113. data/test/acceptance/exception_rescue_test.rb +1 -2
  114. data/test/acceptance/expectations_on_multiple_methods_test.rb +0 -1
  115. data/test/acceptance/expected_invocation_count_test.rb +2 -3
  116. data/test/acceptance/failure_messages_test.rb +16 -1
  117. data/test/acceptance/issue_272_test.rb +1 -2
  118. data/test/acceptance/issue_65_test.rb +0 -1
  119. data/test/acceptance/issue_70_test.rb +0 -1
  120. data/test/acceptance/mocha_example_test.rb +0 -1
  121. data/test/acceptance/mocha_test_result_test.rb +0 -1
  122. data/test/acceptance/mock_built_with_first_argument_type_being_string_test.rb +99 -0
  123. data/test/acceptance/mock_test.rb +47 -6
  124. data/test/acceptance/mocked_methods_dispatch_test.rb +0 -1
  125. data/test/acceptance/multiple_expectations_failure_message_test.rb +0 -1
  126. data/test/acceptance/multiple_yielding_test.rb +56 -0
  127. data/test/acceptance/optional_parameters_test.rb +0 -1
  128. data/test/acceptance/parameter_matcher_test.rb +0 -1
  129. data/test/acceptance/partial_mocks_test.rb +0 -1
  130. data/test/acceptance/prepend_test.rb +0 -1
  131. data/test/acceptance/prevent_use_of_mocha_outside_test_test.rb +0 -1
  132. data/test/acceptance/raise_exception_test.rb +0 -1
  133. data/test/acceptance/return_value_test.rb +0 -1
  134. data/test/acceptance/sequence_test.rb +0 -1
  135. data/test/acceptance/states_test.rb +0 -1
  136. data/test/acceptance/stub_any_instance_method_defined_on_superclass_test.rb +1 -2
  137. data/test/acceptance/stub_any_instance_method_test.rb +20 -1
  138. data/test/acceptance/stub_class_method_defined_on_active_record_association_proxy_test.rb +0 -1
  139. data/test/acceptance/stub_class_method_defined_on_class_test.rb +0 -1
  140. data/test/acceptance/stub_class_method_defined_on_module_test.rb +0 -1
  141. data/test/acceptance/stub_class_method_defined_on_superclass_test.rb +1 -2
  142. data/test/acceptance/stub_everything_test.rb +0 -1
  143. data/test/acceptance/stub_instance_method_defined_on_active_record_association_proxy_test.rb +0 -1
  144. data/test/acceptance/stub_instance_method_defined_on_class_and_aliased_test.rb +0 -1
  145. data/test/acceptance/stub_instance_method_defined_on_class_test.rb +0 -1
  146. data/test/acceptance/stub_instance_method_defined_on_kernel_module_test.rb +0 -1
  147. data/test/acceptance/stub_instance_method_defined_on_module_test.rb +0 -1
  148. data/test/acceptance/stub_instance_method_defined_on_object_class_test.rb +0 -1
  149. data/test/acceptance/stub_instance_method_defined_on_singleton_class_test.rb +0 -1
  150. data/test/acceptance/stub_instance_method_defined_on_superclass_test.rb +0 -1
  151. data/test/acceptance/stub_method_defined_on_module_and_aliased_test.rb +0 -1
  152. data/test/acceptance/stub_module_method_test.rb +0 -1
  153. data/test/acceptance/stub_test.rb +0 -1
  154. data/test/acceptance/stubba_example_test.rb +0 -1
  155. data/test/acceptance/stubba_test_result_test.rb +0 -1
  156. data/test/acceptance/stubbing_error_backtrace_test.rb +4 -5
  157. data/test/acceptance/stubbing_frozen_object_test.rb +0 -1
  158. data/test/acceptance/stubbing_method_accepting_block_parameter_test.rb +0 -1
  159. data/test/acceptance/stubbing_method_unnecessarily_test.rb +5 -5
  160. data/test/acceptance/stubbing_nil_test.rb +5 -5
  161. data/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +27 -11
  162. data/test/acceptance/stubbing_non_existent_class_method_test.rb +11 -11
  163. data/test/acceptance/stubbing_non_existent_instance_method_test.rb +11 -11
  164. data/test/acceptance/stubbing_non_public_any_instance_method_test.rb +8 -8
  165. data/test/acceptance/stubbing_non_public_class_method_test.rb +9 -9
  166. data/test/acceptance/stubbing_non_public_instance_method_test.rb +9 -9
  167. data/test/acceptance/stubbing_on_non_mock_object_test.rb +5 -5
  168. data/test/acceptance/stubbing_same_class_method_on_parent_and_child_classes_test.rb +0 -1
  169. data/test/acceptance/throw_test.rb +0 -1
  170. data/test/acceptance/unexpected_invocation_test.rb +0 -1
  171. data/test/acceptance/unstubbing_test.rb +0 -1
  172. data/test/acceptance/yielding_test.rb +78 -0
  173. data/test/integration/shared_tests.rb +5 -3
  174. data/test/method_definer.rb +11 -17
  175. data/test/test_runner.rb +2 -0
  176. data/test/unit/any_instance_method_test.rb +60 -35
  177. data/test/unit/cardinality_test.rb +41 -23
  178. data/test/unit/central_test.rb +0 -1
  179. data/test/unit/class_methods_test.rb +1 -1
  180. data/test/unit/configuration_test.rb +12 -12
  181. data/test/unit/exception_raiser_test.rb +10 -5
  182. data/test/unit/expectation_list_test.rb +13 -11
  183. data/test/unit/expectation_test.rb +117 -103
  184. data/test/unit/instance_method_test.rb +282 -0
  185. data/test/unit/mock_test.rb +28 -19
  186. data/test/unit/mockery_test.rb +8 -11
  187. data/test/unit/module_methods_test.rb +2 -3
  188. data/test/unit/object_inspect_test.rb +6 -4
  189. data/test/unit/object_methods_test.rb +3 -2
  190. data/test/unit/parameter_matchers/equivalent_uri_test.rb +0 -9
  191. data/test/unit/parameter_matchers/has_entries_test.rb +1 -1
  192. data/test/unit/parameter_matchers/has_entry_test.rb +1 -1
  193. data/test/unit/parameter_matchers/has_key_test.rb +1 -1
  194. data/test/unit/parameter_matchers/has_value_test.rb +1 -1
  195. data/test/unit/parameter_matchers/includes_test.rb +1 -1
  196. data/test/unit/parameter_matchers/responds_with_test.rb +1 -1
  197. data/test/unit/return_values_test.rb +25 -20
  198. data/test/unit/single_return_value_test.rb +6 -1
  199. data/test/unit/thrower_test.rb +7 -2
  200. data/test/unit/yield_parameters_test.rb +35 -53
  201. metadata +18 -35
  202. data/docs/Mocha/UnexpectedInvocation.html +0 -140
  203. data/lib/mocha/class_method.rb +0 -113
  204. data/lib/mocha/mini_test.rb +0 -5
  205. data/lib/mocha/module_method.rb +0 -6
  206. data/lib/mocha/module_methods.rb +0 -10
  207. data/lib/mocha/multiple_yields.rb +0 -15
  208. data/lib/mocha/no_yields.rb +0 -5
  209. data/lib/mocha/parameter_matchers/object.rb +0 -15
  210. data/lib/mocha/single_yield.rb +0 -13
  211. data/lib/mocha/standalone.rb +0 -4
  212. data/lib/mocha/unexpected_invocation.rb +0 -24
  213. data/lib/mocha_standalone.rb +0 -4
  214. data/test/acceptance/mock_with_initializer_block_test.rb +0 -56
  215. data/test/unit/class_method_test.rb +0 -254
  216. data/test/unit/multiple_yields_test.rb +0 -16
  217. data/test/unit/no_yields_test.rb +0 -16
  218. data/test/unit/single_yield_test.rb +0 -16
@@ -1,10 +1,12 @@
1
1
  require File.expand_path('../../test_helper', __FILE__)
2
2
  require 'mocha/expectation_list'
3
3
  require 'mocha/expectation'
4
+ require 'mocha/invocation'
4
5
  require 'set'
5
6
  require 'method_definer'
6
7
 
7
8
  class ExpectationListTest < Mocha::TestCase
9
+ include MethodDefiner
8
10
  include Mocha
9
11
 
10
12
  def test_should_return_added_expectation
@@ -19,7 +21,7 @@ class ExpectationListTest < Mocha::TestCase
19
21
  expectation2 = Expectation.new(nil, :my_method).with(:argument3, :argument4)
20
22
  expectation_list.add(expectation1)
21
23
  expectation_list.add(expectation2)
22
- assert_same expectation1, expectation_list.match(:my_method, :argument1, :argument2)
24
+ assert_same expectation1, expectation_list.match(Invocation.new(:irrelevant, :my_method, :argument1, :argument2))
23
25
  end
24
26
 
25
27
  def test_should_remove_all_expectations_matching_method_name
@@ -31,9 +33,9 @@ class ExpectationListTest < Mocha::TestCase
31
33
  expectation_list.add(expectation2)
32
34
  expectation_list.add(expectation3)
33
35
  expectation_list.remove_all_matching_method(:method_one)
34
- assert_nil expectation_list.match(:method_one, :argument1, :argument2)
35
- assert_nil expectation_list.match(:method_one, :argument3, :argument4)
36
- assert_same expectation3, expectation_list.match(:method_two)
36
+ assert_nil expectation_list.match(Invocation.new(:irrelevant, :method_one, :argument1, :argument2))
37
+ assert_nil expectation_list.match(Invocation.new(:irrelevant, :method_one, :argument3, :argument4))
38
+ assert_same expectation3, expectation_list.match(Invocation.new(:irrelevant, :method_two))
37
39
  end
38
40
 
39
41
  def test_should_find_most_recent_matching_expectation
@@ -42,29 +44,29 @@ class ExpectationListTest < Mocha::TestCase
42
44
  expectation2 = Expectation.new(nil, :my_method).with(:argument1, :argument2)
43
45
  expectation_list.add(expectation1)
44
46
  expectation_list.add(expectation2)
45
- assert_same expectation2, expectation_list.match(:my_method, :argument1, :argument2)
47
+ assert_same expectation2, expectation_list.match(Invocation.new(:irrelevant, :my_method, :argument1, :argument2))
46
48
  end
47
49
 
48
50
  def test_should_find_matching_expectation_allowing_invocation
49
51
  expectation_list = ExpectationList.new
50
52
  expectation1 = Expectation.new(nil, :my_method).with(:argument1, :argument2)
51
53
  expectation2 = Expectation.new(nil, :my_method).with(:argument3, :argument4)
52
- expectation1.define_instance_method(:invocations_allowed?) { true }
53
- expectation2.define_instance_method(:invocations_allowed?) { true }
54
+ define_instance_method(expectation1, :invocations_allowed?) { true }
55
+ define_instance_method(expectation2, :invocations_allowed?) { true }
54
56
  expectation_list.add(expectation1)
55
57
  expectation_list.add(expectation2)
56
- assert_same expectation1, expectation_list.match_allowing_invocation(:my_method, :argument1, :argument2)
58
+ assert_same expectation1, expectation_list.match_allowing_invocation(Invocation.new(:irrelevant, :my_method, :argument1, :argument2))
57
59
  end
58
60
 
59
61
  def test_should_find_most_recent_matching_expectation_allowing_invocation
60
62
  expectation_list = ExpectationList.new
61
63
  expectation1 = Expectation.new(nil, :my_method)
62
64
  expectation2 = Expectation.new(nil, :my_method)
63
- expectation1.define_instance_method(:invocations_allowed?) { true }
64
- expectation2.define_instance_method(:invocations_allowed?) { false }
65
+ define_instance_method(expectation1, :invocations_allowed?) { true }
66
+ define_instance_method(expectation2, :invocations_allowed?) { false }
65
67
  expectation_list.add(expectation1)
66
68
  expectation_list.add(expectation2)
67
- assert_same expectation1, expectation_list.match_allowing_invocation(:my_method)
69
+ assert_same expectation1, expectation_list.match_allowing_invocation(Invocation.new(:irrelevant, :my_method))
68
70
  end
69
71
 
70
72
  def test_should_combine_two_expectation_lists_into_one
@@ -1,10 +1,14 @@
1
1
  require File.expand_path('../../test_helper', __FILE__)
2
- require 'method_definer'
3
2
  require 'mocha/expectation'
3
+ require 'mocha/invocation'
4
4
  require 'mocha/sequence'
5
+ require 'mocha/configuration'
6
+ require 'mocha/deprecation'
5
7
  require 'execution_point'
6
8
  require 'simple_counter'
9
+ require 'deprecation_disabler'
7
10
 
11
+ # rubocop:disable Metrics/ClassLength
8
12
  class ExpectationTest < Mocha::TestCase
9
13
  include Mocha
10
14
 
@@ -12,90 +16,85 @@ class ExpectationTest < Mocha::TestCase
12
16
  Expectation.new(nil, :expected_method)
13
17
  end
14
18
 
19
+ def invoke(expectation, &block)
20
+ expectation.invoke(Invocation.new(:irrelevant, :expected_method, &block))
21
+ end
22
+
15
23
  def test_should_match_calls_to_same_method_with_any_parameters
16
- assert new_expectation.match?(:expected_method, 1, 2, 3)
24
+ assert new_expectation.match?(Invocation.new(:irrelevant, :expected_method, 1, 2, 3))
17
25
  end
18
26
 
19
27
  def test_should_match_calls_to_same_method_with_exactly_zero_parameters
20
- expectation = new_expectation.with
21
- assert expectation.match?(:expected_method)
28
+ assert new_expectation.with.match?(Invocation.new(:irrelevant, :expected_method))
22
29
  end
23
30
 
24
31
  def test_should_not_match_calls_to_same_method_with_more_than_zero_parameters
25
- expectation = new_expectation.with
26
- assert !expectation.match?(:expected_method, 1, 2, 3)
32
+ assert !new_expectation.with.match?(Invocation.new(:irrelevant, :expected_method, 1, 2, 3))
27
33
  end
28
34
 
29
35
  def test_should_match_calls_to_same_method_with_expected_parameter_values
30
- expectation = new_expectation.with(1, 2, 3)
31
- assert expectation.match?(:expected_method, 1, 2, 3)
36
+ assert new_expectation.with(1, 2, 3).match?(Invocation.new(:irrelevant, :expected_method, 1, 2, 3))
32
37
  end
33
38
 
34
39
  def test_should_match_calls_to_same_method_with_parameters_constrained_as_expected
35
40
  expectation = new_expectation.with { |x, y, z| x + y == z }
36
- assert expectation.match?(:expected_method, 1, 2, 3)
41
+ assert expectation.match?(Invocation.new(:irrelevant, :expected_method, 1, 2, 3))
37
42
  end
38
43
 
39
44
  def test_should_not_match_calls_to_different_method_with_parameters_constrained_as_expected
40
45
  expectation = new_expectation.with { |x, y, z| x + y == z }
41
- assert !expectation.match?(:different_method, 1, 2, 3)
46
+ assert !expectation.match?(Invocation.new(:irrelevant, :different_method, 1, 2, 3))
42
47
  end
43
48
 
44
49
  def test_should_not_match_calls_to_different_methods_with_no_parameters
45
- assert !new_expectation.match?(:unexpected_method)
50
+ assert !new_expectation.match?(Invocation.new(:irrelevant, :unexpected_method))
46
51
  end
47
52
 
48
53
  def test_should_not_match_calls_to_same_method_with_too_few_parameters
49
- expectation = new_expectation.with(1, 2, 3)
50
- assert !expectation.match?(:unexpected_method, 1, 2)
54
+ assert !new_expectation.with(1, 2, 3).match?(Invocation.new(:irrelevant, :expected_method, 1, 2))
51
55
  end
52
56
 
53
57
  def test_should_not_match_calls_to_same_method_with_too_many_parameters
54
- expectation = new_expectation.with(1, 2)
55
- assert !expectation.match?(:unexpected_method, 1, 2, 3)
58
+ assert !new_expectation.with(1, 2).match?(Invocation.new(:irrelevant, :expected_method, 1, 2, 3))
56
59
  end
57
60
 
58
61
  def test_should_not_match_calls_to_same_method_with_unexpected_parameter_values
59
- expectation = new_expectation.with(1, 2, 3)
60
- assert !expectation.match?(:unexpected_method, 1, 0, 3)
62
+ assert !new_expectation.with(1, 2, 3).match?(Invocation.new(:irrelevant, :expected_method, 1, 0, 3))
61
63
  end
62
64
 
63
65
  def test_should_not_match_calls_to_same_method_with_parameters_not_constrained_as_expected
64
66
  expectation = new_expectation.with { |x, y, z| x + y == z }
65
- assert !expectation.match?(:expected_method, 1, 0, 3)
67
+ assert !expectation.match?(Invocation.new(:irrelevant, :expected_method, 1, 0, 3))
66
68
  end
67
69
 
68
70
  def test_should_allow_invocations_until_expected_invocation_count_is_one_and_actual_invocation_count_would_be_two
69
71
  expectation = new_expectation.times(1)
70
72
  assert expectation.invocations_allowed?
71
- expectation.invoke
73
+ invoke(expectation)
72
74
  assert !expectation.invocations_allowed?
73
75
  end
74
76
 
75
77
  def test_should_allow_invocations_until_expected_invocation_count_is_two_and_actual_invocation_count_would_be_three
76
78
  expectation = new_expectation.times(2)
77
- assert expectation.invocations_allowed?
78
- expectation.invoke
79
- assert expectation.invocations_allowed?
80
- expectation.invoke
79
+ 2.times do
80
+ assert expectation.invocations_allowed?
81
+ invoke(expectation)
82
+ end
81
83
  assert !expectation.invocations_allowed?
82
84
  end
83
85
 
84
86
  def test_should_allow_invocations_until_expected_invocation_count_is_a_range_from_two_to_three_and_actual_invocation_count_would_be_four
85
87
  expectation = new_expectation.times(2..3)
86
- assert expectation.invocations_allowed?
87
- expectation.invoke
88
- assert expectation.invocations_allowed?
89
- expectation.invoke
90
- assert expectation.invocations_allowed?
91
- expectation.invoke
88
+ 3.times do
89
+ assert expectation.invocations_allowed?
90
+ invoke(expectation)
91
+ end
92
92
  assert !expectation.invocations_allowed?
93
93
  end
94
94
 
95
95
  def test_should_store_provided_backtrace
96
96
  backtrace = Object.new
97
- expectation = Expectation.new(nil, :expected_method, backtrace)
98
- assert_equal backtrace, expectation.backtrace
97
+ assert_equal backtrace, Expectation.new(nil, :expected_method, backtrace).backtrace
99
98
  end
100
99
 
101
100
  # rubocop:disable Style/Semicolon
@@ -107,217 +106,233 @@ class ExpectationTest < Mocha::TestCase
107
106
 
108
107
  def test_should_not_yield
109
108
  yielded = false
110
- new_expectation.invoke { yielded = true }
109
+ invoke(new_expectation) { yielded = true }
111
110
  assert_equal false, yielded
112
111
  end
113
112
 
114
113
  def test_should_yield_no_parameters
115
- expectation = new_expectation.yields
116
114
  yielded_parameters = nil
117
- expectation.invoke { |*parameters| yielded_parameters = parameters }
115
+ invoke(new_expectation.yields) { |*parameters| yielded_parameters = parameters }
118
116
  assert_equal [], yielded_parameters
119
117
  end
120
118
 
119
+ def test_yield_should_fail_when_the_caller_does_not_provide_a_block
120
+ assert_raises(LocalJumpError) { invoke(new_expectation.yields(:foo)) }
121
+ end
122
+
123
+ def test_yields_should_display_warning_when_caller_does_not_provide_block_and_behaviour_from_v1_9_retained
124
+ Mocha::Configuration.override(:reinstate_undocumented_behaviour_from_v1_9 => true) do
125
+ DeprecationDisabler.disable_deprecations do
126
+ invoke(new_expectation.yields(:foo, 1, [2, 3]))
127
+ end
128
+ end
129
+ assert message = Deprecation.messages.last
130
+ assert message.include?('Stubbed method was instructed to yield (:foo, 1, [2, 3])')
131
+ assert message.include?('but no block was given by invocation: :irrelevant.expected_method()')
132
+ assert message.include?('This will raise a LocalJumpError in the future.')
133
+ end
134
+
135
+ def test_multiple_yields_should_display_warning_when_caller_does_not_provide_block_and_behaviour_from_v1_9_retained
136
+ Mocha::Configuration.override(:reinstate_undocumented_behaviour_from_v1_9 => true) do
137
+ DeprecationDisabler.disable_deprecations do
138
+ invoke(new_expectation.multiple_yields(:foo, 1, [2, 3]))
139
+ end
140
+ end
141
+ assert message = Deprecation.messages.last
142
+ assert message.include?('Stubbed method was instructed to yield (2, 3)')
143
+ assert message.include?('but no block was given by invocation: :irrelevant.expected_method()')
144
+ assert message.include?('This will raise a LocalJumpError in the future.')
145
+ end
146
+
121
147
  def test_should_yield_with_specified_parameters
122
- expectation = new_expectation.yields(1, 2, 3)
123
148
  yielded_parameters = nil
124
- expectation.invoke { |*parameters| yielded_parameters = parameters }
149
+ invoke(new_expectation.yields(1, 2, 3)) { |*parameters| yielded_parameters = parameters }
125
150
  assert_equal [1, 2, 3], yielded_parameters
126
151
  end
127
152
 
128
153
  def test_should_yield_different_parameters_on_consecutive_invocations
129
154
  expectation = new_expectation.yields(1, 2, 3).yields(4, 5)
130
155
  yielded_parameters = []
131
- expectation.invoke { |*parameters| yielded_parameters << parameters }
132
- expectation.invoke { |*parameters| yielded_parameters << parameters }
156
+ invoke(expectation) { |*parameters| yielded_parameters << parameters }
157
+ invoke(expectation) { |*parameters| yielded_parameters << parameters }
133
158
  assert_equal [[1, 2, 3], [4, 5]], yielded_parameters
134
159
  end
135
160
 
136
161
  def test_should_yield_multiple_times_for_single_invocation
137
- expectation = new_expectation.multiple_yields([1, 2, 3], [4, 5])
138
162
  yielded_parameters = []
139
- expectation.invoke { |*parameters| yielded_parameters << parameters }
163
+ invoke(new_expectation.multiple_yields([1, 2, 3], [4, 5])) { |*parameters| yielded_parameters << parameters }
140
164
  assert_equal [[1, 2, 3], [4, 5]], yielded_parameters
141
165
  end
142
166
 
143
167
  def test_should_yield_multiple_times_for_first_invocation_and_once_for_second_invocation
144
168
  expectation = new_expectation.multiple_yields([1, 2, 3], [4, 5]).then.yields(6, 7)
145
169
  yielded_parameters = []
146
- expectation.invoke { |*parameters| yielded_parameters << parameters }
147
- expectation.invoke { |*parameters| yielded_parameters << parameters }
170
+ invoke(expectation) { |*parameters| yielded_parameters << parameters }
171
+ invoke(expectation) { |*parameters| yielded_parameters << parameters }
148
172
  assert_equal [[1, 2, 3], [4, 5], [6, 7]], yielded_parameters
149
173
  end
150
174
 
151
175
  def test_should_return_specified_value
152
- expectation = new_expectation.returns(99)
153
- assert_equal 99, expectation.invoke
176
+ assert_equal 99, invoke(new_expectation.returns(99))
154
177
  end
155
178
 
156
179
  def test_should_return_same_specified_value_multiple_times
157
180
  expectation = new_expectation.returns(99)
158
- assert_equal 99, expectation.invoke
159
- assert_equal 99, expectation.invoke
181
+ assert_equal 99, invoke(expectation)
182
+ assert_equal 99, invoke(expectation)
160
183
  end
161
184
 
162
185
  def test_should_return_specified_values_on_consecutive_calls
163
186
  expectation = new_expectation.returns(99, 100, 101)
164
- assert_equal 99, expectation.invoke
165
- assert_equal 100, expectation.invoke
166
- assert_equal 101, expectation.invoke
187
+ assert_equal 99, invoke(expectation)
188
+ assert_equal 100, invoke(expectation)
189
+ assert_equal 101, invoke(expectation)
167
190
  end
168
191
 
169
192
  def test_should_return_specified_values_on_consecutive_calls_even_if_values_are_modified
170
193
  values = [99, 100, 101]
171
194
  expectation = new_expectation.returns(*values)
172
195
  values.shift
173
- assert_equal 99, expectation.invoke
174
- assert_equal 100, expectation.invoke
175
- assert_equal 101, expectation.invoke
196
+ assert_equal 99, invoke(expectation)
197
+ assert_equal 100, invoke(expectation)
198
+ assert_equal 101, invoke(expectation)
176
199
  end
177
200
 
178
201
  def test_should_return_nil_by_default
179
- assert_nil new_expectation.invoke
202
+ assert_nil invoke(new_expectation)
180
203
  end
181
204
 
182
205
  def test_should_return_nil_if_no_value_specified
183
- expectation = new_expectation.returns
184
- assert_nil expectation.invoke
206
+ assert_nil invoke(new_expectation.returns)
185
207
  end
186
208
 
187
209
  def test_should_raise_runtime_exception
188
- expectation = new_expectation.raises
189
- assert_raise(RuntimeError) { expectation.invoke }
210
+ assert_raises(RuntimeError) { invoke(new_expectation.raises) }
190
211
  end
191
212
 
192
213
  def test_should_raise_custom_exception
193
214
  exception = Class.new(Exception)
194
- expectation = new_expectation.raises(exception)
195
- assert_raise(exception) { expectation.invoke }
215
+ assert_raises(exception) { invoke(new_expectation.raises(exception)) }
196
216
  end
197
217
 
198
218
  def test_should_raise_same_instance_of_custom_exception
199
219
  exception_klass = Class.new(StandardError)
200
220
  expected_exception = exception_klass.new
201
- expectation = new_expectation.raises(expected_exception)
202
- actual_exception = assert_raise(exception_klass) { expectation.invoke }
221
+ actual_exception = assert_raises(exception_klass) { invoke(new_expectation.raises(expected_exception)) }
203
222
  assert_same expected_exception, actual_exception
204
223
  end
205
224
 
206
225
  def test_should_use_the_default_exception_message
207
- expectation = new_expectation.raises(Exception)
208
- exception = assert_raise(Exception) { expectation.invoke }
226
+ exception = assert_raises(Exception) { invoke(new_expectation.raises(Exception)) }
209
227
  assert_equal Exception.new.message, exception.message
210
228
  end
211
229
 
212
230
  def test_should_raise_custom_exception_with_message
213
231
  exception_msg = 'exception message'
214
- expectation = new_expectation.raises(Exception, exception_msg)
215
- exception = assert_raise(Exception) { expectation.invoke }
232
+ exception = assert_raises(Exception) { invoke(new_expectation.raises(Exception, exception_msg)) }
216
233
  assert_equal exception_msg, exception.message
217
234
  end
218
235
 
219
236
  def test_should_return_values_then_raise_exception
220
237
  expectation = new_expectation.returns(1, 2).then.raises
221
- assert_equal 1, expectation.invoke
222
- assert_equal 2, expectation.invoke
223
- assert_raise(RuntimeError) { expectation.invoke }
238
+ assert_equal 1, invoke(expectation)
239
+ assert_equal 2, invoke(expectation)
240
+ assert_raises(RuntimeError) { invoke(expectation) }
224
241
  end
225
242
 
226
243
  def test_should_raise_exception_then_return_values
227
244
  expectation = new_expectation.raises.then.returns(1, 2)
228
- assert_raise(RuntimeError) { expectation.invoke }
229
- assert_equal 1, expectation.invoke
230
- assert_equal 2, expectation.invoke
245
+ assert_raises(RuntimeError) { invoke(expectation) }
246
+ assert_equal 1, invoke(expectation)
247
+ assert_equal 2, invoke(expectation)
231
248
  end
232
249
 
233
250
  def test_should_verify_successfully_if_expected_call_was_made
234
251
  expectation = new_expectation
235
- expectation.invoke
252
+ invoke(expectation)
236
253
  assert expectation.verified?
237
254
  end
238
255
 
239
256
  def test_should_not_verify_successfully_if_call_expected_once_but_invoked_twice
240
257
  expectation = new_expectation.once
241
- expectation.invoke
242
- expectation.invoke
258
+ invoke(expectation)
259
+ invoke(expectation)
243
260
  assert !expectation.verified?
244
261
  end
245
262
 
246
263
  def test_should_not_verify_successfully_if_call_expected_once_but_not_invoked
247
- expectation = new_expectation.once
248
- assert !expectation.verified?
264
+ assert !new_expectation.once.verified?
249
265
  end
250
266
 
251
267
  def test_should_verify_successfully_if_call_expected_once_and_invoked_once
252
268
  expectation = new_expectation.once
253
- expectation.invoke
269
+ invoke(expectation)
254
270
  assert expectation.verified?
255
271
  end
256
272
 
257
273
  def test_should_not_verify_successfully_if_call_expected_twice_and_invoked_three_times
258
274
  expectation = new_expectation.twice
259
- expectation.invoke
260
- expectation.invoke
261
- expectation.invoke
275
+ invoke(expectation)
276
+ invoke(expectation)
277
+ invoke(expectation)
262
278
  assert !expectation.verified?
263
279
  end
264
280
 
265
281
  def test_should_not_verify_successfully_if_call_expected_twice_but_invoked_once
266
282
  expectation = new_expectation.twice
267
- expectation.invoke
283
+ invoke(expectation)
268
284
  assert !expectation.verified?
269
285
  end
270
286
 
271
287
  def test_should_verify_successfully_if_call_expected_twice_and_invoked_twice
272
288
  expectation = new_expectation.twice
273
- expectation.invoke
274
- expectation.invoke
289
+ invoke(expectation)
290
+ invoke(expectation)
275
291
  assert expectation.verified?
276
292
  end
277
293
 
278
294
  def test_should_verify_successfully_if_expected_call_was_made_at_least_once
279
295
  expectation = new_expectation.at_least_once
280
- 3.times { expectation.invoke }
296
+ 3.times { invoke(expectation) }
281
297
  assert expectation.verified?
282
298
  end
283
299
 
284
300
  def test_should_not_verify_successfully_if_expected_call_was_not_made_at_least_once
285
301
  expectation = new_expectation.with(1, 2, 3).at_least_once
286
302
  assert !expectation.verified?
287
- assert_match(/expected at least once, not yet invoked/i, expectation.mocha_inspect)
303
+ assert_match(/expected at least once, invoked never/i, expectation.mocha_inspect)
288
304
  end
289
305
 
290
306
  def test_should_verify_successfully_if_expected_call_was_made_expected_number_of_times
291
307
  expectation = new_expectation.times(2)
292
- 2.times { expectation.invoke }
308
+ 2.times { invoke(expectation) }
293
309
  assert expectation.verified?
294
310
  end
295
311
 
296
312
  def test_should_not_verify_successfully_if_expected_call_was_made_too_few_times
297
313
  expectation = new_expectation.times(2)
298
- 1.times { expectation.invoke }
314
+ 1.times { invoke(expectation) }
299
315
  assert !expectation.verified?
300
316
  assert_match(/expected exactly twice, invoked once/i, expectation.mocha_inspect)
301
317
  end
302
318
 
303
319
  def test_should_not_verify_successfully_if_expected_call_was_made_too_many_times
304
320
  expectation = new_expectation.times(2)
305
- 3.times { expectation.invoke }
321
+ 3.times { invoke(expectation) }
306
322
  assert !expectation.verified?
307
323
  end
308
324
 
309
325
  def test_should_increment_assertion_counter_for_expectation_because_it_does_need_verifyng
310
326
  expectation = new_expectation
311
- expectation.invoke
327
+ invoke(expectation)
312
328
  assertion_counter = SimpleCounter.new
313
329
  expectation.verified?(assertion_counter)
314
330
  assert_equal 1, assertion_counter.count
315
331
  end
316
332
 
317
333
  def test_should_not_increment_assertion_counter_for_stub_because_it_does_not_need_verifying
318
- stub = Expectation.new(nil, :expected_method).at_least(0)
319
334
  assertion_counter = SimpleCounter.new
320
- stub.verified?(assertion_counter)
335
+ Expectation.new(nil, :expected_method).at_least(0).verified?(assertion_counter)
321
336
  assert_equal 0, assertion_counter.count
322
337
  end
323
338
 
@@ -381,7 +396,7 @@ class ExpectationTest < Mocha::TestCase
381
396
  expectation = Expectation.new(nil, :method_one)
382
397
  expectation.add_ordering_constraint(constraint_one)
383
398
  expectation.add_ordering_constraint(constraint_two)
384
- assert expectation.match?(:method_one)
399
+ assert expectation.match?(Invocation.new(:irrelevant, :method_one))
385
400
  end
386
401
 
387
402
  def test_should_not_match_if_one_ordering_constraints_does_not_allow_invocation_now
@@ -390,29 +405,28 @@ class ExpectationTest < Mocha::TestCase
390
405
  expectation = Expectation.new(nil, :method_one)
391
406
  expectation.add_ordering_constraint(constraint_one)
392
407
  expectation.add_ordering_constraint(constraint_two)
393
- assert !expectation.match?(:method_one)
408
+ assert !expectation.match?(Invocation.new(:irrelevant, :method_one))
394
409
  end
395
410
 
396
411
  def test_should_not_be_satisfied_when_required_invocation_has_not_been_made
397
- expectation = Expectation.new(nil, :method_one).times(1)
398
- assert !expectation.satisfied?
412
+ assert !Expectation.new(nil, :method_one).times(1).satisfied?
399
413
  end
400
414
 
401
415
  def test_should_be_satisfied_when_required_invocation_has_been_made
402
416
  expectation = Expectation.new(nil, :method_one).times(1)
403
- expectation.invoke
417
+ invoke(expectation)
404
418
  assert expectation.satisfied?
405
419
  end
406
420
 
407
421
  def test_should_not_be_satisfied_when_minimum_number_of_invocations_has_not_been_made
408
422
  expectation = Expectation.new(nil, :method_one).at_least(2)
409
- expectation.invoke
423
+ invoke(expectation)
410
424
  assert !expectation.satisfied?
411
425
  end
412
426
 
413
427
  def test_should_be_satisfied_when_minimum_number_of_invocations_has_been_made
414
428
  expectation = Expectation.new(nil, :method_one).at_least(2)
415
- 2.times { expectation.invoke }
429
+ 2.times { invoke(expectation) }
416
430
  assert expectation.satisfied?
417
431
  end
418
432
 
@@ -457,7 +471,7 @@ class ExpectationTest < Mocha::TestCase
457
471
 
458
472
  expectation.then(state)
459
473
 
460
- expectation.invoke
474
+ invoke(expectation)
461
475
  assert state.active?
462
476
  end
463
477
 
@@ -466,10 +480,10 @@ class ExpectationTest < Mocha::TestCase
466
480
  expectation = Expectation.new(nil, :method_one)
467
481
 
468
482
  expectation.when(state)
469
- assert !expectation.match?(:method_one)
483
+ assert !expectation.match?(Invocation.new(:irrelevant, :method_one))
470
484
 
471
485
  state.activate
472
- assert expectation.match?(:method_one)
486
+ assert expectation.match?(Invocation.new(:irrelevant, :method_one))
473
487
  end
474
488
 
475
489
  def test_should_include_default_representation_of_object_in_inspect
@@ -477,8 +491,7 @@ class ExpectationTest < Mocha::TestCase
477
491
  class << object
478
492
  define_method(:inspect) { 'mock' }
479
493
  end
480
- expectation = Expectation.new(object, :method_one)
481
- assert_match Regexp.new('^#<Expectation:0x[0-9A-Fa-f]{1,12} .* >$'), expectation.inspect
494
+ assert_match Regexp.new('^#<Expectation:0x[0-9A-Fa-f]{1,12} .* >$'), Expectation.new(object, :method_one).inspect
482
495
  end
483
496
 
484
497
  def test_should_include_output_of_mocha_inspect_in_inspect
@@ -490,3 +503,4 @@ class ExpectationTest < Mocha::TestCase
490
503
  assert expectation.inspect.include?(expectation.mocha_inspect)
491
504
  end
492
505
  end
506
+ # rubocop:enable Metrics/ClassLength