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
@@ -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
  #
@@ -0,0 +1,31 @@
1
+ module Mocha
2
+ module BlockMatchers
3
+ class OptionalBlock
4
+ def match?(_actual_block)
5
+ true
6
+ end
7
+
8
+ def mocha_inspect; end
9
+ end
10
+
11
+ class BlockGiven
12
+ def match?(actual_block)
13
+ !actual_block.nil?
14
+ end
15
+
16
+ def mocha_inspect
17
+ 'with block given'
18
+ end
19
+ end
20
+
21
+ class NoBlockGiven
22
+ def match?(actual_block)
23
+ actual_block.nil?
24
+ end
25
+
26
+ def mocha_inspect
27
+ 'with no block given'
28
+ end
29
+ end
30
+ end
31
+ end
@@ -26,37 +26,43 @@ module Mocha
26
26
  def initialize(required, maximum)
27
27
  @required = required
28
28
  @maximum = maximum
29
+ @invocations = []
29
30
  end
30
31
 
31
- def invocations_allowed?(invocation_count)
32
- invocation_count < maximum
32
+ def <<(invocation)
33
+ @invocations << invocation
33
34
  end
34
35
 
35
- def satisfied?(invocations_so_far)
36
- invocations_so_far >= required
36
+ def invocations_allowed?
37
+ @invocations.size < maximum
38
+ end
39
+
40
+ def satisfied?
41
+ @invocations.size >= required
37
42
  end
38
43
 
39
44
  def needs_verifying?
40
45
  !allowed_any_number_of_times?
41
46
  end
42
47
 
43
- def verified?(invocation_count)
44
- (invocation_count >= required) && (invocation_count <= maximum)
48
+ def verified?
49
+ (@invocations.size >= required) && (@invocations.size <= maximum)
45
50
  end
46
51
 
47
52
  def allowed_any_number_of_times?
48
53
  required.zero? && infinite?(maximum)
49
54
  end
50
55
 
51
- def used?(invocation_count)
52
- (invocation_count > 0) || maximum.zero?
56
+ def used?
57
+ @invocations.any? || maximum.zero?
53
58
  end
54
59
 
55
- def mocha_inspect
60
+ # rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
61
+ def anticipated_times
56
62
  if allowed_any_number_of_times?
57
63
  'allowed any number of times'
58
64
  elsif required.zero? && maximum.zero?
59
- 'expected never'
65
+ "expected #{times(maximum)}"
60
66
  elsif required == maximum
61
67
  "expected exactly #{times(required)}"
62
68
  elsif infinite?(maximum)
@@ -67,6 +73,15 @@ module Mocha
67
73
  "expected between #{required} and #{times(maximum)}"
68
74
  end
69
75
  end
76
+ # rubocop:enable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
77
+
78
+ def invoked_times
79
+ "invoked #{times(@invocations.size)}"
80
+ end
81
+
82
+ def actual_invocations
83
+ @invocations.map(&:full_description).join
84
+ end
70
85
 
71
86
  protected
72
87
 
@@ -74,7 +89,7 @@ module Mocha
74
89
 
75
90
  def times(number)
76
91
  case number
77
- when 0 then 'no times'
92
+ when 0 then 'never'
78
93
  when 1 then 'once'
79
94
  when 2 then 'twice'
80
95
  else "#{number} times"
@@ -1,15 +1,9 @@
1
1
  require 'mocha/mockery'
2
- require 'mocha/class_method'
3
2
  require 'mocha/any_instance_method'
4
3
 
5
4
  module Mocha
6
5
  # Methods added to all classes to allow mocking and stubbing on real (i.e. non-mock) objects.
7
6
  module ClassMethods
8
- # @private
9
- def stubba_method
10
- Mocha::ClassMethod
11
- end
12
-
13
7
  # @private
14
8
  class AnyInstance
15
9
  def initialize(klass)
@@ -28,17 +22,15 @@ module Mocha
28
22
  Mocha::AnyInstanceMethod
29
23
  end
30
24
 
31
- attr_reader :stubba_object
25
+ def stubba_class
26
+ @stubba_object
27
+ end
32
28
 
33
- def method_exists?(method, include_public_methods = true)
34
- if include_public_methods
35
- return true if @stubba_object.public_instance_methods(true).include?(method)
36
- return true if @stubba_object.allocate.respond_to?(method.to_sym)
37
- end
38
- return true if @stubba_object.protected_instance_methods(true).include?(method)
39
- return true if @stubba_object.private_instance_methods(true).include?(method)
40
- false
29
+ def respond_to?(method)
30
+ @stubba_object.allocate.respond_to?(method.to_sym)
41
31
  end
32
+
33
+ attr_reader :stubba_object
42
34
  end
43
35
 
44
36
  # @return [Mock] a mock object which will detect calls to any instance of this class.
@@ -56,5 +48,15 @@ module Mocha
56
48
  end
57
49
  @any_instance ||= AnyInstance.new(self)
58
50
  end
51
+
52
+ # @private
53
+ # rubocop:disable Metrics/CyclomaticComplexity
54
+ def __method_visibility__(method, include_public_methods = true)
55
+ (include_public_methods && public_method_defined?(method) && :public) ||
56
+ (protected_method_defined?(method) && :protected) ||
57
+ (private_method_defined?(method) && :private)
58
+ end
59
+ # rubocop:enable Metrics/CyclomaticComplexity
60
+ alias_method :__method_exists__?, :__method_visibility__
59
61
  end
60
62
  end
@@ -1,117 +1,379 @@
1
1
  module Mocha
2
- # This class allows you to determine what should happen under certain circumstances. In each scenario, Mocha can be configured to {.allow do nothing}, {.warn_when display a warning message}, or {.prevent raise an exception}. The relevant scenario is identified using one of the following symbols:
2
+ # Allows setting of configuration options. See {Configuration} for the available options.
3
3
  #
4
- # * +:stubbing_method_unnecessarily+ This is useful for identifying unused stubs. Unused stubs are often accidentally introduced when code is {http://martinfowler.com/bliki/DefinitionOfRefactoring.html refactored}. Allowed by default.
5
- # * +:stubbing_non_existent_method+ - This is useful if you want to ensure that methods you're mocking really exist. A common criticism of unit tests with mock objects is that such a test may (incorrectly) pass when an equivalent non-mocking test would (correctly) fail. While you should always have some integration tests, particularly for critical business functionality, this Mocha configuration setting should catch scenarios when mocked methods and real methods have become misaligned. Allowed by default.
6
- # * +:stubbing_non_public_method+ - Many people think that it's good practice only to mock public methods. This is one way to prevent your tests being too tightly coupled to the internal implementation of a class. Such tests tend to be very brittle and not much use when refactoring. Allowed by default.
7
- # * +:stubbing_method_on_non_mock_object+ - If you like the idea of {http://www.jmock.org/oopsla2004.pdf mocking roles not objects} and {http://www.mockobjects.com/2007/04/test-smell-mocking-concrete-classes.html you don't like stubbing concrete classes}, this is the setting for you. However, while this restriction makes a lot of sense in Java with its {http://java.sun.com/docs/books/tutorial/java/concepts/interface.html explicit interfaces}, it may be moot in Ruby where roles are probably best represented as Modules. Allowed by default.
8
- # * +:stubbing_method_on_nil+ - This is usually done accidentally, but there might be rare cases where it is intended. Prevented by default.
4
+ # Typically the configuration is set globally in a +test_helper.rb+ or +spec_helper.rb+ file.
9
5
  #
10
- # @example Preventing unnecessary stubbing of a method
11
- # Mocha::Configuration.prevent(:stubbing_method_unnecessarily)
6
+ # @see Configuration
12
7
  #
13
- # example = mock('example')
14
- # example.stubs(:unused_stub)
15
- # # => Mocha::StubbingError: stubbing method unnecessarily:
16
- # # => #<Mock:example>.unused_stub(any_parameters)
8
+ # @yieldparam configuration [Configuration] the configuration for modification
17
9
  #
18
- # @example Preventing stubbing of a method on a non-mock object
19
- # Mocha::Configuration.prevent(:stubbing_method_on_non_mock_object)
20
- #
21
- # class Example
22
- # def example_method; end
23
- # end
24
- #
25
- # example = Example.new
26
- # example.stubs(:example_method)
27
- # # => Mocha::StubbingError: stubbing method on non-mock object:
28
- # # => #<Example:0x593620>.example_method
29
- #
30
- # @example Preventing stubbing of a non-existent method
31
- #
32
- # Mocha::Configuration.prevent(:stubbing_non_existent_method)
33
- #
34
- # class Example
35
- # end
36
- #
37
- # example = Example.new
38
- # example.stubs(:method_that_doesnt_exist)
39
- # # => Mocha::StubbingError: stubbing non-existent method:
40
- # # => #<Example:0x593760>.method_that_doesnt_exist
41
- #
42
- # @example Preventing stubbing of a non-public method
43
- # Mocha::Configuration.prevent(:stubbing_non_public_method)
44
- #
45
- # class Example
46
- # def internal_method; end
47
- # private :internal_method
10
+ # @example Setting multiple configuration options
11
+ # Mocha.configure do |c|
12
+ # c.stubbing_method_unnecessarily = :prevent
13
+ # c.stubbing_method_on_non_mock_object = :warn
14
+ # c.stubbing_method_on_nil = :allow
48
15
  # end
49
16
  #
50
- # example = Example.new
51
- # example.stubs(:internal_method)
52
- # # => Mocha::StubbingError: stubbing non-public method:
53
- # # => #<Example:0x593530>.internal_method
54
- #
55
- # Typically the configuration would be set globally in a +test_helper.rb+ or +spec_helper.rb+ file. However, it can also be temporarily overridden locally using the block syntax of the relevant method. In the latter case, the original configuration settings are restored when the block is exited.
17
+ def self.configure
18
+ yield configuration
19
+ end
20
+
21
+ # @private
22
+ def self.configuration
23
+ Configuration.configuration
24
+ end
25
+
26
+ # This class provides a number of ways to configure the library.
56
27
  #
57
- # @example Temporarily allowing stubbing of a non-existent method
58
- # Mocha::Configuration.prevent(:stubbing_non_public_method)
28
+ # Typically the configuration is set globally in a +test_helper.rb+ or +spec_helper.rb+ file.
59
29
  #
60
- # class Example
30
+ # @example Setting multiple configuration options
31
+ # Mocha.configure do |c|
32
+ # c.stubbing_method_unnecessarily = :prevent
33
+ # c.stubbing_method_on_non_mock_object = :warn
34
+ # c.stubbing_method_on_nil = :allow
61
35
  # end
62
36
  #
63
- # Mocha::Configuration.allow(:stubbing_non_existent_method) do
64
- # example = Example.new
65
- # example.stubs(:method_that_doesnt_exist)
66
- # # => no exception raised
67
- # end
68
37
  class Configuration
38
+ # @private
69
39
  DEFAULTS = {
70
40
  :stubbing_method_unnecessarily => :allow,
71
41
  :stubbing_method_on_non_mock_object => :allow,
72
42
  :stubbing_non_existent_method => :allow,
73
43
  :stubbing_non_public_method => :allow,
74
- :stubbing_method_on_nil => :prevent
44
+ :stubbing_method_on_nil => :prevent,
45
+ :display_matching_invocations_on_failure => false,
46
+ :reinstate_undocumented_behaviour_from_v1_9 => false
75
47
  }.freeze
76
48
 
49
+ attr_reader :options
50
+ protected :options
51
+
52
+ # @private
53
+ def initialize(options = {})
54
+ @options = DEFAULTS.merge(options)
55
+ end
56
+
57
+ # @private
58
+ def initialize_copy(other)
59
+ @options = other.options.dup
60
+ end
61
+
62
+ # @private
63
+ def merge(other)
64
+ self.class.new(@options.merge(other.options))
65
+ end
66
+
67
+ # Configure whether stubbing methods unnecessarily is allowed.
68
+ #
69
+ # This is useful for identifying unused stubs. Unused stubs are often accidentally introduced when code is {http://martinfowler.com/bliki/DefinitionOfRefactoring.html refactored}.
70
+ #
71
+ # When +value+ is +:allow+, do nothing. This is the default.
72
+ # When +value+ is +:warn+, display a warning.
73
+ # When +value+ is +:prevent+, raise a {StubbingError}.
74
+ #
75
+ # @param [Symbol] value one of +:allow+, +:warn+, +:prevent+.
76
+ #
77
+ # @example Preventing unnecessary stubbing of a method
78
+ # Mocha.configure do |c|
79
+ # c.stubbing_method_unnecessarily = :prevent
80
+ # end
81
+ #
82
+ # example = mock('example')
83
+ # example.stubs(:unused_stub)
84
+ # # => Mocha::StubbingError: stubbing method unnecessarily:
85
+ # # => #<Mock:example>.unused_stub(any_parameters)
86
+ #
87
+ def stubbing_method_unnecessarily=(value)
88
+ @options[:stubbing_method_unnecessarily] = value
89
+ end
90
+
91
+ # @private
92
+ def stubbing_method_unnecessarily
93
+ @options[:stubbing_method_unnecessarily]
94
+ end
95
+
96
+ # Configure whether stubbing methods on non-mock objects is allowed.
97
+ #
98
+ # If you like the idea of {http://www.jmock.org/oopsla2004.pdf mocking roles not objects} and {http://www.mockobjects.com/2007/04/test-smell-mocking-concrete-classes.html you don't like stubbing concrete classes}, this is the setting for you. However, while this restriction makes a lot of sense in Java with its {http://java.sun.com/docs/books/tutorial/java/concepts/interface.html explicit interfaces}, it may be moot in Ruby where roles are probably best represented as Modules.
99
+ #
100
+ # When +value+ is +:allow+, do nothing. This is the default.
101
+ # When +value+ is +:warn+, display a warning.
102
+ # When +value+ is +:prevent+, raise a {StubbingError}.
103
+ #
104
+ # @param [Symbol] value one of +:allow+, +:warn+, +:prevent+.
105
+ #
106
+ # @example Preventing stubbing of a method on a non-mock object
107
+ # Mocha.configure do |c|
108
+ # c.stubbing_method_on_non_mock_object = :prevent
109
+ # end
110
+ #
111
+ # class Example
112
+ # def example_method; end
113
+ # end
114
+ #
115
+ # example = Example.new
116
+ # example.stubs(:example_method)
117
+ # # => Mocha::StubbingError: stubbing method on non-mock object:
118
+ # # => #<Example:0x593620>.example_method
119
+ #
120
+ def stubbing_method_on_non_mock_object=(value)
121
+ @options[:stubbing_method_on_non_mock_object] = value
122
+ end
123
+
124
+ # @private
125
+ def stubbing_method_on_non_mock_object
126
+ @options[:stubbing_method_on_non_mock_object]
127
+ end
128
+
129
+ # Configure whether stubbing of non-existent methods is allowed.
130
+ #
131
+ # This is useful if you want to ensure that methods you're mocking really exist. A common criticism of unit tests with mock objects is that such a test may (incorrectly) pass when an equivalent non-mocking test would (correctly) fail. While you should always have some integration tests, particularly for critical business functionality, this Mocha configuration setting should catch scenarios when mocked methods and real methods have become misaligned.
132
+ #
133
+ # When +value+ is +:allow+, do nothing. This is the default.
134
+ # When +value+ is +:warn+, display a warning.
135
+ # When +value+ is +:prevent+, raise a {StubbingError}.
136
+ #
137
+ # @param [Symbol] value one of +:allow+, +:warn+, +:prevent+.
138
+ #
139
+ # @example Preventing stubbing of a non-existent method
140
+ #
141
+ # Mocha.configure do |c|
142
+ # c.stubbing_non_existent_method = :prevent
143
+ # end
144
+ #
145
+ # class Example
146
+ # end
147
+ #
148
+ # example = Example.new
149
+ # example.stubs(:method_that_doesnt_exist)
150
+ # # => Mocha::StubbingError: stubbing non-existent method:
151
+ # # => #<Example:0x593760>.method_that_doesnt_exist
152
+ #
153
+ def stubbing_non_existent_method=(value)
154
+ @options[:stubbing_non_existent_method] = value
155
+ end
156
+
157
+ # @private
158
+ def stubbing_non_existent_method
159
+ @options[:stubbing_non_existent_method]
160
+ end
161
+
162
+ # Configure whether stubbing of non-public methods is allowed.
163
+ #
164
+ # Many people think that it's good practice only to mock public methods. This is one way to prevent your tests being too tightly coupled to the internal implementation of a class. Such tests tend to be very brittle and not much use when refactoring.
165
+ #
166
+ # When +value+ is +:allow+, do nothing. This is the default.
167
+ # When +value+ is +:warn+, display a warning.
168
+ # When +value+ is +:prevent+, raise a {StubbingError}.
169
+ #
170
+ # @param [Symbol] value one of +:allow+, +:warn+, +:prevent+.
171
+ #
172
+ # @example Preventing stubbing of a non-public method
173
+ # Mocha.configure do |c|
174
+ # c.stubbing_non_public_method = :prevent
175
+ # end
176
+ #
177
+ # class Example
178
+ # def internal_method; end
179
+ # private :internal_method
180
+ # end
181
+ #
182
+ # example = Example.new
183
+ # example.stubs(:internal_method)
184
+ # # => Mocha::StubbingError: stubbing non-public method:
185
+ # # => #<Example:0x593530>.internal_method
186
+ #
187
+ def stubbing_non_public_method=(value)
188
+ @options[:stubbing_non_public_method] = value
189
+ end
190
+
191
+ # @private
192
+ def stubbing_non_public_method
193
+ @options[:stubbing_non_public_method]
194
+ end
195
+
196
+ # Configure whether stubbing methods on the +nil+ object is allowed.
197
+ #
198
+ # This is usually done accidentally, but there might be rare cases where it is intended.
199
+ #
200
+ # This option only works for Ruby < v2.2.0. In later versions of Ruby +nil+ is frozen and so a {StubbingError} will be raised if you attempt to stub a method on +nil+.
201
+ #
202
+ # When +value+ is +:allow+, do nothing.
203
+ # When +value+ is +:warn+, display a warning.
204
+ # When +value+ is +:prevent+, raise a {StubbingError}. This is the default.
205
+ #
206
+ # @param [Symbol] value one of +:allow+, +:warn+, +:prevent+.
207
+ #
208
+ def stubbing_method_on_nil=(value)
209
+ @options[:stubbing_method_on_nil] = value
210
+ end
211
+
212
+ # @private
213
+ def stubbing_method_on_nil
214
+ @options[:stubbing_method_on_nil]
215
+ end
216
+
217
+ # Display matching invocations alongside expectations on Mocha-related test failure.
218
+ #
219
+ # @param [Boolean] value +true+ to enable display of matching invocations; disabled by default.
220
+ #
221
+ # @example Enable display of matching invocations
222
+ # Mocha.configure do |c|
223
+ # c.display_matching_invocations_on_failure = true
224
+ # end
225
+ #
226
+ # foo = mock('foo')
227
+ # foo.expects(:bar)
228
+ # foo.stubs(:baz).returns('baz').raises(RuntimeError).throws(:tag, 'value')
229
+ #
230
+ # foo.baz(1, 2)
231
+ # assert_raises(RuntimeError) { foo.baz(3, 4) }
232
+ # assert_throws(:tag) { foo.baz(5, 6) }
233
+ #
234
+ # not all expectations were satisfied
235
+ # unsatisfied expectations:
236
+ # - expected exactly once, invoked never: #<Mock:foo>.bar
237
+ # satisfied expectations:
238
+ # - allowed any number of times, invoked 3 times: #<Mock:foo>.baz(any_parameters)
239
+ # - #<Mock:foo>.baz(1, 2) # => "baz"
240
+ # - #<Mock:foo>.baz(3, 4) # => raised RuntimeError
241
+ # - #<Mock:foo>.baz(5, 6) # => threw (:tag, "value")
242
+ def display_matching_invocations_on_failure=(value)
243
+ @options[:display_matching_invocations_on_failure] = value
244
+ end
245
+
246
+ # @private
247
+ def display_matching_invocations_on_failure?
248
+ @options[:display_matching_invocations_on_failure]
249
+ end
250
+
251
+ # Reinstate undocumented behaviour from v1.9
252
+ #
253
+ # Previously when {API#mock}, {API#stub}, or {API#stub_everything} were called with the first argument being a symbol, they built an *unnamed* mock object *and* expected or stubbed the method identified by the symbol argument; subsequent arguments were ignored.
254
+ # Now these methods build a *named* mock with the name specified by the symbol argument; *no* methods are expected or stubbed and subsequent arguments *are* taken into account.
255
+ #
256
+ # Previously if {Expectation#yields} or {Expectation#multiple_yields} was called on an expectation, but no block was given when the method was invoked, the instruction to yield was ignored.
257
+ # Now a +LocalJumpError+ is raised.
258
+ #
259
+ # Enabling this configuration option reinstates the previous behaviour, but displays a deprecation warning.
260
+ #
261
+ # @param [Boolean] value +true+ to reinstate undocumented behaviour; disabled by default.
262
+ #
263
+ # @example Reinstate undocumented behaviour for {API#mock}
264
+ # Mocha.configure do |c|
265
+ # c.reinstate_undocumented_behaviour_from_v1_9 = true
266
+ # end
267
+ #
268
+ # foo = mock(:bar)
269
+ # foo.inspect # => #<Mock>
270
+ #
271
+ # not all expectations were satisfied
272
+ # unsatisfied expectations:
273
+ # - expected exactly once, invoked never: #<Mock>.foo
274
+ #
275
+ # @example Reinstate undocumented behaviour for {API#stub}
276
+ # Mocha.configure do |c|
277
+ # c.reinstate_undocumented_behaviour_from_v1_9 = true
278
+ # end
279
+ #
280
+ # foo = stub(:bar)
281
+ # foo.inspect # => #<Mock>
282
+ # foo.bar # => nil
283
+ #
284
+ # @example Reinstate undocumented behaviour for {Expectation#yields}
285
+ # foo = mock('foo')
286
+ # foo.stubs(:my_method).yields(1, 2)
287
+ # foo.my_method # => raises LocalJumpError when no block is supplied
288
+ #
289
+ # Mocha.configure do |c|
290
+ # c.reinstate_undocumented_behaviour_from_v1_9 = true
291
+ # end
292
+ #
293
+ # foo = mock('foo')
294
+ # foo.stubs(:my_method).yields(1, 2)
295
+ # foo.my_method # => does *not* raise LocalJumpError when no block is supplied
296
+ #
297
+ def reinstate_undocumented_behaviour_from_v1_9=(value)
298
+ @options[:reinstate_undocumented_behaviour_from_v1_9] = value
299
+ end
300
+
301
+ # @private
302
+ def reinstate_undocumented_behaviour_from_v1_9?
303
+ @options[:reinstate_undocumented_behaviour_from_v1_9]
304
+ end
305
+
77
306
  class << self
78
307
  # Allow the specified +action+.
79
308
  #
80
309
  # @param [Symbol] action one of +:stubbing_method_unnecessarily+, +:stubbing_method_on_non_mock_object+, +:stubbing_non_existent_method+, +:stubbing_non_public_method+, +:stubbing_method_on_nil+.
81
310
  # @yield optional block during which the configuration change will be changed before being returned to its original value at the end of the block.
311
+ # @deprecated If a block is supplied, call {.override} with a +Hash+ containing an entry with the +action+ as the key and +:allow+ as the value. If no block is supplied, call the appropriate +action+ writer method with +value+ set to +:allow+ via {Mocha.configure}. The writer method will be the one of the following corresponding to the +action+:
312
+ # * {#stubbing_method_unnecessarily=}
313
+ # * {#stubbing_method_on_non_mock_object=}
314
+ # * {#stubbing_non_existent_method=}
315
+ # * {#stubbing_non_public_method=}
316
+ # * {#stubbing_method_on_nil=}
82
317
  def allow(action, &block)
318
+ if block_given?
319
+ Deprecation.warning("Use Mocha::Configuration.override(#{action}: :allow) with the same block")
320
+ else
321
+ Deprecation.warning("Use Mocha.configure { |c| c.#{action} = :allow }")
322
+ end
83
323
  change_config action, :allow, &block
84
324
  end
85
325
 
86
326
  # @private
87
327
  def allow?(action)
88
- configuration[action] == :allow
328
+ configuration.allow?(action)
89
329
  end
90
330
 
91
331
  # Warn if the specified +action+ is attempted.
92
332
  #
93
333
  # @param [Symbol] action one of +:stubbing_method_unnecessarily+, +:stubbing_method_on_non_mock_object+, +:stubbing_non_existent_method+, +:stubbing_non_public_method+, +:stubbing_method_on_nil+.
94
334
  # @yield optional block during which the configuration change will be changed before being returned to its original value at the end of the block.
335
+ # @deprecated If a block is supplied, call {.override} with a +Hash+ containing an entry with the +action+ as the key and +:warn+ as the value. If no block is supplied, call the appropriate +action+ writer method with +value+ set to +:warn+ via {Mocha.configure}. The writer method will be the one of the following corresponding to the +action+:
336
+ # * {#stubbing_method_unnecessarily=}
337
+ # * {#stubbing_method_on_non_mock_object=}
338
+ # * {#stubbing_non_existent_method=}
339
+ # * {#stubbing_non_public_method=}
340
+ # * {#stubbing_method_on_nil=}
95
341
  def warn_when(action, &block)
342
+ if block_given?
343
+ Deprecation.warning("Use Mocha::Configuration.override(#{action}: :warn) with the same block")
344
+ else
345
+ Deprecation.warning("Use Mocha.configure { |c| c.#{action} = :warn }")
346
+ end
96
347
  change_config action, :warn, &block
97
348
  end
98
349
 
99
350
  # @private
100
351
  def warn_when?(action)
101
- configuration[action] == :warn
352
+ configuration.warn_when?(action)
102
353
  end
103
354
 
104
- # Raise a {StubbingError} if if the specified +action+ is attempted.
355
+ # Raise a {StubbingError} if the specified +action+ is attempted.
105
356
  #
106
357
  # @param [Symbol] action one of +:stubbing_method_unnecessarily+, +:stubbing_method_on_non_mock_object+, +:stubbing_non_existent_method+, +:stubbing_non_public_method+, +:stubbing_method_on_nil+.
107
358
  # @yield optional block during which the configuration change will be changed before being returned to its original value at the end of the block.
359
+ # @deprecated If a block is supplied, call {.override} with a +Hash+ containing an entry with the +action+ as the key and +:prevent+ as the value. If no block is supplied, call the appropriate +action+ writer method with +value+ set to +:prevent+ via {Mocha.configure}. The writer method will be the one of the following corresponding to the +action+:
360
+ # * {#stubbing_method_unnecessarily=}
361
+ # * {#stubbing_method_on_non_mock_object=}
362
+ # * {#stubbing_non_existent_method=}
363
+ # * {#stubbing_non_public_method=}
364
+ # * {#stubbing_method_on_nil=}
108
365
  def prevent(action, &block)
366
+ if block_given?
367
+ Deprecation.warning("Use Mocha::Configuration.override(#{action}: :prevent) with the same block")
368
+ else
369
+ Deprecation.warning("Use Mocha.configure { |c| c.#{action} = :prevent }")
370
+ end
109
371
  change_config action, :prevent, &block
110
372
  end
111
373
 
112
374
  # @private
113
375
  def prevent?(action)
114
- configuration[action] == :prevent
376
+ configuration.prevent?(action)
115
377
  end
116
378
 
117
379
  # @private
@@ -119,29 +381,51 @@ module Mocha
119
381
  @configuration = nil
120
382
  end
121
383
 
122
- private
384
+ # Temporarily modify {Configuration} options.
385
+ #
386
+ # The supplied +temporary_options+ will override the current configuration for the duration of the supplied block.
387
+ # The configuration will be returned to its original state when the block returns.
388
+ #
389
+ # @param [Hash] temporary_options the configuration options to apply for the duration of the block.
390
+ # @yield block during which the configuration change will be in force.
391
+ #
392
+ # @example Temporarily allow stubbing of +nil+
393
+ # Mocha::Configuration.override(stubbing_method_on_nil: :allow) do
394
+ # nil.stubs(:foo)
395
+ # end
396
+ def override(temporary_options)
397
+ original_configuration = configuration
398
+ @configuration = configuration.merge(new(temporary_options))
399
+ yield
400
+ ensure
401
+ @configuration = original_configuration
402
+ end
123
403
 
124
404
  # @private
125
405
  def configuration
126
- @configuration ||= DEFAULTS.dup
406
+ @configuration ||= new
127
407
  end
128
408
 
409
+ private
410
+
129
411
  # @private
130
412
  def change_config(action, new_value, &block)
131
413
  if block_given?
132
414
  temporarily_change_config action, new_value, &block
133
415
  else
134
- configuration[action] = new_value
416
+ configuration.send("#{action}=".to_sym, new_value)
135
417
  end
136
418
  end
137
419
 
138
420
  # @private
139
421
  def temporarily_change_config(action, new_value)
140
- original_value = configuration[action]
141
- configuration[action] = new_value
422
+ original_configuration = configuration
423
+ new_configuration = configuration.dup
424
+ new_configuration.send("#{action}=".to_sym, new_value)
425
+ @configuration = new_configuration
142
426
  yield
143
427
  ensure
144
- configuration[action] = original_value
428
+ @configuration = original_configuration
145
429
  end
146
430
  end
147
431
  end