mocha 1.9.0 → 1.11.1

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