mocha 1.7.0 → 1.10.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (212) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +3 -0
  3. data/.rubocop_todo.yml +7 -30
  4. data/CONTRIBUTING.md +4 -9
  5. data/README.md +55 -27
  6. data/RELEASE.md +89 -0
  7. data/Rakefile +15 -16
  8. data/bin/build-matrix +15 -2
  9. data/docs/CNAME +1 -0
  10. data/docs/Mocha.html +254 -0
  11. data/docs/Mocha/API.html +1287 -0
  12. data/docs/Mocha/ClassMethods.html +264 -0
  13. data/docs/Mocha/Configuration.html +1525 -0
  14. data/docs/Mocha/Expectation.html +2654 -0
  15. data/docs/Mocha/ExpectationError.html +152 -0
  16. data/docs/Mocha/ExpectationErrorFactory.html +260 -0
  17. data/docs/Mocha/Hooks.html +370 -0
  18. data/docs/Mocha/Integration.html +125 -0
  19. data/docs/Mocha/Integration/MiniTest.html +123 -0
  20. data/docs/Mocha/Integration/MiniTest/Adapter.html +164 -0
  21. data/docs/Mocha/Integration/TestUnit.html +123 -0
  22. data/docs/Mocha/Integration/TestUnit/Adapter.html +164 -0
  23. data/docs/Mocha/Mock.html +1237 -0
  24. data/docs/Mocha/ObjectMethods.html +765 -0
  25. data/docs/Mocha/ParameterMatchers.html +2961 -0
  26. data/docs/Mocha/ParameterMatchers/AllOf.html +153 -0
  27. data/docs/Mocha/ParameterMatchers/AnyOf.html +153 -0
  28. data/docs/Mocha/ParameterMatchers/AnyParameters.html +153 -0
  29. data/docs/Mocha/ParameterMatchers/Anything.html +153 -0
  30. data/docs/Mocha/ParameterMatchers/Base.html +441 -0
  31. data/docs/Mocha/ParameterMatchers/Equals.html +153 -0
  32. data/docs/Mocha/ParameterMatchers/EquivalentUri.html +153 -0
  33. data/docs/Mocha/ParameterMatchers/HasEntries.html +153 -0
  34. data/docs/Mocha/ParameterMatchers/HasEntry.html +153 -0
  35. data/docs/Mocha/ParameterMatchers/HasKey.html +153 -0
  36. data/docs/Mocha/ParameterMatchers/HasValue.html +153 -0
  37. data/docs/Mocha/ParameterMatchers/Includes.html +153 -0
  38. data/docs/Mocha/ParameterMatchers/InstanceOf.html +153 -0
  39. data/docs/Mocha/ParameterMatchers/IsA.html +153 -0
  40. data/docs/Mocha/ParameterMatchers/KindOf.html +153 -0
  41. data/docs/Mocha/ParameterMatchers/Not.html +153 -0
  42. data/docs/Mocha/ParameterMatchers/Optionally.html +153 -0
  43. data/docs/Mocha/ParameterMatchers/RegexpMatches.html +153 -0
  44. data/docs/Mocha/ParameterMatchers/RespondsWith.html +153 -0
  45. data/docs/Mocha/ParameterMatchers/YamlEquivalent.html +153 -0
  46. data/docs/Mocha/Sequence.html +149 -0
  47. data/docs/Mocha/StateMachine.html +527 -0
  48. data/docs/Mocha/StateMachine/State.html +140 -0
  49. data/docs/Mocha/StateMachine/StatePredicate.html +140 -0
  50. data/docs/Mocha/StubbingError.html +150 -0
  51. data/docs/_index.html +519 -0
  52. data/docs/class_list.html +51 -0
  53. data/docs/css/common.css +1 -0
  54. data/docs/css/full_list.css +58 -0
  55. data/docs/css/style.css +496 -0
  56. data/docs/file.COPYING.html +81 -0
  57. data/docs/file.MIT-LICENSE.html +85 -0
  58. data/docs/file.README.html +448 -0
  59. data/docs/file.RELEASE.html +974 -0
  60. data/docs/file_list.html +71 -0
  61. data/docs/frames.html +17 -0
  62. data/docs/index.html +448 -0
  63. data/docs/js/app.js +303 -0
  64. data/docs/js/full_list.js +216 -0
  65. data/docs/js/jquery.js +4 -0
  66. data/docs/method_list.html +635 -0
  67. data/docs/top-level-namespace.html +118 -0
  68. data/gemfiles/Gemfile.minitest.5.11.3 +7 -0
  69. data/init.rb +1 -3
  70. data/lib/mocha.rb +8 -0
  71. data/lib/mocha/any_instance_method.rb +11 -53
  72. data/lib/mocha/api.rb +120 -56
  73. data/lib/mocha/cardinality.rb +26 -11
  74. data/lib/mocha/class_methods.rb +17 -15
  75. data/lib/mocha/configuration.rb +351 -67
  76. data/lib/mocha/deprecation.rb +2 -1
  77. data/lib/mocha/detection/test_unit.rb +1 -3
  78. data/lib/mocha/exception_raiser.rb +2 -1
  79. data/lib/mocha/expectation.rb +56 -58
  80. data/lib/mocha/expectation_error.rb +1 -3
  81. data/lib/mocha/expectation_list.rb +6 -6
  82. data/lib/mocha/inspect.rb +28 -26
  83. data/lib/mocha/instance_method.rb +19 -2
  84. data/lib/mocha/integration.rb +1 -3
  85. data/lib/mocha/integration/mini_test.rb +7 -0
  86. data/lib/mocha/integration/test_unit.rb +7 -0
  87. data/lib/mocha/invocation.rb +73 -0
  88. data/lib/mocha/macos_version.rb +5 -0
  89. data/lib/mocha/minitest.rb +6 -1
  90. data/lib/mocha/mock.rb +46 -31
  91. data/lib/mocha/mockery.rb +25 -61
  92. data/lib/mocha/names.rb +1 -1
  93. data/lib/mocha/object_methods.rb +13 -19
  94. data/lib/mocha/parameter_matchers.rb +1 -1
  95. data/lib/mocha/parameter_matchers/all_of.rb +1 -1
  96. data/lib/mocha/parameter_matchers/any_of.rb +1 -1
  97. data/lib/mocha/parameter_matchers/equivalent_uri.rb +0 -9
  98. data/lib/mocha/parameter_matchers/includes.rb +2 -0
  99. data/lib/mocha/parameter_matchers/instance_methods.rb +18 -0
  100. data/lib/mocha/raised_exception.rb +11 -0
  101. data/lib/mocha/return_values.rb +3 -3
  102. data/lib/mocha/setup.rb +5 -0
  103. data/lib/mocha/single_return_value.rb +2 -1
  104. data/lib/mocha/singleton_class.rb +9 -0
  105. data/lib/mocha/stubbed_method.rb +127 -0
  106. data/lib/mocha/test_unit.rb +6 -1
  107. data/lib/mocha/thrower.rb +2 -1
  108. data/lib/mocha/thrown_object.rb +12 -0
  109. data/lib/mocha/version.rb +1 -1
  110. data/mocha.gemspec +3 -6
  111. data/test/acceptance/acceptance_test_helper.rb +6 -0
  112. data/test/acceptance/bug_18914_test.rb +0 -1
  113. data/test/acceptance/bug_21465_test.rb +0 -1
  114. data/test/acceptance/bug_21563_test.rb +0 -1
  115. data/test/acceptance/display_matching_invocations_alongside_expectations_test.rb +69 -0
  116. data/test/acceptance/exception_rescue_test.rb +1 -2
  117. data/test/acceptance/expectations_on_multiple_methods_test.rb +0 -1
  118. data/test/acceptance/expected_invocation_count_test.rb +2 -3
  119. data/test/acceptance/failure_messages_test.rb +0 -1
  120. data/test/acceptance/issue_272_test.rb +1 -2
  121. data/test/acceptance/issue_65_test.rb +0 -1
  122. data/test/acceptance/issue_70_test.rb +0 -1
  123. data/test/acceptance/mocha_example_test.rb +0 -1
  124. data/test/acceptance/mocha_test_result_test.rb +0 -1
  125. data/test/acceptance/mock_built_with_first_argument_type_being_string_test.rb +99 -0
  126. data/test/acceptance/mock_test.rb +47 -6
  127. data/test/acceptance/mocked_methods_dispatch_test.rb +0 -1
  128. data/test/acceptance/multiple_expectations_failure_message_test.rb +0 -1
  129. data/test/acceptance/optional_parameters_test.rb +0 -1
  130. data/test/acceptance/parameter_matcher_test.rb +0 -1
  131. data/test/acceptance/partial_mocks_test.rb +0 -1
  132. data/test/acceptance/prepend_test.rb +0 -1
  133. data/test/acceptance/prevent_use_of_mocha_outside_test_test.rb +0 -1
  134. data/test/acceptance/raise_exception_test.rb +0 -1
  135. data/test/acceptance/return_value_test.rb +0 -1
  136. data/test/acceptance/sequence_test.rb +0 -1
  137. data/test/acceptance/states_test.rb +0 -1
  138. data/test/acceptance/stub_any_instance_method_defined_on_superclass_test.rb +1 -2
  139. data/test/acceptance/stub_any_instance_method_test.rb +20 -1
  140. data/test/acceptance/stub_class_method_defined_on_active_record_association_proxy_test.rb +0 -1
  141. data/test/acceptance/stub_class_method_defined_on_class_test.rb +0 -1
  142. data/test/acceptance/stub_class_method_defined_on_module_test.rb +0 -1
  143. data/test/acceptance/stub_class_method_defined_on_superclass_test.rb +1 -2
  144. data/test/acceptance/stub_everything_test.rb +0 -1
  145. data/test/acceptance/stub_instance_method_defined_on_active_record_association_proxy_test.rb +0 -1
  146. data/test/acceptance/stub_instance_method_defined_on_class_and_aliased_test.rb +0 -1
  147. data/test/acceptance/stub_instance_method_defined_on_class_test.rb +0 -1
  148. data/test/acceptance/stub_instance_method_defined_on_kernel_module_test.rb +0 -1
  149. data/test/acceptance/stub_instance_method_defined_on_module_test.rb +0 -1
  150. data/test/acceptance/stub_instance_method_defined_on_object_class_test.rb +0 -1
  151. data/test/acceptance/stub_instance_method_defined_on_singleton_class_test.rb +0 -1
  152. data/test/acceptance/stub_instance_method_defined_on_superclass_test.rb +0 -1
  153. data/test/acceptance/stub_method_defined_on_module_and_aliased_test.rb +0 -1
  154. data/test/acceptance/stub_module_method_test.rb +0 -1
  155. data/test/acceptance/stub_test.rb +0 -1
  156. data/test/acceptance/stubba_example_test.rb +0 -1
  157. data/test/acceptance/stubba_test_result_test.rb +0 -1
  158. data/test/acceptance/stubbing_error_backtrace_test.rb +4 -5
  159. data/test/acceptance/stubbing_frozen_object_test.rb +0 -1
  160. data/test/acceptance/stubbing_method_accepting_block_parameter_test.rb +0 -1
  161. data/test/acceptance/stubbing_method_unnecessarily_test.rb +5 -5
  162. data/test/acceptance/stubbing_nil_test.rb +5 -5
  163. data/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +27 -11
  164. data/test/acceptance/stubbing_non_existent_class_method_test.rb +11 -11
  165. data/test/acceptance/stubbing_non_existent_instance_method_test.rb +11 -11
  166. data/test/acceptance/stubbing_non_public_any_instance_method_test.rb +8 -8
  167. data/test/acceptance/stubbing_non_public_class_method_test.rb +9 -9
  168. data/test/acceptance/stubbing_non_public_instance_method_test.rb +9 -9
  169. data/test/acceptance/stubbing_on_non_mock_object_test.rb +5 -5
  170. data/test/acceptance/stubbing_same_class_method_on_parent_and_child_classes_test.rb +0 -1
  171. data/test/acceptance/throw_test.rb +0 -1
  172. data/test/acceptance/unexpected_invocation_test.rb +0 -1
  173. data/test/acceptance/unstubbing_test.rb +0 -1
  174. data/test/integration/shared_tests.rb +5 -3
  175. data/test/method_definer.rb +11 -17
  176. data/test/test_runner.rb +2 -0
  177. data/test/unit/any_instance_method_test.rb +60 -35
  178. data/test/unit/cardinality_test.rb +41 -23
  179. data/test/unit/central_test.rb +0 -1
  180. data/test/unit/class_methods_test.rb +1 -1
  181. data/test/unit/configuration_test.rb +12 -12
  182. data/test/unit/exception_raiser_test.rb +10 -5
  183. data/test/unit/expectation_list_test.rb +13 -11
  184. data/test/unit/expectation_test.rb +103 -103
  185. data/test/unit/instance_method_test.rb +282 -0
  186. data/test/unit/mock_test.rb +28 -19
  187. data/test/unit/mockery_test.rb +8 -11
  188. data/test/unit/module_methods_test.rb +2 -3
  189. data/test/unit/object_inspect_test.rb +16 -4
  190. data/test/unit/object_methods_test.rb +3 -2
  191. data/test/unit/parameter_matchers/equivalent_uri_test.rb +0 -9
  192. data/test/unit/parameter_matchers/has_entries_test.rb +1 -1
  193. data/test/unit/parameter_matchers/has_entry_test.rb +1 -1
  194. data/test/unit/parameter_matchers/has_key_test.rb +1 -1
  195. data/test/unit/parameter_matchers/has_value_test.rb +1 -1
  196. data/test/unit/parameter_matchers/includes_test.rb +1 -1
  197. data/test/unit/parameter_matchers/responds_with_test.rb +1 -1
  198. data/test/unit/return_values_test.rb +25 -20
  199. data/test/unit/single_return_value_test.rb +6 -1
  200. data/test/unit/thrower_test.rb +7 -2
  201. data/yard-templates/default/layout/html/google_analytics.erb +6 -9
  202. metadata +78 -61
  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/parameter_matchers/object.rb +0 -15
  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 -254
@@ -0,0 +1,118 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>
7
+ Top Level Namespace
8
+
9
+ &mdash; Mocha 1.10.2
10
+
11
+ </title>
12
+
13
+ <link rel="stylesheet" href="css/style.css" type="text/css" charset="utf-8" />
14
+
15
+ <link rel="stylesheet" href="css/common.css" type="text/css" charset="utf-8" />
16
+
17
+ <script type="text/javascript" charset="utf-8">
18
+ pathId = "";
19
+ relpath = '';
20
+ </script>
21
+
22
+
23
+ <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
24
+
25
+ <script type="text/javascript" charset="utf-8" src="js/app.js"></script>
26
+
27
+
28
+ </head>
29
+ <body>
30
+ <div class="nav_wrap">
31
+ <iframe id="nav" src="class_list.html?1"></iframe>
32
+ <div id="resizer"></div>
33
+ </div>
34
+
35
+ <div id="main" tabindex="-1">
36
+ <div id="header">
37
+ <div id="menu">
38
+
39
+ <a href="_index.html">Index</a> &raquo;
40
+
41
+
42
+ <span class="title">Top Level Namespace</span>
43
+
44
+ </div>
45
+
46
+ <div id="search">
47
+
48
+ <a class="full_list_link" id="class_list_link"
49
+ href="class_list.html">
50
+
51
+ <svg width="24" height="24">
52
+ <rect x="0" y="4" width="24" height="4" rx="1" ry="1"></rect>
53
+ <rect x="0" y="12" width="24" height="4" rx="1" ry="1"></rect>
54
+ <rect x="0" y="20" width="24" height="4" rx="1" ry="1"></rect>
55
+ </svg>
56
+ </a>
57
+
58
+ </div>
59
+ <div class="clear"></div>
60
+ </div>
61
+
62
+ <div id="content"><h1>Top Level Namespace
63
+
64
+
65
+
66
+ </h1>
67
+ <div class="box_info">
68
+
69
+
70
+
71
+
72
+
73
+
74
+
75
+
76
+
77
+
78
+
79
+ </div>
80
+
81
+ <h2>Defined Under Namespace</h2>
82
+ <p class="children">
83
+
84
+
85
+ <strong class="modules">Modules:</strong> <span class='object_link'><a href="Mocha.html" title="Mocha (module)">Mocha</a></span>
86
+
87
+
88
+
89
+
90
+ </p>
91
+
92
+
93
+
94
+
95
+
96
+
97
+
98
+
99
+
100
+ <script async src="https://www.googletagmanager.com/gtag/js?id=UA-625523-7"></script>
101
+ <script>
102
+ window.dataLayer = window.dataLayer || [];
103
+ function gtag(){dataLayer.push(arguments);}
104
+ gtag('js', new Date());
105
+
106
+ gtag('config', 'UA-625523-7');
107
+ </script>
108
+ </div>
109
+
110
+ <div id="footer">
111
+ Generated on Thu Dec 12 10:57:46 2019 by
112
+ <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
113
+ 0.9.20 (ruby-2.6.5).
114
+ </div>
115
+
116
+ </div>
117
+ </body>
118
+ </html>
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec :path=>"../"
4
+
5
+ group :development do
6
+ gem "minitest", "5.11.3"
7
+ end
data/init.rb CHANGED
@@ -1,3 +1 @@
1
- # Mocha should no longer be loaded at plugin load time
2
- # You should explicitly load Mocha *after* Test::Unit or MiniTest have been loaded
3
- # e.g. by adding "require 'mocha'" at the bottom of test/test_helper.rb
1
+ warn 'Mocha deprecation warning: The old-style Rails plugin will not be supported in future versions of Mocha.'
@@ -1 +1,9 @@
1
1
  require 'mocha/version'
2
+ require 'mocha/ruby_version'
3
+ require 'mocha/deprecation'
4
+
5
+ if Mocha::PRE_RUBY_V19
6
+ Mocha::Deprecation.warning(
7
+ 'Versions of Ruby earlier than v1.9 will not be supported in future versions of Mocha.'
8
+ )
9
+ end
@@ -1,66 +1,24 @@
1
1
  require 'mocha/ruby_version'
2
- require 'mocha/class_method'
2
+ require 'mocha/stubbed_method'
3
3
 
4
4
  module Mocha
5
- class AnyInstanceMethod < ClassMethod
6
- def mock
7
- stubbee.any_instance.mocha
8
- end
9
-
10
- def reset_mocha
11
- stubbee.any_instance.reset_mocha
12
- end
13
-
14
- def hide_original_method
15
- return unless (@original_visibility = method_visibility(method))
16
- begin
17
- if RUBY_V2_PLUS
18
- @definition_target = PrependedModule.new
19
- stubbee.__send__ :prepend, @definition_target
20
- else
21
- @original_method = stubbee.instance_method(method)
22
- if @original_method && @original_method.owner == stubbee
23
- stubbee.send(:remove_method, method)
24
- end
25
- end
26
- # rubocop:disable Lint/HandleExceptions
27
- rescue NameError
28
- # deal with nasties like ActiveRecord::Associations::AssociationProxy
29
- end
30
- # rubocop:enable Lint/HandleExceptions
31
- end
32
-
33
- def define_new_method
34
- definition_target.class_eval(<<-CODE, __FILE__, __LINE__ + 1)
35
- def #{method}(*args, &block)
36
- self.class.any_instance.mocha.method_missing(:#{method}, *args, &block)
37
- end
38
- CODE
39
- return unless @original_visibility
40
- Module.instance_method(@original_visibility).bind(definition_target).call(method)
41
- end
5
+ class AnyInstanceMethod < StubbedMethod
6
+ private
42
7
 
43
- def remove_new_method
44
- definition_target.send(:remove_method, method)
8
+ def mock_owner
9
+ stubbee.any_instance
45
10
  end
46
11
 
47
- def restore_original_method
48
- return if RUBY_V2_PLUS
49
- return unless @original_method && @original_method.owner == stubbee
50
- stubbee.send(:define_method, method, @original_method)
51
- Module.instance_method(@original_visibility).bind(stubbee).call(method)
12
+ def method_body(method)
13
+ method
52
14
  end
53
15
 
54
- def method_visibility(method)
55
- (stubbee.public_instance_methods(true).include?(method) && :public) ||
56
- (stubbee.protected_instance_methods(true).include?(method) && :protected) ||
57
- (stubbee.private_instance_methods(true).include?(method) && :private)
16
+ def stubbee_method(method_name)
17
+ stubbee.instance_method(method_name)
58
18
  end
59
19
 
60
- private
61
-
62
- def definition_target
63
- @definition_target ||= stubbee
20
+ def original_method_owner
21
+ stubbee
64
22
  end
65
23
  end
66
24
  end
@@ -3,11 +3,33 @@ require 'mocha/hooks'
3
3
  require 'mocha/mockery'
4
4
  require 'mocha/sequence'
5
5
  require 'mocha/object_methods'
6
- require 'mocha/module_methods'
7
6
  require 'mocha/class_methods'
8
7
 
9
8
  module Mocha
10
9
  # Methods added to +Test::Unit::TestCase+, +MiniTest::Unit::TestCase+ or equivalent.
10
+ # The mock creation methods are {#mock}, {#stub} and {#stub_everything}, all of which return a #{Mock}
11
+ # which can be further modified by {Mock#responds_like} and {Mock#responds_like_instance_of} methods,
12
+ # both of which return a {Mock}, too, and can therefore, be chained to the original creation methods.
13
+ #
14
+ # {Mock#responds_like} and {Mock#responds_like_instance_of} force the mock to indicate what it is
15
+ # supposed to be mocking, thus making it a safer verifying mock. They check that the underlying +responder+
16
+ # will actually respond to the methods being stubbed, throwing a +NoMethodError+ upon invocation otherwise.
17
+ #
18
+ # @example Verifying mock using {Mock#responds_like_instance_of}
19
+ # class Sheep
20
+ # def initialize
21
+ # raise "some awkward code we don't want to call"
22
+ # end
23
+ # def chew(grass); end
24
+ # end
25
+ #
26
+ # sheep = mock('sheep').responds_like_instance_of(Sheep)
27
+ # sheep.expects(:chew)
28
+ # sheep.expects(:foo)
29
+ # sheep.respond_to?(:chew) # => true
30
+ # sheep.respond_to?(:foo) # => false
31
+ # sheep.chew
32
+ # sheep.foo # => raises NoMethodError exception
11
33
  module API
12
34
  include ParameterMatchers
13
35
  include Hooks
@@ -15,22 +37,26 @@ module Mocha
15
37
  # @private
16
38
  def self.included(_mod)
17
39
  Object.send(:include, Mocha::ObjectMethods)
18
- Module.send(:include, Mocha::ModuleMethods)
19
40
  Class.send(:include, Mocha::ClassMethods)
20
41
  end
21
42
 
43
+ # @private
44
+ def self.extended(mod)
45
+ included(mod)
46
+ end
47
+
22
48
  # Builds a new mock object
23
49
  #
24
- # @param [String] name identifies mock object in error messages.
25
- # @param [Hash] expected_methods_vs_return_values expected method name symbols as keys and corresponding return values as values - these expectations are setup as if {Mock#expects} were called multiple times.
26
- # @yield optional block to be evaluated in the context of the mock object instance, giving an alternative way to setup stubbed methods.
27
- # @yield note that the block is evaulated by calling Mock#instance_eval and so things like instance variables declared in the test will not be available within the block.
28
- # @yield deprecated: use Object#tap or define stubs/expectations with an explicit receiver instead.
29
50
  # @return [Mock] a new mock object
30
51
  #
31
- # @overload def mock(name, &block)
32
- # @overload def mock(expected_methods_vs_return_values = {}, &block)
33
- # @overload def mock(name, expected_methods_vs_return_values = {}, &block)
52
+ # @overload def mock(name)
53
+ # @param [String, Symbol] name identifies mock object in error messages.
54
+ # @note Prior to v1.10.0 when +name+ was a +Symbol+, this method returned an unnamed +Mock+ that expected the method identified by +name+. This was undocumented behaviour and it will be removed in the future, but for the moment it can be reinstated using {Configuration#reinstate_undocumented_behaviour_from_v1_9=}.
55
+ # @overload def mock(expected_methods_vs_return_values = {})
56
+ # @param [Hash] expected_methods_vs_return_values expected method name symbols as keys and corresponding return values as values - these expectations are setup as if {Mock#expects} were called multiple times.
57
+ # @overload def mock(name, expected_methods_vs_return_values = {})
58
+ # @param [String, Symbol] name identifies mock object in error messages.
59
+ # @param [Hash] expected_methods_vs_return_values expected method name symbols as keys and corresponding return values as values - these expectations are setup as if {Mock#expects} were called multiple times.
34
60
  #
35
61
  # @example Using expected_methods_vs_return_values Hash to setup expectations.
36
62
  # def test_motor_starts_and_stops
@@ -39,36 +65,45 @@ module Mocha
39
65
  # assert motor.stop
40
66
  # # an error will be raised unless both Motor#start and Motor#stop have been called
41
67
  # end
42
- # @example Using the optional block to setup expectations & stubbed methods [deprecated].
43
- # def test_motor_starts_and_stops
44
- # motor = mock('motor') do
45
- # expects(:start).with(100.rpm).returns(true)
46
- # stubs(:stop).returns(true)
47
- # end
48
- # assert motor.start(100.rpm)
49
- # assert motor.stop
50
- # # an error will only be raised if Motor#start(100.rpm) has not been called
51
- # end
52
- def mock(*arguments, &block)
53
- name = arguments.shift if arguments.first.is_a?(String)
68
+ # rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
69
+ def mock(*arguments)
70
+ if Mocha.configuration.reinstate_undocumented_behaviour_from_v1_9?
71
+ if arguments.first.is_a?(Symbol)
72
+ method_name = arguments[0]
73
+ Deprecation.warning(
74
+ "Explicitly include `#{method_name}` in Hash of expected methods vs return values,",
75
+ " e.g. `mock(:#{method_name} => nil)`."
76
+ )
77
+ if arguments[1]
78
+ Deprecation.warning(
79
+ "In this case the 2nd argument for `mock(:##{method_name}, ...)` is ignored,",
80
+ ' but in the future a Hash of expected methods vs return values will be respected.'
81
+ )
82
+ end
83
+ elsif arguments.first.is_a?(String)
84
+ name = arguments.shift
85
+ end
86
+ elsif arguments.first.is_a?(String) || arguments.first.is_a?(Symbol)
87
+ name = arguments.shift
88
+ end
54
89
  expectations = arguments.shift || {}
55
- mock = name ? Mockery.instance.named_mock(name, &block) : Mockery.instance.unnamed_mock(&block)
90
+ mock = name ? Mockery.instance.named_mock(name) : Mockery.instance.unnamed_mock
56
91
  mock.expects(expectations)
57
92
  mock
58
93
  end
94
+ # rubocop:enable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
59
95
 
60
96
  # Builds a new mock object
61
97
  #
62
- # @param [String] name identifies mock object in error messages.
63
- # @param [Hash] stubbed_methods_vs_return_values stubbed method name symbols as keys and corresponding return values as values - these stubbed methods are setup as if {Mock#stubs} were called multiple times.
64
- # @yield optional block to be evaluated in the context of the mock object instance, giving an alternative way to setup stubbed methods.
65
- # @yield note that the block is evaulated by calling Mock#instance_eval and so things like instance variables declared in the test will not be available within the block.
66
- # @yield deprecated: use Object#tap or define stubs/expectations with an explicit receiver instead.
67
98
  # @return [Mock] a new mock object
68
99
  #
69
- # @overload def stub(name, &block)
70
- # @overload def stub(stubbed_methods_vs_return_values = {}, &block)
71
- # @overload def stub(name, stubbed_methods_vs_return_values = {}, &block)
100
+ # @overload def stub(name)
101
+ # @param [String, Symbol] name identifies mock object in error messages.
102
+ # @note Prior to v1.10.0 when +name+ was a +Symbol+, this method returned an unnamed +Mock+ that stubbed the method identified by +name+. This was undocumented behaviour and it will be removed in the future, but for the moment it can be reinstated using {Configuration#reinstate_undocumented_behaviour_from_v1_9=}.
103
+ # @overload def stub(stubbed_methods_vs_return_values = {})
104
+ # @param [Hash] stubbed_methods_vs_return_values stubbed method name symbols as keys and corresponding return values as values - these stubbed methods are setup as if {Mock#stubs} were called multiple times.
105
+ # @overload def stub(name, stubbed_methods_vs_return_values = {})
106
+ # @param [String, Symbol] name identifies mock object in error messages.
72
107
  #
73
108
  # @example Using stubbed_methods_vs_return_values Hash to setup stubbed methods.
74
109
  # def test_motor_starts_and_stops
@@ -77,37 +112,46 @@ module Mocha
77
112
  # assert motor.stop
78
113
  # # an error will not be raised even if either Motor#start or Motor#stop has not been called
79
114
  # end
80
- #
81
- # @example Using the optional block to setup expectations & stubbed methods [deprecated].
82
- # def test_motor_starts_and_stops
83
- # motor = stub('motor') do
84
- # expects(:start).with(100.rpm).returns(true)
85
- # stubs(:stop).returns(true)
86
- # end
87
- # assert motor.start(100.rpm)
88
- # assert motor.stop
89
- # # an error will only be raised if Motor#start(100.rpm) has not been called
90
- # end
91
- def stub(*arguments, &block)
92
- name = arguments.shift if arguments.first.is_a?(String)
115
+ # rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
116
+ def stub(*arguments)
117
+ if Mocha.configuration.reinstate_undocumented_behaviour_from_v1_9?
118
+ if arguments.first.is_a?(Symbol)
119
+ method_name = arguments[0]
120
+ Deprecation.warning(
121
+ "Explicitly include `#{method_name}` in Hash of stubbed methods vs return values,",
122
+ " e.g. `stub(:#{method_name} => nil)`."
123
+ )
124
+ if arguments[1]
125
+ Deprecation.warning(
126
+ "In this case the 2nd argument for `stub(:##{method_name}, ...)` is ignored,",
127
+ ' but in the future a Hash of stubbed methods vs return values will be respected.'
128
+ )
129
+ end
130
+ elsif arguments.first.is_a?(String)
131
+ name = arguments.shift
132
+ end
133
+ elsif arguments.first.is_a?(String) || arguments.first.is_a?(Symbol)
134
+ name = arguments.shift
135
+ end
93
136
  expectations = arguments.shift || {}
94
- stub = name ? Mockery.instance.named_mock(name, &block) : Mockery.instance.unnamed_mock(&block)
137
+ stub = name ? Mockery.instance.named_mock(name) : Mockery.instance.unnamed_mock
95
138
  stub.stubs(expectations)
96
139
  stub
97
140
  end
141
+ # rubocop:enable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
98
142
 
99
143
  # Builds a mock object that accepts calls to any method. By default it will return +nil+ for any method call.
100
144
  #
101
- # @param [String] name identifies mock object in error messages.
102
- # @param [Hash] stubbed_methods_vs_return_values stubbed method name symbols as keys and corresponding return values as values - these stubbed methods are setup as if {Mock#stubs} were called multiple times.
103
- # @yield optional block to be evaluated in the context of the mock object instance, giving an alternative way to setup stubbed methods.
104
- # @yield note that the block is evaulated by calling Mock#instance_eval and so things like instance variables declared in the test will not be available within the block.
105
- # @yield deprecated: use Object#tap or define stubs/expectations with an explicit receiver instead.
106
145
  # @return [Mock] a new mock object
107
146
  #
108
- # @overload def stub_everything(name, &block)
109
- # @overload def stub_everything(stubbed_methods_vs_return_values = {}, &block)
110
- # @overload def stub_everything(name, stubbed_methods_vs_return_values = {}, &block)
147
+ # @overload def stub_everything(name)
148
+ # @param [String, Symbol] name identifies mock object in error messages.
149
+ # @note Prior to v1.10.0 when +name+ was a +Symbol+, this method returned an unnamed +Mock+ that stubbed the method identified by +name+. This was undocumented behaviour and it will be removed in the future, but for the moment it can be reinstated using {Configuration#reinstate_undocumented_behaviour_from_v1_9=}.
150
+ # @overload def stub_everything(stubbed_methods_vs_return_values = {})
151
+ # @param [Hash] stubbed_methods_vs_return_values stubbed method name symbols as keys and corresponding return values as values - these stubbed methods are setup as if {Mock#stubs} were called multiple times.
152
+ # @overload def stub_everything(name, stubbed_methods_vs_return_values = {})
153
+ # @param [String, Symbol] name identifies mock object in error messages.
154
+ # @param [Hash] stubbed_methods_vs_return_values stubbed method name symbols as keys and corresponding return values as values - these stubbed methods are setup as if {Mock#stubs} were called multiple times.
111
155
  #
112
156
  # @example Ignore invocations of irrelevant methods.
113
157
  # def test_motor_stops
@@ -116,14 +160,34 @@ module Mocha
116
160
  # assert_nil motor.irrelevant_method_2 # => no error raised
117
161
  # assert motor.stop
118
162
  # end
119
- def stub_everything(*arguments, &block)
120
- name = arguments.shift if arguments.first.is_a?(String)
163
+ # rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
164
+ def stub_everything(*arguments)
165
+ if Mocha.configuration.reinstate_undocumented_behaviour_from_v1_9?
166
+ if arguments.first.is_a?(Symbol)
167
+ method_name = arguments[0]
168
+ Deprecation.warning(
169
+ "Explicitly include `#{method_name}` in Hash of stubbed methods vs return values,",
170
+ " e.g. `stub_everything(:#{method_name} => nil)`."
171
+ )
172
+ if arguments[1]
173
+ Deprecation.warning(
174
+ "In this case the 2nd argument for `stub_everything(:##{method_name}, ...)` is ignored,",
175
+ ' but in the future a Hash of stubbed methods vs return values will be respected.'
176
+ )
177
+ end
178
+ elsif arguments.first.is_a?(String)
179
+ name = arguments.shift
180
+ end
181
+ elsif arguments.first.is_a?(String) || arguments.first.is_a?(Symbol)
182
+ name = arguments.shift
183
+ end
121
184
  expectations = arguments.shift || {}
122
- stub = name ? Mockery.instance.named_mock(name, &block) : Mockery.instance.unnamed_mock(&block)
185
+ stub = name ? Mockery.instance.named_mock(name) : Mockery.instance.unnamed_mock
123
186
  stub.stub_everything
124
187
  stub.stubs(expectations)
125
188
  stub
126
189
  end
190
+ # rubocop:enable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
127
191
 
128
192
  # Builds a new sequence which can be used to constrain the order in which expectations can occur.
129
193
  #