mocha 0.10.5 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (229) hide show
  1. data/COPYING.rdoc +1 -1
  2. data/README.rdoc +6 -4
  3. data/RELEASE.rdoc +12 -0
  4. data/Rakefile +29 -60
  5. data/doc/Mocha.html +112 -0
  6. data/doc/Mocha/API.html +898 -0
  7. data/doc/Mocha/ClassMethods.html +246 -0
  8. data/doc/Mocha/Configuration.html +471 -0
  9. data/doc/Mocha/Expectation.html +2570 -0
  10. data/doc/Mocha/Mock.html +830 -0
  11. data/doc/Mocha/ObjectMethods.html +668 -0
  12. data/doc/Mocha/ParameterMatchers.html +2715 -0
  13. data/doc/Mocha/ParameterMatchers/AllOf.html +137 -0
  14. data/doc/Mocha/ParameterMatchers/AnyOf.html +137 -0
  15. data/doc/Mocha/ParameterMatchers/AnyParameters.html +136 -0
  16. data/doc/Mocha/ParameterMatchers/Anything.html +136 -0
  17. data/doc/Mocha/ParameterMatchers/Base.html +419 -0
  18. data/doc/Mocha/ParameterMatchers/Equals.html +137 -0
  19. data/doc/Mocha/ParameterMatchers/HasEntries.html +137 -0
  20. data/doc/Mocha/ParameterMatchers/HasEntry.html +137 -0
  21. data/doc/Mocha/ParameterMatchers/HasKey.html +137 -0
  22. data/doc/Mocha/ParameterMatchers/HasValue.html +137 -0
  23. data/doc/Mocha/ParameterMatchers/Includes.html +137 -0
  24. data/doc/Mocha/ParameterMatchers/InstanceOf.html +137 -0
  25. data/doc/Mocha/ParameterMatchers/IsA.html +136 -0
  26. data/doc/Mocha/ParameterMatchers/KindOf.html +137 -0
  27. data/doc/Mocha/ParameterMatchers/Not.html +137 -0
  28. data/doc/Mocha/ParameterMatchers/Optionally.html +136 -0
  29. data/doc/Mocha/ParameterMatchers/QueryStringMatches.html +136 -0
  30. data/doc/Mocha/ParameterMatchers/RegexpMatches.html +137 -0
  31. data/doc/Mocha/ParameterMatchers/RespondsWith.html +137 -0
  32. data/doc/Mocha/ParameterMatchers/YamlEquivalent.html +137 -0
  33. data/doc/Mocha/Sequence.html +133 -0
  34. data/doc/Mocha/StateMachine.html +510 -0
  35. data/doc/Mocha/StateMachine/State.html +125 -0
  36. data/doc/Mocha/StateMachine/StatePredicate.html +125 -0
  37. data/doc/Mocha/StubbingError.html +134 -0
  38. data/doc/Mocha/UnexpectedInvocation.html +124 -0
  39. data/doc/_index.html +481 -0
  40. data/doc/class_list.html +47 -0
  41. data/doc/css/common.css +1 -0
  42. data/doc/css/full_list.css +55 -0
  43. data/doc/css/style.css +322 -0
  44. data/doc/file.COPYING.html +72 -0
  45. data/doc/file.MIT-LICENSE.html +86 -0
  46. data/doc/file.README.html +153 -0
  47. data/doc/file.RELEASE.html +933 -0
  48. data/doc/file.misc.html +108 -0
  49. data/doc/file.mocha.html +90 -0
  50. data/doc/file.stubba.html +129 -0
  51. data/doc/file_list.html +67 -0
  52. data/doc/frames.html +13 -0
  53. data/doc/index.html +153 -0
  54. data/doc/js/app.js +205 -0
  55. data/doc/js/full_list.js +173 -0
  56. data/doc/js/jquery.js +16 -0
  57. data/doc/method_list.html +510 -0
  58. data/doc/top-level-namespace.html +105 -0
  59. data/lib/mocha/any_instance_method.rb +16 -11
  60. data/lib/mocha/api.rb +120 -106
  61. data/lib/mocha/argument_iterator.rb +6 -6
  62. data/lib/mocha/backtrace_filter.rb +1 -1
  63. data/lib/mocha/cardinality.rb +21 -21
  64. data/lib/mocha/central.rb +8 -8
  65. data/lib/mocha/change_state_side_effect.rb +5 -5
  66. data/lib/mocha/class_method.rb +14 -19
  67. data/lib/mocha/configuration.rb +47 -37
  68. data/lib/mocha/deprecation.rb +8 -8
  69. data/lib/mocha/exception_raiser.rb +7 -7
  70. data/lib/mocha/expectation.rb +173 -77
  71. data/lib/mocha/expectation_error.rb +2 -2
  72. data/lib/mocha/expectation_list.rb +11 -11
  73. data/lib/mocha/in_state_ordering_constraint.rb +5 -5
  74. data/lib/mocha/inspect.rb +8 -8
  75. data/lib/mocha/instance_method.rb +1 -17
  76. data/lib/mocha/integration.rb +9 -9
  77. data/lib/mocha/integration/mini_test/assertion_counter.rb +9 -9
  78. data/lib/mocha/integration/mini_test/exception_translation.rb +6 -6
  79. data/lib/mocha/integration/mini_test/version_13.rb +6 -6
  80. data/lib/mocha/integration/mini_test/version_140.rb +6 -6
  81. data/lib/mocha/integration/mini_test/version_141.rb +6 -6
  82. data/lib/mocha/integration/mini_test/version_142_to_172.rb +7 -7
  83. data/lib/mocha/integration/mini_test/version_200.rb +7 -7
  84. data/lib/mocha/integration/test_unit/assertion_counter.rb +10 -10
  85. data/lib/mocha/integration/test_unit/gem_version_200.rb +6 -6
  86. data/lib/mocha/integration/test_unit/gem_version_201_to_202.rb +6 -6
  87. data/lib/mocha/integration/test_unit/ruby_version_185_and_below.rb +6 -6
  88. data/lib/mocha/integration/test_unit/ruby_version_186_and_above.rb +6 -6
  89. data/lib/mocha/is_a.rb +1 -1
  90. data/lib/mocha/logger.rb +6 -6
  91. data/lib/mocha/method_matcher.rb +7 -7
  92. data/lib/mocha/mock.rb +85 -48
  93. data/lib/mocha/mockery.rb +47 -33
  94. data/lib/mocha/module_method.rb +2 -2
  95. data/lib/mocha/multiple_yields.rb +9 -9
  96. data/lib/mocha/names.rb +18 -18
  97. data/lib/mocha/no_yields.rb +7 -7
  98. data/lib/mocha/object.rb +104 -75
  99. data/lib/mocha/parameter_matchers.rb +3 -3
  100. data/lib/mocha/parameter_matchers/all_of.rb +22 -12
  101. data/lib/mocha/parameter_matchers/any_of.rb +23 -12
  102. data/lib/mocha/parameter_matchers/any_parameters.rb +15 -8
  103. data/lib/mocha/parameter_matchers/anything.rb +20 -11
  104. data/lib/mocha/parameter_matchers/base.rb +35 -27
  105. data/lib/mocha/parameter_matchers/equals.rb +23 -12
  106. data/lib/mocha/parameter_matchers/has_entries.rb +22 -12
  107. data/lib/mocha/parameter_matchers/has_entry.rb +31 -13
  108. data/lib/mocha/parameter_matchers/has_key.rb +21 -11
  109. data/lib/mocha/parameter_matchers/has_value.rb +21 -11
  110. data/lib/mocha/parameter_matchers/includes.rb +15 -5
  111. data/lib/mocha/parameter_matchers/instance_of.rb +23 -12
  112. data/lib/mocha/parameter_matchers/is_a.rb +23 -12
  113. data/lib/mocha/parameter_matchers/kind_of.rb +23 -12
  114. data/lib/mocha/parameter_matchers/not.rb +22 -12
  115. data/lib/mocha/parameter_matchers/object.rb +5 -3
  116. data/lib/mocha/parameter_matchers/optionally.rb +24 -12
  117. data/lib/mocha/parameter_matchers/query_string.rb +14 -3
  118. data/lib/mocha/parameter_matchers/regexp_matches.rb +21 -11
  119. data/lib/mocha/parameter_matchers/responds_with.rb +17 -6
  120. data/lib/mocha/parameter_matchers/yaml_equivalent.rb +16 -6
  121. data/lib/mocha/parameters_matcher.rb +8 -8
  122. data/lib/mocha/pretty_parameters.rb +7 -7
  123. data/lib/mocha/return_values.rb +11 -11
  124. data/lib/mocha/sequence.rb +23 -14
  125. data/lib/mocha/single_return_value.rb +7 -7
  126. data/lib/mocha/single_yield.rb +9 -9
  127. data/lib/mocha/standalone.rb +1 -1
  128. data/lib/mocha/state_machine.rb +61 -46
  129. data/lib/mocha/stubbing_error.rb +8 -5
  130. data/lib/mocha/thrower.rb +2 -2
  131. data/lib/mocha/unexpected_invocation.rb +9 -6
  132. data/lib/mocha/version.rb +1 -1
  133. data/lib/mocha/yield_parameters.rb +10 -10
  134. data/lib/mocha_standalone.rb +1 -1
  135. data/mocha.gemspec +4 -7
  136. data/test/acceptance/acceptance_test_helper.rb +10 -10
  137. data/test/acceptance/api_test.rb +20 -20
  138. data/test/acceptance/bug_18914_test.rb +12 -12
  139. data/test/acceptance/bug_21465_test.rb +6 -6
  140. data/test/acceptance/bug_21563_test.rb +5 -5
  141. data/test/acceptance/exception_rescue_test.rb +1 -1
  142. data/test/acceptance/expected_invocation_count_test.rb +17 -17
  143. data/test/acceptance/failure_messages_test.rb +13 -13
  144. data/test/acceptance/minitest_test.rb +39 -39
  145. data/test/acceptance/mocha_example_test.rb +26 -26
  146. data/test/acceptance/mocha_test_result_test.rb +13 -13
  147. data/test/acceptance/mock_test.rb +5 -5
  148. data/test/acceptance/mock_with_initializer_block_test.rb +5 -5
  149. data/test/acceptance/mocked_methods_dispatch_test.rb +4 -4
  150. data/test/acceptance/multiple_expectations_failure_message_test.rb +2 -2
  151. data/test/acceptance/optional_parameters_test.rb +5 -5
  152. data/test/acceptance/parameter_matcher_test.rb +18 -18
  153. data/test/acceptance/partial_mocks_test.rb +9 -9
  154. data/test/acceptance/raise_exception_test.rb +1 -1
  155. data/test/acceptance/return_value_test.rb +5 -5
  156. data/test/acceptance/sequence_test.rb +29 -29
  157. data/test/acceptance/states_test.rb +17 -17
  158. data/test/acceptance/stub_any_instance_method_test.rb +13 -13
  159. data/test/acceptance/stub_class_method_defined_on_active_record_association_proxy_test.rb +7 -7
  160. data/test/acceptance/stub_everything_test.rb +5 -5
  161. data/test/acceptance/stub_instance_method_defined_on_singleton_class_test.rb +1 -1
  162. data/test/acceptance/stub_module_method_test.rb +17 -17
  163. data/test/acceptance/stub_test.rb +5 -5
  164. data/test/acceptance/stubba_example_test.rb +24 -24
  165. data/test/acceptance/stubba_test_result_test.rb +11 -11
  166. data/test/acceptance/stubbing_error_backtrace_test.rb +6 -6
  167. data/test/acceptance/stubbing_frozen_object_test.rb +88 -0
  168. data/test/acceptance/stubbing_method_unnecessarily_test.rb +9 -9
  169. data/test/acceptance/stubbing_nil_test.rb +59 -0
  170. data/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +13 -13
  171. data/test/acceptance/stubbing_non_existent_class_method_test.rb +15 -15
  172. data/test/acceptance/stubbing_non_existent_instance_method_test.rb +14 -14
  173. data/test/acceptance/stubbing_non_public_any_instance_method_test.rb +13 -13
  174. data/test/acceptance/stubbing_non_public_class_method_test.rb +12 -12
  175. data/test/acceptance/stubbing_non_public_instance_method_test.rb +12 -12
  176. data/test/acceptance/stubbing_on_non_mock_object_test.rb +9 -9
  177. data/test/acceptance/stubbing_same_class_method_on_parent_and_child_classes_test.rb +35 -0
  178. data/test/acceptance/throw_test.rb +1 -1
  179. data/test/deprecation_disabler.rb +1 -1
  180. data/test/execution_point.rb +7 -7
  181. data/test/method_definer.rb +1 -1
  182. data/test/mini_test_result.rb +21 -21
  183. data/test/simple_counter.rb +5 -5
  184. data/test/test_helper.rb +1 -1
  185. data/test/test_runner.rb +22 -24
  186. data/test/test_unit_result.rb +20 -0
  187. data/test/unit/any_instance_method_test.rb +28 -32
  188. data/test/unit/array_inspect_test.rb +3 -3
  189. data/test/unit/backtrace_filter_test.rb +3 -3
  190. data/test/unit/cardinality_test.rb +12 -12
  191. data/test/unit/central_test.rb +18 -18
  192. data/test/unit/change_state_side_effect_test.rb +12 -12
  193. data/test/unit/class_method_test.rb +33 -91
  194. data/test/unit/date_time_inspect_test.rb +4 -4
  195. data/test/unit/exception_raiser_test.rb +5 -5
  196. data/test/unit/expectation_list_test.rb +7 -7
  197. data/test/unit/expectation_test.rb +66 -66
  198. data/test/unit/hash_inspect_test.rb +4 -4
  199. data/test/unit/in_state_ordering_constraint_test.rb +13 -13
  200. data/test/unit/method_matcher_test.rb +3 -3
  201. data/test/unit/mock_test.rb +40 -40
  202. data/test/unit/mockery_test.rb +25 -25
  203. data/test/unit/multiple_yields_test.rb +2 -2
  204. data/test/unit/no_yields_test.rb +2 -2
  205. data/test/unit/object_inspect_test.rb +4 -4
  206. data/test/unit/object_test.rb +15 -15
  207. data/test/unit/parameter_matchers/all_of_test.rb +6 -6
  208. data/test/unit/parameter_matchers/any_of_test.rb +6 -6
  209. data/test/unit/parameter_matchers/anything_test.rb +5 -5
  210. data/test/unit/parameter_matchers/has_entries_test.rb +10 -10
  211. data/test/unit/parameter_matchers/has_entry_test.rb +13 -13
  212. data/test/unit/parameter_matchers/has_key_test.rb +11 -11
  213. data/test/unit/parameter_matchers/has_value_test.rb +12 -12
  214. data/test/unit/parameter_matchers/includes_test.rb +4 -4
  215. data/test/unit/parameter_matchers/not_test.rb +6 -6
  216. data/test/unit/parameter_matchers/regexp_matches_test.rb +9 -9
  217. data/test/unit/parameter_matchers/responds_with_test.rb +6 -6
  218. data/test/unit/parameter_matchers/stub_matcher.rb +4 -4
  219. data/test/unit/parameter_matchers/yaml_equivalent_test.rb +6 -6
  220. data/test/unit/parameters_matcher_test.rb +16 -16
  221. data/test/unit/return_values_test.rb +5 -5
  222. data/test/unit/sequence_test.rb +10 -10
  223. data/test/unit/single_return_value_test.rb +3 -3
  224. data/test/unit/single_yield_test.rb +2 -2
  225. data/test/unit/state_machine_test.rb +19 -19
  226. data/test/unit/string_inspect_test.rb +2 -2
  227. data/test/unit/thrower_test.rb +1 -1
  228. data/test/unit/yield_parameters_test.rb +11 -11
  229. metadata +113 -101
@@ -0,0 +1,105 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4
+ <head>
5
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6
+ <title>
7
+ Top Level Namespace
8
+
9
+ &mdash; Mocha 0.11.0
10
+
11
+ </title>
12
+
13
+ <link rel="stylesheet" href="css/style.css" type="text/css" media="screen" charset="utf-8" />
14
+
15
+ <link rel="stylesheet" href="css/common.css" type="text/css" media="screen" charset="utf-8" />
16
+
17
+ <script type="text/javascript" charset="utf-8">
18
+ relpath = '';
19
+ if (relpath != '') relpath += '/';
20
+ </script>
21
+
22
+ <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
23
+
24
+ <script type="text/javascript" charset="utf-8" src="js/app.js"></script>
25
+
26
+
27
+ </head>
28
+ <body>
29
+ <script type="text/javascript" charset="utf-8">
30
+ if (window.top.frames.main) document.body.className = 'frames';
31
+ </script>
32
+
33
+ <div id="header">
34
+ <div id="menu">
35
+
36
+ <a href="_index.html">Index</a> &raquo;
37
+
38
+
39
+ <span class="title">Top Level Namespace</span>
40
+
41
+
42
+ <div class="noframes"><span class="title">(</span><a href="." target="_top">no frames</a><span class="title">)</span></div>
43
+ </div>
44
+
45
+ <div id="search">
46
+
47
+ <a id="class_list_link" href="#">Class List</a>
48
+
49
+ <a id="method_list_link" href="#">Method List</a>
50
+
51
+ <a id="file_list_link" href="#">File List</a>
52
+
53
+ </div>
54
+ <div class="clear"></div>
55
+ </div>
56
+
57
+ <iframe id="search_frame"></iframe>
58
+
59
+ <div id="content"><h1>Top Level Namespace
60
+
61
+
62
+
63
+ </h1>
64
+
65
+ <dl class="box">
66
+
67
+
68
+
69
+
70
+
71
+
72
+
73
+
74
+ </dl>
75
+ <div class="clear"></div>
76
+
77
+ <h2>Defined Under Namespace</h2>
78
+ <p class="children">
79
+
80
+
81
+ <strong class="modules">Modules:</strong> <span class='object_link'><a href="Mocha.html" title="Mocha (module)">Mocha</a></span>
82
+
83
+
84
+
85
+
86
+ </p>
87
+
88
+
89
+
90
+
91
+
92
+
93
+
94
+
95
+
96
+ </div>
97
+
98
+ <div id="footer">
99
+ Generated on Thu Apr 19 10:54:34 2012 by
100
+ <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
101
+ 0.7.5 (ruby-1.9.3).
102
+ </div>
103
+
104
+ </body>
105
+ </html>
@@ -3,7 +3,7 @@ require 'mocha/class_method'
3
3
  module Mocha
4
4
 
5
5
  class AnyInstanceMethod < ClassMethod
6
-
6
+
7
7
  def mock
8
8
  stubbee.any_instance.mocha
9
9
  end
@@ -15,7 +15,16 @@ module Mocha
15
15
  def hide_original_method
16
16
  if method_exists?(method)
17
17
  begin
18
- stubbee.send(:alias_method, hidden_method, method)
18
+ @original_method = stubbee.instance_method(method)
19
+ if @original_method && @original_method.owner == stubbee
20
+ @original_visibility = :public
21
+ if stubbee.protected_instance_methods.include?(method)
22
+ @original_visibility = :protected
23
+ elsif stubbee.private_instance_methods.include?(method)
24
+ @original_visibility = :private
25
+ end
26
+ stubbee.send(:remove_method, method)
27
+ end
19
28
  rescue NameError
20
29
  # deal with nasties like ActiveRecord::Associations::AssociationProxy
21
30
  end
@@ -35,13 +44,9 @@ module Mocha
35
44
  end
36
45
 
37
46
  def restore_original_method
38
- if method_exists?(hidden_method)
39
- begin
40
- stubbee.send(:alias_method, method, hidden_method)
41
- stubbee.send(:remove_method, hidden_method)
42
- rescue NameError
43
- # deal with nasties like ActiveRecord::Associations::AssociationProxy
44
- end
47
+ if @original_method && @original_method.owner == stubbee
48
+ stubbee.send(:define_method, method, @original_method)
49
+ stubbee.send(@original_visibility, method)
45
50
  end
46
51
  end
47
52
 
@@ -51,7 +56,7 @@ module Mocha
51
56
  return true if stubbee.private_instance_methods(false).include?(method)
52
57
  return false
53
58
  end
54
-
59
+
55
60
  end
56
-
61
+
57
62
  end
@@ -2,41 +2,41 @@ require 'mocha/parameter_matchers'
2
2
  require 'mocha/mockery'
3
3
  require 'mocha/sequence'
4
4
 
5
- module Mocha # :nodoc:
6
-
7
- # Methods added to Test::Unit::TestCase or equivalent.
5
+ module Mocha
6
+
7
+ # Methods added to +Test::Unit::TestCase+ or equivalent.
8
8
  module API
9
-
9
+
10
10
  include ParameterMatchers
11
-
12
- # :call-seq: mock(name, &block) -> mock object
13
- # mock(expected_methods = {}, &block) -> mock object
14
- # mock(name, expected_methods = {}, &block) -> mock object
15
- #
16
- # Creates a mock object.
17
- #
18
- # +name+ is a +String+ identifier for the mock object.
19
- #
20
- # +expected_methods+ is a +Hash+ with expected method name symbols as keys and corresponding return values as values.
21
- #
22
- # Note that (contrary to expectations set up by #stub) these expectations <b>must</b> be fulfilled during the test.
23
- # def test_product
24
- # product = mock('ipod_product', :manufacturer => 'ipod', :price => 100)
25
- # assert_equal 'ipod', product.manufacturer
26
- # assert_equal 100, product.price
27
- # # an error will be raised unless both Product#manufacturer and Product#price have been called
28
- # end
29
- #
30
- # +block+ is an optional block to be evaluated against the mock object instance, giving an alernative way to set up expectations & stubs.
31
- # def test_product
32
- # product = mock('ipod_product') do
33
- # expects(:manufacturer).returns('ipod')
34
- # expects(:price).returns(100)
11
+
12
+ # Builds a new mock object
13
+ #
14
+ # @param [String] name identifies mock object in error messages.
15
+ # @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.
16
+ # @yield optional block to be evaluated against the mock object instance, giving an alternative way to setup expectations.
17
+ # @return [Mock] a new mock object
18
+ #
19
+ # @overload def mock(name, &block)
20
+ # @overload def mock(expected_methods_vs_return_values = {}, &block)
21
+ # @overload def mock(name, expected_methods_vs_return_values = {}, &block)
22
+ #
23
+ # @example Using expected_methods_vs_return_values Hash to setup expectations.
24
+ # def test_motor_starts_and_stops
25
+ # motor = mock('motor', :start => true, :stop => true)
26
+ # assert motor.start
27
+ # assert motor.stop
28
+ # # an error will be raised unless both Motor#start and Motor#stop have been called
29
+ # end
30
+ # @example Using the optional block to setup expectations & stubbed methods.
31
+ # def test_motor_starts_and_stops
32
+ # motor = mock('motor') do
33
+ # expects(:start).with(100.rpm).returns(true)
34
+ # stubs(:stop).returns(true)
35
35
  # end
36
- # assert_equal 'ipod', product.manufacturer
37
- # assert_equal 100, product.price
38
- # # an error will be raised unless both Product#manufacturer and Product#price have been called
39
- # end
36
+ # assert motor.start(100.rpm)
37
+ # assert motor.stop
38
+ # # an error will only be raised if Motor#start(100.rpm) has not been called
39
+ # end
40
40
  def mock(*arguments, &block)
41
41
  name = arguments.shift if arguments.first.is_a?(String)
42
42
  expectations = arguments.shift || {}
@@ -44,33 +44,35 @@ module Mocha # :nodoc:
44
44
  mock.expects(expectations)
45
45
  mock
46
46
  end
47
-
48
- # :call-seq: stub(name, &block) -> mock object
49
- # stub(stubbed_methods = {}, &block) -> mock object
50
- # stub(name, stubbed_methods = {}, &block) -> mock object
51
- #
52
- # Creates a mock object.
53
- #
54
- # +name+ is a +String+ identifier for the mock object.
55
- #
56
- # +stubbed_methods+ is a +Hash+ with stubbed method name symbols as keys and corresponding return values as values.
57
- # Note that (contrary to expectations set up by #mock) these expectations <b>need not</b> be fulfilled during the test.
58
- # def test_product
59
- # product = stub('ipod_product', :manufacturer => 'ipod', :price => 100)
60
- # assert_equal 'ipod', product.manufacturer
61
- # assert_equal 100, product.price
62
- # # an error will not be raised even if Product#manufacturer and Product#price have not been called
47
+
48
+ # Builds a new mock object
49
+ #
50
+ # @param [String] name identifies mock object in error messages.
51
+ # @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.
52
+ # @yield optional block to be evaluated against the mock object instance, giving an alternative way to setup stubbed methods.
53
+ # @return [Mock] a new mock object
54
+ #
55
+ # @overload def stub(name, &block)
56
+ # @overload def stub(stubbed_methods_vs_return_values = {}, &block)
57
+ # @overload def stub(name, stubbed_methods_vs_return_values = {}, &block)
58
+ #
59
+ # @example Using stubbed_methods_vs_return_values Hash to setup stubbed methods.
60
+ # def test_motor_starts_and_stops
61
+ # motor = mock('motor', :start => true, :stop => true)
62
+ # assert motor.start
63
+ # assert motor.stop
64
+ # # an error will not be raised even if either Motor#start or Motor#stop has not been called
63
65
  # end
64
66
  #
65
- # +block+ is an optional block to be evaluated against the mock object instance, giving an alernative way to set up expectations & stubs.
66
- # def test_product
67
- # product = stub('ipod_product') do
68
- # stubs(:manufacturer).returns('ipod')
69
- # stubs(:price).returns(100)
67
+ # @example Using the optional block to setup expectations & stubbed methods.
68
+ # def test_motor_starts_and_stops
69
+ # motor = mock('motor') do
70
+ # expects(:start).with(100.rpm).returns(true)
71
+ # stubs(:stop).returns(true)
70
72
  # end
71
- # assert_equal 'ipod', product.manufacturer
72
- # assert_equal 100, product.price
73
- # # an error will not be raised even if Product#manufacturer and Product#price have not been called
73
+ # assert motor.start(100.rpm)
74
+ # assert motor.stop
75
+ # # an error will only be raised if Motor#start(100.rpm) has not been called
74
76
  # end
75
77
  def stub(*arguments, &block)
76
78
  name = arguments.shift if arguments.first.is_a?(String)
@@ -79,23 +81,24 @@ module Mocha # :nodoc:
79
81
  stub.stubs(expectations)
80
82
  stub
81
83
  end
82
-
83
- # :call-seq: stub_everything(name, &block) -> mock object
84
- # stub_everything(stubbed_methods = {}, &block) -> mock object
85
- # stub_everything(name, stubbed_methods = {}, &block) -> mock object
86
- #
87
- # Creates a mock object that accepts calls to any method.
88
- #
89
- # By default it will return +nil+ for any method call.
90
- #
91
- # +block+ is a block to be evaluated against the mock object instance, giving an alernative way to set up expectations & stubs.
92
- #
93
- # +name+ and +stubbed_methods+ work in the same way as for #stub.
94
- # def test_product
95
- # product = stub_everything('ipod_product', :price => 100)
96
- # assert_nil product.manufacturer
97
- # assert_nil product.any_old_method
98
- # assert_equal 100, product.price
84
+
85
+ # Builds a mock object that accepts calls to any method. By default it will return +nil+ for any method call.
86
+ #
87
+ # @param [String] name identifies mock object in error messages.
88
+ # @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.
89
+ # @yield optional block to be evaluated against the mock object instance, giving an alternative way to setup stubbed methods.
90
+ # @return [Mock] a new mock object
91
+ #
92
+ # @overload def stub_everything(name, &block)
93
+ # @overload def stub_everything(stubbed_methods_vs_return_values = {}, &block)
94
+ # @overload def stub_everything(name, stubbed_methods_vs_return_values = {}, &block)
95
+ #
96
+ # @example Ignore invocations of irrelevant methods.
97
+ # def test_motor_stops
98
+ # motor = stub_everything('motor', :stop => true)
99
+ # assert_nil motor.irrelevant_method_1 # => no error raised
100
+ # assert_nil motor.irrelevant_method_2 # => no error raised
101
+ # assert motor.stop
99
102
  # end
100
103
  def stub_everything(*arguments, &block)
101
104
  name = arguments.shift if arguments.first.is_a?(String)
@@ -105,69 +108,80 @@ module Mocha # :nodoc:
105
108
  stub.stubs(expectations)
106
109
  stub
107
110
  end
108
-
109
- # :call-seq: sequence(name) -> sequence
111
+
112
+ # Builds a new sequence which can be used to constrain the order in which expectations can occur.
113
+ #
114
+ # Specify that an expected invocation must occur within a named {Sequence} by using {Expectation#in_sequence}.
110
115
  #
111
- # Returns a new sequence that is used to constrain the order in which expectations can occur.
116
+ # @return [Sequence] a new sequence
112
117
  #
113
- # Specify that an expected invocation must occur in within a named +sequence+ by using Expectation#in_sequence.
118
+ # @see Expectation#in_sequence
114
119
  #
115
- # See also Expectation#in_sequence.
120
+ # @example Ensure methods on egg are invoked in correct order.
116
121
  # breakfast = sequence('breakfast')
117
122
  #
118
- # egg = mock('egg')
119
- # egg.expects(:crack).in_sequence(breakfast)
120
- # egg.expects(:fry).in_sequence(breakfast)
121
- # egg.expects(:eat).in_sequence(breakfast)
123
+ # egg = mock('egg') do
124
+ # expects(:crack).in_sequence(breakfast)
125
+ # expects(:fry).in_sequence(breakfast)
126
+ # expects(:eat).in_sequence(breakfast)
127
+ # end
122
128
  def sequence(name)
123
129
  Sequence.new(name)
124
130
  end
125
-
126
- # :call-seq: states(name) -> state_machine
131
+
132
+ # Builds a new state machine which can be used to constrain the order in which expectations can occur.
127
133
  #
128
- # Returns a new +state_machine+ that is used to constrain the order in which expectations can occur.
134
+ # Specify the initial state of the state machine by using {StateMachine#starts_as}.
129
135
  #
130
- # Specify the initial +state+ of the +state_machine+ by using StateMachine#starts_as.
136
+ # Specify that an expected invocation should change the state of the state machine by using {Expectation#then}.
131
137
  #
132
- # Specify that an expected invocation should change the +state+ of the +state_machine+ by using Expectation#then.
138
+ # Specify that an expected invocation should be constrained to occur within a particular +state+ by using {Expectation#when}.
133
139
  #
134
- # Specify that an expected invocation should be constrained to occur within a particular +state+ by using Expectation#when.
140
+ # A test can contain multiple state machines.
135
141
  #
136
- # A test can contain multiple +state_machines+.
142
+ # @return [StateMachine] a new state machine
137
143
  #
138
- # See also Expectation#then, Expectation#when and StateMachine.
144
+ # @see Expectation#then
145
+ # @see Expectation#when
146
+ # @see StateMachine
147
+ # @example Constrain expected invocations to occur in particular states.
139
148
  # power = states('power').starts_as('off')
140
149
  #
141
- # radio = mock('radio')
142
- # radio.expects(:switch_on).then(power.is('on'))
143
- # radio.expects(:select_channel).with('BBC Radio 4').when(power.is('on'))
144
- # radio.expects(:adjust_volume).with(+5).when(power.is('on'))
145
- # radio.expects(:select_channel).with('BBC World Service').when(power.is('on'))
146
- # radio.expects(:adjust_volume).with(-5).when(power.is('on'))
147
- # radio.expects(:switch_off).then(power.is('off'))
150
+ # radio = mock('radio') do
151
+ # expects(:switch_on).then(power.is('on'))
152
+ # expects(:select_channel).with('BBC Radio 4').when(power.is('on'))
153
+ # expects(:adjust_volume).with(+5).when(power.is('on'))
154
+ # expects(:select_channel).with('BBC World Service').when(power.is('on'))
155
+ # expects(:adjust_volume).with(-5).when(power.is('on'))
156
+ # expects(:switch_off).then(power.is('off'))
157
+ # end
148
158
  def states(name)
149
159
  Mockery.instance.new_state_machine(name)
150
160
  end
151
-
152
- def mocha_setup # :nodoc:
161
+
162
+ # @private
163
+ def mocha_setup
153
164
  end
154
-
155
- def mocha_verify(assertion_counter = nil) # :nodoc:
165
+
166
+ # @private
167
+ def mocha_verify(assertion_counter = nil)
156
168
  Mockery.instance.verify(assertion_counter)
157
169
  end
158
-
159
- def mocha_teardown # :nodoc:
170
+
171
+ # @private
172
+ def mocha_teardown
160
173
  Mockery.instance.teardown
161
174
  Mockery.reset_instance
162
175
  end
163
-
176
+
164
177
  end
165
-
178
+
179
+ # @private
166
180
  def self.const_missing(name)
167
181
  return super unless name == :Standalone
168
182
  require 'mocha/deprecation'
169
183
  Deprecation.warning "Mocha::Standalone has been renamed to Mocha::API"
170
184
  return API
171
185
  end
172
-
186
+
173
187
  end
@@ -1,11 +1,11 @@
1
1
  module Mocha
2
-
2
+
3
3
  class ArgumentIterator
4
-
4
+
5
5
  def initialize(argument)
6
6
  @argument = argument
7
7
  end
8
-
8
+
9
9
  def each(&block)
10
10
  if @argument.is_a?(Hash) then
11
11
  @argument.each do |method_name, return_value|
@@ -15,7 +15,7 @@ module Mocha
15
15
  block.call(@argument)
16
16
  end
17
17
  end
18
-
18
+
19
19
  end
20
-
21
- end
20
+
21
+ end