mocha 1.9.0 → 1.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (200) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +3 -0
  3. data/.rubocop_todo.yml +7 -30
  4. data/README.md +30 -11
  5. data/RELEASE.md +58 -0
  6. data/Rakefile +9 -3
  7. data/docs/CNAME +1 -0
  8. data/docs/Mocha.html +132 -5
  9. data/docs/Mocha/API.html +313 -216
  10. data/docs/Mocha/ClassMethods.html +13 -16
  11. data/docs/Mocha/Configuration.html +931 -174
  12. data/docs/Mocha/Expectation.html +200 -255
  13. data/docs/Mocha/ExpectationError.html +5 -10
  14. data/docs/Mocha/ExpectationErrorFactory.html +9 -18
  15. data/docs/Mocha/Hooks.html +12 -27
  16. data/docs/Mocha/Integration.html +3 -3
  17. data/docs/Mocha/Integration/MiniTest.html +3 -3
  18. data/docs/Mocha/Integration/MiniTest/Adapter.html +5 -6
  19. data/docs/Mocha/Integration/TestUnit.html +3 -3
  20. data/docs/Mocha/Integration/TestUnit/Adapter.html +5 -6
  21. data/docs/Mocha/Mock.html +202 -162
  22. data/docs/Mocha/ObjectMethods.html +121 -68
  23. data/docs/Mocha/ParameterMatchers.html +21 -109
  24. data/docs/Mocha/ParameterMatchers/AllOf.html +4 -5
  25. data/docs/Mocha/ParameterMatchers/AnyOf.html +4 -5
  26. data/docs/Mocha/ParameterMatchers/AnyParameters.html +3 -3
  27. data/docs/Mocha/ParameterMatchers/Anything.html +3 -3
  28. data/docs/Mocha/ParameterMatchers/Base.html +8 -15
  29. data/docs/Mocha/ParameterMatchers/Equals.html +4 -5
  30. data/docs/Mocha/ParameterMatchers/EquivalentUri.html +3 -3
  31. data/docs/Mocha/ParameterMatchers/HasEntries.html +4 -5
  32. data/docs/Mocha/ParameterMatchers/HasEntry.html +4 -5
  33. data/docs/Mocha/ParameterMatchers/HasKey.html +4 -5
  34. data/docs/Mocha/ParameterMatchers/HasValue.html +4 -5
  35. data/docs/Mocha/ParameterMatchers/Includes.html +4 -5
  36. data/docs/Mocha/ParameterMatchers/InstanceOf.html +4 -5
  37. data/docs/Mocha/ParameterMatchers/IsA.html +3 -3
  38. data/docs/Mocha/ParameterMatchers/KindOf.html +4 -5
  39. data/docs/Mocha/ParameterMatchers/Not.html +4 -5
  40. data/docs/Mocha/ParameterMatchers/Optionally.html +3 -3
  41. data/docs/Mocha/ParameterMatchers/RegexpMatches.html +4 -5
  42. data/docs/Mocha/ParameterMatchers/RespondsWith.html +4 -5
  43. data/docs/Mocha/ParameterMatchers/YamlEquivalent.html +4 -5
  44. data/docs/Mocha/Sequence.html +3 -3
  45. data/docs/Mocha/StateMachine.html +13 -25
  46. data/docs/Mocha/StateMachine/State.html +4 -5
  47. data/docs/Mocha/StateMachine/StatePredicate.html +4 -5
  48. data/docs/Mocha/StubbingError.html +3 -3
  49. data/docs/_index.html +4 -22
  50. data/docs/class_list.html +1 -1
  51. data/docs/file.COPYING.html +3 -3
  52. data/docs/file.MIT-LICENSE.html +3 -3
  53. data/docs/file.README.html +36 -14
  54. data/docs/file.RELEASE.html +72 -3
  55. data/docs/frames.html +1 -1
  56. data/docs/index.html +36 -14
  57. data/docs/method_list.html +96 -40
  58. data/docs/top-level-namespace.html +3 -3
  59. data/gemfiles/Gemfile.minitest.5.11.3 +7 -0
  60. data/init.rb +1 -3
  61. data/lib/mocha.rb +8 -0
  62. data/lib/mocha/any_instance_method.rb +9 -25
  63. data/lib/mocha/api.rb +65 -66
  64. data/lib/mocha/cardinality.rb +26 -11
  65. data/lib/mocha/class_methods.rb +17 -15
  66. data/lib/mocha/configuration.rb +295 -67
  67. data/lib/mocha/detection/test_unit.rb +1 -3
  68. data/lib/mocha/exception_raiser.rb +2 -1
  69. data/lib/mocha/expectation.rb +56 -58
  70. data/lib/mocha/expectation_error.rb +1 -3
  71. data/lib/mocha/expectation_list.rb +6 -6
  72. data/lib/mocha/inspect.rb +28 -26
  73. data/lib/mocha/instance_method.rb +19 -2
  74. data/lib/mocha/integration.rb +1 -3
  75. data/lib/mocha/integration/mini_test.rb +7 -0
  76. data/lib/mocha/integration/test_unit.rb +7 -0
  77. data/lib/mocha/invocation.rb +61 -0
  78. data/lib/mocha/macos_version.rb +5 -0
  79. data/lib/mocha/minitest.rb +6 -1
  80. data/lib/mocha/mock.rb +46 -31
  81. data/lib/mocha/mockery.rb +28 -61
  82. data/lib/mocha/names.rb +1 -1
  83. data/lib/mocha/object_methods.rb +13 -19
  84. data/lib/mocha/parameter_matchers.rb +1 -1
  85. data/lib/mocha/parameter_matchers/all_of.rb +1 -1
  86. data/lib/mocha/parameter_matchers/any_of.rb +1 -1
  87. data/lib/mocha/parameter_matchers/equivalent_uri.rb +0 -9
  88. data/lib/mocha/parameter_matchers/includes.rb +2 -0
  89. data/lib/mocha/parameter_matchers/instance_methods.rb +18 -0
  90. data/lib/mocha/raised_exception.rb +11 -0
  91. data/lib/mocha/return_values.rb +3 -3
  92. data/lib/mocha/setup.rb +5 -0
  93. data/lib/mocha/single_return_value.rb +2 -1
  94. data/lib/mocha/singleton_class.rb +9 -0
  95. data/lib/mocha/{class_method.rb → stubbed_method.rb} +23 -43
  96. data/lib/mocha/test_unit.rb +6 -1
  97. data/lib/mocha/thrower.rb +2 -1
  98. data/lib/mocha/thrown_object.rb +12 -0
  99. data/lib/mocha/version.rb +1 -1
  100. data/mocha.gemspec +1 -3
  101. data/test/acceptance/acceptance_test_helper.rb +6 -0
  102. data/test/acceptance/bug_18914_test.rb +0 -1
  103. data/test/acceptance/bug_21465_test.rb +0 -1
  104. data/test/acceptance/bug_21563_test.rb +0 -1
  105. data/test/acceptance/display_matching_invocations_alongside_expectations_test.rb +69 -0
  106. data/test/acceptance/exception_rescue_test.rb +1 -2
  107. data/test/acceptance/expectations_on_multiple_methods_test.rb +0 -1
  108. data/test/acceptance/expected_invocation_count_test.rb +2 -3
  109. data/test/acceptance/failure_messages_test.rb +0 -1
  110. data/test/acceptance/issue_272_test.rb +1 -2
  111. data/test/acceptance/issue_65_test.rb +0 -1
  112. data/test/acceptance/issue_70_test.rb +0 -1
  113. data/test/acceptance/mocha_example_test.rb +0 -1
  114. data/test/acceptance/mocha_test_result_test.rb +0 -1
  115. data/test/acceptance/mock_test.rb +47 -6
  116. data/test/acceptance/mocked_methods_dispatch_test.rb +0 -1
  117. data/test/acceptance/multiple_expectations_failure_message_test.rb +0 -1
  118. data/test/acceptance/optional_parameters_test.rb +0 -1
  119. data/test/acceptance/parameter_matcher_test.rb +0 -1
  120. data/test/acceptance/partial_mocks_test.rb +0 -1
  121. data/test/acceptance/prepend_test.rb +0 -1
  122. data/test/acceptance/prevent_use_of_mocha_outside_test_test.rb +0 -1
  123. data/test/acceptance/raise_exception_test.rb +0 -1
  124. data/test/acceptance/return_value_test.rb +0 -1
  125. data/test/acceptance/sequence_test.rb +0 -1
  126. data/test/acceptance/states_test.rb +0 -1
  127. data/test/acceptance/stub_any_instance_method_defined_on_superclass_test.rb +1 -2
  128. data/test/acceptance/stub_any_instance_method_test.rb +20 -1
  129. data/test/acceptance/stub_class_method_defined_on_active_record_association_proxy_test.rb +0 -1
  130. data/test/acceptance/stub_class_method_defined_on_class_test.rb +0 -1
  131. data/test/acceptance/stub_class_method_defined_on_module_test.rb +0 -1
  132. data/test/acceptance/stub_class_method_defined_on_superclass_test.rb +1 -2
  133. data/test/acceptance/stub_everything_test.rb +0 -1
  134. data/test/acceptance/stub_instance_method_defined_on_active_record_association_proxy_test.rb +0 -1
  135. data/test/acceptance/stub_instance_method_defined_on_class_and_aliased_test.rb +0 -1
  136. data/test/acceptance/stub_instance_method_defined_on_class_test.rb +0 -1
  137. data/test/acceptance/stub_instance_method_defined_on_kernel_module_test.rb +0 -1
  138. data/test/acceptance/stub_instance_method_defined_on_module_test.rb +0 -1
  139. data/test/acceptance/stub_instance_method_defined_on_object_class_test.rb +0 -1
  140. data/test/acceptance/stub_instance_method_defined_on_singleton_class_test.rb +0 -1
  141. data/test/acceptance/stub_instance_method_defined_on_superclass_test.rb +0 -1
  142. data/test/acceptance/stub_method_defined_on_module_and_aliased_test.rb +0 -1
  143. data/test/acceptance/stub_module_method_test.rb +0 -1
  144. data/test/acceptance/stub_test.rb +0 -1
  145. data/test/acceptance/stubba_example_test.rb +0 -1
  146. data/test/acceptance/stubba_test_result_test.rb +0 -1
  147. data/test/acceptance/stubbing_error_backtrace_test.rb +4 -5
  148. data/test/acceptance/stubbing_frozen_object_test.rb +0 -1
  149. data/test/acceptance/stubbing_method_accepting_block_parameter_test.rb +0 -1
  150. data/test/acceptance/stubbing_method_unnecessarily_test.rb +5 -5
  151. data/test/acceptance/stubbing_nil_test.rb +5 -5
  152. data/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +11 -11
  153. data/test/acceptance/stubbing_non_existent_class_method_test.rb +11 -11
  154. data/test/acceptance/stubbing_non_existent_instance_method_test.rb +11 -11
  155. data/test/acceptance/stubbing_non_public_any_instance_method_test.rb +8 -8
  156. data/test/acceptance/stubbing_non_public_class_method_test.rb +9 -9
  157. data/test/acceptance/stubbing_non_public_instance_method_test.rb +9 -9
  158. data/test/acceptance/stubbing_on_non_mock_object_test.rb +5 -5
  159. data/test/acceptance/stubbing_same_class_method_on_parent_and_child_classes_test.rb +0 -1
  160. data/test/acceptance/throw_test.rb +0 -1
  161. data/test/acceptance/unexpected_invocation_test.rb +0 -1
  162. data/test/acceptance/unstubbing_test.rb +0 -1
  163. data/test/integration/shared_tests.rb +5 -3
  164. data/test/method_definer.rb +11 -17
  165. data/test/test_runner.rb +2 -0
  166. data/test/unit/any_instance_method_test.rb +41 -40
  167. data/test/unit/cardinality_test.rb +41 -23
  168. data/test/unit/central_test.rb +0 -1
  169. data/test/unit/class_methods_test.rb +1 -1
  170. data/test/unit/configuration_test.rb +12 -12
  171. data/test/unit/exception_raiser_test.rb +10 -5
  172. data/test/unit/expectation_list_test.rb +13 -11
  173. data/test/unit/expectation_test.rb +88 -103
  174. data/test/unit/instance_method_test.rb +282 -0
  175. data/test/unit/mock_test.rb +28 -19
  176. data/test/unit/mockery_test.rb +8 -11
  177. data/test/unit/module_methods_test.rb +2 -3
  178. data/test/unit/object_inspect_test.rb +6 -4
  179. data/test/unit/object_methods_test.rb +3 -2
  180. data/test/unit/parameter_matchers/equivalent_uri_test.rb +0 -9
  181. data/test/unit/parameter_matchers/has_entries_test.rb +1 -1
  182. data/test/unit/parameter_matchers/has_entry_test.rb +1 -1
  183. data/test/unit/parameter_matchers/has_key_test.rb +1 -1
  184. data/test/unit/parameter_matchers/has_value_test.rb +1 -1
  185. data/test/unit/parameter_matchers/includes_test.rb +1 -1
  186. data/test/unit/parameter_matchers/responds_with_test.rb +1 -1
  187. data/test/unit/return_values_test.rb +25 -20
  188. data/test/unit/single_return_value_test.rb +6 -1
  189. data/test/unit/thrower_test.rb +7 -2
  190. metadata +14 -28
  191. data/docs/Mocha/UnexpectedInvocation.html +0 -140
  192. data/lib/mocha/mini_test.rb +0 -5
  193. data/lib/mocha/module_method.rb +0 -6
  194. data/lib/mocha/module_methods.rb +0 -10
  195. data/lib/mocha/parameter_matchers/object.rb +0 -15
  196. data/lib/mocha/standalone.rb +0 -4
  197. data/lib/mocha/unexpected_invocation.rb +0 -24
  198. data/lib/mocha_standalone.rb +0 -4
  199. data/test/acceptance/mock_with_initializer_block_test.rb +0 -56
  200. data/test/unit/class_method_test.rb +0 -276
@@ -1,5 +1,4 @@
1
1
  require File.expand_path('../acceptance_test_helper', __FILE__)
2
- require 'mocha/setup'
3
2
 
4
3
  class ThrowTest < Mocha::TestCase
5
4
  include AcceptanceTest
@@ -1,5 +1,4 @@
1
1
  require File.expand_path('../acceptance_test_helper', __FILE__)
2
- require 'mocha/setup'
3
2
 
4
3
  class UnexpectedInvocationTest < Mocha::TestCase
5
4
  include AcceptanceTest
@@ -1,5 +1,4 @@
1
1
  require File.expand_path('../acceptance_test_helper', __FILE__)
2
- require 'mocha/setup'
3
2
 
4
3
  class UnstubbingTest < Mocha::TestCase
5
4
  include AcceptanceTest
@@ -1,6 +1,7 @@
1
1
  require 'test_runner'
2
2
  require 'execution_point'
3
3
 
4
+ # rubocop:disable Metrics/ModuleLength
4
5
  module SharedTests
5
6
  include TestRunner
6
7
 
@@ -63,7 +64,7 @@ module SharedTests
63
64
  assert_equal [
64
65
  'not all expectations were satisfied',
65
66
  'unsatisfied expectations:',
66
- '- expected exactly once, not yet invoked: #<Mock:expecting invocation>.expected(any_parameters)'
67
+ '- expected exactly once, invoked never: #<Mock:expecting invocation>.expected(any_parameters)'
67
68
  ], test_result.failure_message_lines
68
69
  end
69
70
 
@@ -101,7 +102,7 @@ module SharedTests
101
102
  assert_equal [
102
103
  'not all expectations were satisfied',
103
104
  'unsatisfied expectations:',
104
- '- expected exactly once, not yet invoked: #<Mock:expecting invocation>.expected(any_parameters)'
105
+ '- expected exactly once, invoked never: #<Mock:expecting invocation>.expected(any_parameters)'
105
106
  ], test_result.failure_message_lines
106
107
  end
107
108
 
@@ -151,7 +152,7 @@ module SharedTests
151
152
  assert_equal [
152
153
  'not all expectations were satisfied',
153
154
  'unsatisfied expectations:',
154
- "- expected exactly once, not yet invoked: #{object.mocha_inspect}.expected(any_parameters)"
155
+ "- expected exactly once, invoked never: #{object.mocha_inspect}.expected(any_parameters)"
155
156
  ], test_result.failure_message_lines
156
157
  end
157
158
 
@@ -174,3 +175,4 @@ module SharedTests
174
175
  end
175
176
  # rubocop:enable Style/Semicolon
176
177
  end
178
+ # rubocop:enable Metrics/ModuleLength
@@ -1,22 +1,16 @@
1
- require 'metaclass'
1
+ require 'mocha/singleton_class'
2
2
 
3
- module Mocha
4
- module ObjectMethods
5
- def define_instance_method(method_symbol, &block)
6
- __metaclass__.send(:define_method, method_symbol, block)
7
- end
8
-
9
- def replace_instance_method(method_symbol, &block)
10
- raise "Cannot replace #{method_symbol} as #{self} does not respond to it." unless respond_to?(method_symbol)
11
- define_instance_method(method_symbol, &block)
12
- end
3
+ module MethodDefiner
4
+ def define_instance_method(object, method_symbol, &block)
5
+ object.singleton_class.send(:define_method, method_symbol, block)
6
+ end
13
7
 
14
- def define_instance_accessor(*symbols)
15
- symbols.each { |symbol| __metaclass__.send(:attr_accessor, symbol) }
16
- end
8
+ def replace_instance_method(object, method_symbol, &block)
9
+ raise "Cannot replace #{method_symbol} as #{self} does not respond to it." unless object.respond_to?(method_symbol)
10
+ define_instance_method(object, method_symbol, &block)
17
11
  end
18
- end
19
12
 
20
- class Object
21
- include Mocha::ObjectMethods
13
+ def define_instance_accessor(object, *symbols)
14
+ symbols.each { |symbol| object.singleton_class.send(:attr_accessor, symbol) }
15
+ end
22
16
  end
@@ -7,6 +7,7 @@ module TestRunner
7
7
  run_as_tests(:test_me => block)
8
8
  end
9
9
 
10
+ # rubocop:disable Metrics/AbcSize
10
11
  def run_as_tests(methods = {})
11
12
  base_class = Mocha::TestCase
12
13
  test_class = Class.new(base_class) do
@@ -44,6 +45,7 @@ module TestRunner
44
45
 
45
46
  test_result
46
47
  end
48
+ # rubocop:enable Metrics/AbcSize
47
49
 
48
50
  def assert_passed(test_result)
49
51
  flunk "Test failed unexpectedly with message: #{test_result.failures}" if test_result.failure_count > 0
@@ -1,14 +1,23 @@
1
1
  require File.expand_path('../../test_helper', __FILE__)
2
2
  require 'method_definer'
3
+ require 'mocha/class_methods'
3
4
  require 'mocha/mock'
4
5
  require 'mocha/any_instance_method'
5
6
 
6
7
  class AnyInstanceMethodTest < Mocha::TestCase
8
+ include MethodDefiner
7
9
  include Mocha
8
10
 
11
+ def class_with_method(method, result = nil)
12
+ Class.new do
13
+ extend ClassMethods
14
+ define_method(method) { result } if method
15
+ end
16
+ end
17
+
9
18
  unless RUBY_V2_PLUS
10
19
  def test_should_hide_original_method
11
- klass = Class.new { def method_x; end }
20
+ klass = class_with_method(:method_x)
12
21
  method = AnyInstanceMethod.new(klass, :method_x)
13
22
 
14
23
  method.hide_original_method
@@ -18,20 +27,20 @@ class AnyInstanceMethodTest < Mocha::TestCase
18
27
  end
19
28
 
20
29
  def test_should_not_raise_error_hiding_method_that_isnt_defined
21
- klass = Class.new
30
+ klass = class_with_method(:irrelevant)
22
31
  method = AnyInstanceMethod.new(klass, :method_x)
23
32
 
24
33
  assert_nothing_raised { method.hide_original_method }
25
34
  end
26
35
 
27
36
  def test_should_define_a_new_method
28
- klass = Class.new { def method_x; end }
37
+ klass = class_with_method(:method_x)
29
38
  method = AnyInstanceMethod.new(klass, :method_x)
30
39
  mocha = build_mock
31
40
  mocha.expects(:method_x).with(:param1, :param2).returns(:result)
32
41
  any_instance = Object.new
33
- any_instance.define_instance_method(:mocha) { mocha }
34
- klass.define_instance_method(:any_instance) { any_instance }
42
+ define_instance_method(any_instance, :mocha) { mocha }
43
+ define_instance_method(klass, :any_instance) { any_instance }
35
44
 
36
45
  method.hide_original_method
37
46
  method.define_new_method
@@ -44,19 +53,19 @@ class AnyInstanceMethodTest < Mocha::TestCase
44
53
  end
45
54
 
46
55
  def test_should_include_the_filename_and_line_number_in_exceptions
47
- klass = Class.new { def method_x; end }
56
+ klass = class_with_method(:method_x)
48
57
  method = AnyInstanceMethod.new(klass, :method_x)
49
58
  mocha = build_mock
50
59
  mocha.stubs(:method_x).raises(Exception)
51
60
  any_instance = Object.new
52
- any_instance.define_instance_method(:mocha) { mocha }
53
- klass.define_instance_method(:any_instance) { any_instance }
61
+ define_instance_method(any_instance, :mocha) { mocha }
62
+ define_instance_method(klass, :any_instance) { any_instance }
54
63
 
55
64
  method.hide_original_method
56
65
  method.define_new_method
57
66
 
58
- expected_filename = 'any_instance_method.rb'
59
- expected_line_number = 30
67
+ expected_filename = 'stubbed_method.rb'
68
+ expected_line_number = 61
60
69
 
61
70
  exception = assert_raises(Exception) { klass.new.method_x }
62
71
  matching_line = exception.backtrace.find do |line|
@@ -68,11 +77,7 @@ class AnyInstanceMethodTest < Mocha::TestCase
68
77
  end
69
78
 
70
79
  def test_should_restore_original_method
71
- klass = Class.new do
72
- def method_x
73
- :original_result
74
- end
75
- end
80
+ klass = class_with_method(:method_x, :original_result)
76
81
  method = AnyInstanceMethod.new(klass, :method_x)
77
82
 
78
83
  method.hide_original_method
@@ -86,11 +91,7 @@ class AnyInstanceMethodTest < Mocha::TestCase
86
91
  end
87
92
 
88
93
  def test_should_not_restore_original_method_if_none_was_defined_in_first_place
89
- klass = Class.new do
90
- def method_x
91
- :new_result
92
- end
93
- end
94
+ klass = class_with_method(:method_x, :new_result)
94
95
  method = AnyInstanceMethod.new(klass, :method_x)
95
96
 
96
97
  method.restore_original_method
@@ -100,16 +101,16 @@ class AnyInstanceMethodTest < Mocha::TestCase
100
101
  end
101
102
 
102
103
  def test_should_call_remove_new_method
103
- klass = Class.new { def method_x; end }
104
+ klass = class_with_method(:method_x)
104
105
  any_instance = build_mock
105
106
  any_instance_mocha = build_mock
106
107
  any_instance.stubs(:mocha).returns(any_instance_mocha)
107
- klass.define_instance_method(:any_instance) { any_instance }
108
+ define_instance_method(klass, :any_instance) { any_instance }
108
109
  method = AnyInstanceMethod.new(klass, :method_x)
109
- method.replace_instance_method(:restore_original_method) {}
110
- method.replace_instance_method(:reset_mocha) {}
111
- method.define_instance_accessor(:remove_called)
112
- method.replace_instance_method(:remove_new_method) { self.remove_called = true }
110
+ replace_instance_method(method, :restore_original_method) {}
111
+ replace_instance_method(method, :reset_mocha) {}
112
+ define_instance_accessor(method, :remove_called)
113
+ replace_instance_method(method, :remove_new_method) { self.remove_called = true }
113
114
 
114
115
  method.unstub
115
116
 
@@ -117,16 +118,16 @@ class AnyInstanceMethodTest < Mocha::TestCase
117
118
  end
118
119
 
119
120
  def test_should_call_restore_original_method
120
- klass = Class.new { def method_x; end }
121
+ klass = class_with_method(:method_x)
121
122
  any_instance = build_mock
122
123
  any_instance_mocha = build_mock
123
124
  any_instance.stubs(:mocha).returns(any_instance_mocha)
124
- klass.define_instance_method(:any_instance) { any_instance }
125
+ define_instance_method(klass, :any_instance) { any_instance }
125
126
  method = AnyInstanceMethod.new(klass, :method_x)
126
- method.replace_instance_method(:remove_new_method) {}
127
- method.replace_instance_method(:reset_mocha) {}
128
- method.define_instance_accessor(:restore_called)
129
- method.replace_instance_method(:restore_original_method) { self.restore_called = true }
127
+ replace_instance_method(method, :remove_new_method) {}
128
+ replace_instance_method(method, :reset_mocha) {}
129
+ define_instance_accessor(method, :restore_called)
130
+ replace_instance_method(method, :restore_original_method) { self.restore_called = true }
130
131
 
131
132
  method.unstub
132
133
 
@@ -134,12 +135,12 @@ class AnyInstanceMethodTest < Mocha::TestCase
134
135
  end
135
136
 
136
137
  def test_should_call_mock_unstub
137
- klass = Class.new { def method_x; end }
138
+ klass = class_with_method(:method_x)
138
139
 
139
140
  method = AnyInstanceMethod.new(klass, :method_x)
140
141
 
141
- method.replace_instance_method(:remove_new_method) {}
142
- method.replace_instance_method(:restore_original_method) {}
142
+ replace_instance_method(method, :remove_new_method) {}
143
+ replace_instance_method(method, :restore_original_method) {}
143
144
  mocha = Class.new do
144
145
  class << self
145
146
  attr_accessor :unstub_method
@@ -148,8 +149,8 @@ class AnyInstanceMethodTest < Mocha::TestCase
148
149
  self.unstub_method = method
149
150
  end
150
151
  end
151
- mocha.define_instance_method(:any_expectations?) { true }
152
- method.replace_instance_method(:mock) { mocha }
152
+ define_instance_method(mocha, :any_expectations?) { true }
153
+ replace_instance_method(method, :mock) { mocha }
153
154
 
154
155
  method.unstub
155
156
 
@@ -159,9 +160,9 @@ class AnyInstanceMethodTest < Mocha::TestCase
159
160
  def test_should_return_any_instance_mocha_for_stubbee
160
161
  mocha = Object.new
161
162
  any_instance = Object.new
162
- any_instance.define_instance_method(:mocha) { mocha }
163
- stubbee = Class.new
164
- stubbee.define_instance_method(:any_instance) { any_instance }
163
+ define_instance_method(any_instance, :mocha) { mocha }
164
+ stubbee = class_with_method(:method_x)
165
+ define_instance_method(stubbee, :any_instance) { any_instance }
165
166
  method = AnyInstanceMethod.new(stubbee, :method_name)
166
167
  assert_equal stubbee.any_instance.mocha, method.mock
167
168
  end
@@ -1,43 +1,61 @@
1
1
  require File.expand_path('../../test_helper', __FILE__)
2
2
  require 'mocha/cardinality'
3
+ require 'mocha/invocation'
4
+ require 'mocha/return_values'
5
+ require 'mocha/yield_parameters'
3
6
 
4
7
  class CardinalityTest < Mocha::TestCase
5
8
  include Mocha
6
9
 
10
+ def new_invocation
11
+ Invocation.new(:irrelevant, :irrelevant)
12
+ end
13
+
7
14
  def test_should_allow_invocations_if_invocation_count_has_not_yet_reached_maximum
8
15
  cardinality = Cardinality.new(2, 3)
9
- assert cardinality.invocations_allowed?(0)
10
- assert cardinality.invocations_allowed?(1)
11
- assert cardinality.invocations_allowed?(2)
12
- assert !cardinality.invocations_allowed?(3)
16
+ assert cardinality.invocations_allowed?
17
+ cardinality << new_invocation
18
+ assert cardinality.invocations_allowed?
19
+ cardinality << new_invocation
20
+ assert cardinality.invocations_allowed?
21
+ cardinality << new_invocation
22
+ assert !cardinality.invocations_allowed?
13
23
  end
14
24
 
15
25
  def test_should_be_satisfied_if_invocations_so_far_have_reached_required_threshold
16
26
  cardinality = Cardinality.new(2, 3)
17
- assert !cardinality.satisfied?(0)
18
- assert !cardinality.satisfied?(1)
19
- assert cardinality.satisfied?(2)
20
- assert cardinality.satisfied?(3)
27
+ assert !cardinality.satisfied?
28
+ cardinality << new_invocation
29
+ assert !cardinality.satisfied?
30
+ cardinality << new_invocation
31
+ assert cardinality.satisfied?
32
+ cardinality << new_invocation
33
+ assert cardinality.satisfied?
21
34
  end
22
35
 
23
- def test_should_describe_cardinality
24
- assert_equal 'allowed any number of times', Cardinality.at_least(0).mocha_inspect
25
-
26
- assert_equal 'expected at most once', Cardinality.at_most(1).mocha_inspect
27
- assert_equal 'expected at most twice', Cardinality.at_most(2).mocha_inspect
28
- assert_equal 'expected at most 3 times', Cardinality.at_most(3).mocha_inspect
36
+ def test_should_describe_cardinality_defined_using_at_least
37
+ assert_equal 'allowed any number of times', Cardinality.at_least(0).anticipated_times
38
+ assert_equal 'expected at least once', Cardinality.at_least(1).anticipated_times
39
+ assert_equal 'expected at least twice', Cardinality.at_least(2).anticipated_times
40
+ assert_equal 'expected at least 3 times', Cardinality.at_least(3).anticipated_times
41
+ end
29
42
 
30
- assert_equal 'expected at least once', Cardinality.at_least(1).mocha_inspect
31
- assert_equal 'expected at least twice', Cardinality.at_least(2).mocha_inspect
32
- assert_equal 'expected at least 3 times', Cardinality.at_least(3).mocha_inspect
43
+ def test_should_describe_cardinality_defined_using_at_most
44
+ assert_equal 'expected at most once', Cardinality.at_most(1).anticipated_times
45
+ assert_equal 'expected at most twice', Cardinality.at_most(2).anticipated_times
46
+ assert_equal 'expected at most 3 times', Cardinality.at_most(3).anticipated_times
47
+ end
33
48
 
34
- assert_equal 'expected never', Cardinality.exactly(0).mocha_inspect
35
- assert_equal 'expected exactly once', Cardinality.exactly(1).mocha_inspect
36
- assert_equal 'expected exactly twice', Cardinality.exactly(2).mocha_inspect
37
- assert_equal 'expected exactly 3 times', Cardinality.times(3).mocha_inspect
49
+ def test_should_describe_cardinality_defined_using_exactly
50
+ assert_equal 'expected never', Cardinality.exactly(0).anticipated_times
51
+ assert_equal 'expected exactly once', Cardinality.exactly(1).anticipated_times
52
+ assert_equal 'expected exactly twice', Cardinality.exactly(2).anticipated_times
53
+ assert_equal 'expected exactly 3 times', Cardinality.exactly(3).anticipated_times
54
+ end
38
55
 
39
- assert_equal 'expected between 2 and 4 times', Cardinality.times(2..4).mocha_inspect
40
- assert_equal 'expected between 1 and 3 times', Cardinality.times(1..3).mocha_inspect
56
+ def test_should_describe_cardinality_defined_using_times_with_range
57
+ assert_equal 'expected between 2 and 4 times', Cardinality.times(2..4).anticipated_times
58
+ assert_equal 'expected between 1 and 3 times', Cardinality.times(1..3).anticipated_times
41
59
  end
42
60
 
43
61
  def test_should_need_verifying
@@ -2,7 +2,6 @@ require File.expand_path('../../test_helper', __FILE__)
2
2
 
3
3
  require 'mocha/central'
4
4
  require 'mocha/mock'
5
- require 'method_definer'
6
5
 
7
6
  class CentralTest < Mocha::TestCase
8
7
  include Mocha
@@ -51,7 +51,7 @@ class ClassMethodsTest < Mocha::TestCase
51
51
  end
52
52
 
53
53
  def test_should_use_stubba_class_method_for_class
54
- assert_equal Mocha::ClassMethod, @klass.stubba_method
54
+ assert_equal Mocha::InstanceMethod, @klass.stubba_method
55
55
  end
56
56
 
57
57
  def test_should_use_stubba_class_method_for_any_instance
@@ -3,35 +3,35 @@ require 'mocha/configuration'
3
3
 
4
4
  class ConfigurationTest < Mocha::TestCase
5
5
  def test_allow_temporarily_changes_config_when_given_block
6
- Mocha::Configuration.warn_when(:stubbing_method_unnecessarily)
6
+ Mocha.configure { |c| c.stubbing_method_unnecessarily = :warn }
7
7
  yielded = false
8
- Mocha::Configuration.allow(:stubbing_method_unnecessarily) do
8
+ Mocha::Configuration.override(:stubbing_method_unnecessarily => :allow) do
9
9
  yielded = true
10
- assert Mocha::Configuration.allow?(:stubbing_method_unnecessarily)
10
+ assert_equal :allow, Mocha.configuration.stubbing_method_unnecessarily
11
11
  end
12
12
  assert yielded
13
- assert Mocha::Configuration.warn_when?(:stubbing_method_unnecessarily)
13
+ assert_equal :warn, Mocha.configuration.stubbing_method_unnecessarily
14
14
  end
15
15
 
16
16
  def test_prevent_temporarily_changes_config_when_given_block
17
- Mocha::Configuration.allow(:stubbing_method_unnecessarily)
17
+ Mocha.configure { |c| c.stubbing_method_unnecessarily = :allow }
18
18
  yielded = false
19
- Mocha::Configuration.prevent(:stubbing_method_unnecessarily) do
19
+ Mocha::Configuration.override(:stubbing_method_unnecessarily => :prevent) do
20
20
  yielded = true
21
- assert Mocha::Configuration.prevent?(:stubbing_method_unnecessarily)
21
+ assert_equal :prevent, Mocha.configuration.stubbing_method_unnecessarily
22
22
  end
23
23
  assert yielded
24
- assert Mocha::Configuration.allow?(:stubbing_method_unnecessarily)
24
+ assert_equal :allow, Mocha.configuration.stubbing_method_unnecessarily
25
25
  end
26
26
 
27
27
  def test_warn_when_temporarily_changes_config_when_given_block
28
- Mocha::Configuration.allow(:stubbing_method_unnecessarily)
28
+ Mocha.configure { |c| c.stubbing_method_unnecessarily = :allow }
29
29
  yielded = false
30
- Mocha::Configuration.warn_when(:stubbing_method_unnecessarily) do
30
+ Mocha::Configuration.override(:stubbing_method_unnecessarily => :warn) do
31
31
  yielded = true
32
- assert Mocha::Configuration.warn_when?(:stubbing_method_unnecessarily)
32
+ assert_equal :warn, Mocha.configuration.stubbing_method_unnecessarily
33
33
  end
34
34
  assert yielded
35
- assert Mocha::Configuration.allow?(:stubbing_method_unnecessarily)
35
+ assert_equal :allow, Mocha.configuration.stubbing_method_unnecessarily
36
36
  end
37
37
  end
@@ -1,40 +1,45 @@
1
1
  require File.expand_path('../../test_helper', __FILE__)
2
2
 
3
+ require 'mocha/invocation'
3
4
  require 'mocha/exception_raiser'
4
5
  require 'timeout'
5
6
 
6
7
  class ExceptionRaiserTest < Mocha::TestCase
7
8
  include Mocha
8
9
 
10
+ def new_invocation
11
+ Invocation.new(:irrelevant, :irrelevant)
12
+ end
13
+
9
14
  def test_should_raise_exception_with_specified_class_and_default_message
10
15
  exception_class = Class.new(StandardError)
11
16
  raiser = ExceptionRaiser.new(exception_class, nil)
12
- exception = assert_raises(exception_class) { raiser.evaluate }
17
+ exception = assert_raises(exception_class) { raiser.evaluate(new_invocation) }
13
18
  assert_equal exception_class.to_s, exception.message
14
19
  end
15
20
 
16
21
  def test_should_raise_exception_with_specified_class_and_message
17
22
  exception_class = Class.new(StandardError)
18
23
  raiser = ExceptionRaiser.new(exception_class, 'message')
19
- exception = assert_raises(exception_class) { raiser.evaluate }
24
+ exception = assert_raises(exception_class) { raiser.evaluate(new_invocation) }
20
25
  assert_equal 'message', exception.message
21
26
  end
22
27
 
23
28
  def test_should_raise_exception_instance
24
29
  exception_class = Class.new(StandardError)
25
30
  raiser = ExceptionRaiser.new(exception_class.new('message'), nil)
26
- exception = assert_raises(exception_class) { raiser.evaluate }
31
+ exception = assert_raises(exception_class) { raiser.evaluate(new_invocation) }
27
32
  assert_equal 'message', exception.message
28
33
  end
29
34
 
30
35
  def test_should_raise_interrupt_exception_with_default_message_so_it_works_in_ruby_1_8_6
31
36
  raiser = ExceptionRaiser.new(Interrupt, nil)
32
- assert_raises(Interrupt) { raiser.evaluate }
37
+ assert_raises(Interrupt) { raiser.evaluate(new_invocation) }
33
38
  end
34
39
 
35
40
  def test_should_raise_subclass_of_interrupt_exception_with_default_message_so_it_works_in_ruby_1_8_6
36
41
  exception_class = Class.new(Interrupt)
37
42
  raiser = ExceptionRaiser.new(exception_class, nil)
38
- assert_raises(exception_class) { raiser.evaluate }
43
+ assert_raises(exception_class) { raiser.evaluate(new_invocation) }
39
44
  end
40
45
  end