mocha 1.11.2 → 1.12.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/CONTRIBUTING.md +1 -1
  3. data/README.md +28 -30
  4. data/RELEASE.md +25 -0
  5. data/Rakefile +6 -1
  6. data/lib/mocha/cardinality.rb +30 -27
  7. data/lib/mocha/expectation.rb +24 -24
  8. data/lib/mocha/mock.rb +1 -1
  9. data/lib/mocha/mockery.rb +9 -22
  10. data/lib/mocha/state_machine.rb +30 -38
  11. data/lib/mocha/version.rb +1 -1
  12. data/mocha.gemspec +3 -1
  13. metadata +5 -192
  14. data/docs/CNAME +0 -1
  15. data/docs/Mocha.html +0 -254
  16. data/docs/Mocha/API.html +0 -1285
  17. data/docs/Mocha/ClassMethods.html +0 -264
  18. data/docs/Mocha/Configuration.html +0 -1525
  19. data/docs/Mocha/Expectation.html +0 -2862
  20. data/docs/Mocha/ExpectationError.html +0 -152
  21. data/docs/Mocha/ExpectationErrorFactory.html +0 -260
  22. data/docs/Mocha/Hooks.html +0 -370
  23. data/docs/Mocha/Integration.html +0 -125
  24. data/docs/Mocha/Integration/MiniTest.html +0 -123
  25. data/docs/Mocha/Integration/MiniTest/Adapter.html +0 -164
  26. data/docs/Mocha/Integration/TestUnit.html +0 -123
  27. data/docs/Mocha/Integration/TestUnit/Adapter.html +0 -164
  28. data/docs/Mocha/Mock.html +0 -1210
  29. data/docs/Mocha/ObjectMethods.html +0 -765
  30. data/docs/Mocha/ParameterMatchers.html +0 -2949
  31. data/docs/Mocha/ParameterMatchers/AllOf.html +0 -153
  32. data/docs/Mocha/ParameterMatchers/AnyOf.html +0 -153
  33. data/docs/Mocha/ParameterMatchers/AnyParameters.html +0 -153
  34. data/docs/Mocha/ParameterMatchers/Anything.html +0 -153
  35. data/docs/Mocha/ParameterMatchers/Base.html +0 -441
  36. data/docs/Mocha/ParameterMatchers/Equals.html +0 -153
  37. data/docs/Mocha/ParameterMatchers/EquivalentUri.html +0 -153
  38. data/docs/Mocha/ParameterMatchers/HasEntries.html +0 -153
  39. data/docs/Mocha/ParameterMatchers/HasEntry.html +0 -153
  40. data/docs/Mocha/ParameterMatchers/HasKey.html +0 -153
  41. data/docs/Mocha/ParameterMatchers/HasValue.html +0 -153
  42. data/docs/Mocha/ParameterMatchers/Includes.html +0 -153
  43. data/docs/Mocha/ParameterMatchers/InstanceOf.html +0 -153
  44. data/docs/Mocha/ParameterMatchers/IsA.html +0 -153
  45. data/docs/Mocha/ParameterMatchers/KindOf.html +0 -153
  46. data/docs/Mocha/ParameterMatchers/Not.html +0 -153
  47. data/docs/Mocha/ParameterMatchers/Optionally.html +0 -153
  48. data/docs/Mocha/ParameterMatchers/RegexpMatches.html +0 -153
  49. data/docs/Mocha/ParameterMatchers/RespondsWith.html +0 -153
  50. data/docs/Mocha/ParameterMatchers/YamlEquivalent.html +0 -153
  51. data/docs/Mocha/Sequence.html +0 -149
  52. data/docs/Mocha/StateMachine.html +0 -527
  53. data/docs/Mocha/StateMachine/State.html +0 -140
  54. data/docs/Mocha/StateMachine/StatePredicate.html +0 -140
  55. data/docs/Mocha/StubbingError.html +0 -150
  56. data/docs/_index.html +0 -519
  57. data/docs/class_list.html +0 -51
  58. data/docs/css/common.css +0 -1
  59. data/docs/css/full_list.css +0 -58
  60. data/docs/css/style.css +0 -496
  61. data/docs/file.COPYING.html +0 -81
  62. data/docs/file.MIT-LICENSE.html +0 -85
  63. data/docs/file.README.html +0 -448
  64. data/docs/file.RELEASE.html +0 -1028
  65. data/docs/file_list.html +0 -71
  66. data/docs/frames.html +0 -17
  67. data/docs/index.html +0 -448
  68. data/docs/js/app.js +0 -303
  69. data/docs/js/full_list.js +0 -216
  70. data/docs/js/jquery.js +0 -4
  71. data/docs/method_list.html +0 -651
  72. data/docs/top-level-namespace.html +0 -118
  73. data/test/acceptance/acceptance_test_helper.rb +0 -42
  74. data/test/acceptance/bug_18914_test.rb +0 -37
  75. data/test/acceptance/bug_21465_test.rb +0 -31
  76. data/test/acceptance/bug_21563_test.rb +0 -22
  77. data/test/acceptance/display_matching_invocations_alongside_expectations_test.rb +0 -69
  78. data/test/acceptance/exception_rescue_test.rb +0 -53
  79. data/test/acceptance/expectations_on_multiple_methods_test.rb +0 -55
  80. data/test/acceptance/expected_invocation_count_test.rb +0 -229
  81. data/test/acceptance/failure_messages_test.rb +0 -77
  82. data/test/acceptance/issue_272_test.rb +0 -50
  83. data/test/acceptance/issue_457_test.rb +0 -31
  84. data/test/acceptance/issue_65_test.rb +0 -64
  85. data/test/acceptance/issue_70_test.rb +0 -53
  86. data/test/acceptance/mocha_example_test.rb +0 -106
  87. data/test/acceptance/mocha_test_result_test.rb +0 -83
  88. data/test/acceptance/mock_built_with_first_argument_type_being_string_test.rb +0 -98
  89. data/test/acceptance/mock_test.rb +0 -191
  90. data/test/acceptance/mocked_methods_dispatch_test.rb +0 -75
  91. data/test/acceptance/multiple_expectations_failure_message_test.rb +0 -66
  92. data/test/acceptance/multiple_yielding_test.rb +0 -59
  93. data/test/acceptance/optional_parameters_test.rb +0 -67
  94. data/test/acceptance/parameter_matcher_test.rb +0 -299
  95. data/test/acceptance/partial_mocks_test.rb +0 -44
  96. data/test/acceptance/prepend_test.rb +0 -86
  97. data/test/acceptance/prevent_use_of_mocha_outside_test_test.rb +0 -76
  98. data/test/acceptance/raise_exception_test.rb +0 -36
  99. data/test/acceptance/return_value_test.rb +0 -49
  100. data/test/acceptance/sequence_test.rb +0 -189
  101. data/test/acceptance/states_test.rb +0 -71
  102. data/test/acceptance/stub_any_instance_method_defined_on_superclass_test.rb +0 -64
  103. data/test/acceptance/stub_any_instance_method_test.rb +0 -301
  104. data/test/acceptance/stub_class_method_defined_on_active_record_association_proxy_test.rb +0 -105
  105. data/test/acceptance/stub_class_method_defined_on_class_test.rb +0 -78
  106. data/test/acceptance/stub_class_method_defined_on_module_test.rb +0 -73
  107. data/test/acceptance/stub_class_method_defined_on_superclass_test.rb +0 -144
  108. data/test/acceptance/stub_everything_test.rb +0 -53
  109. data/test/acceptance/stub_instance_method_defined_on_active_record_association_proxy_test.rb +0 -91
  110. data/test/acceptance/stub_instance_method_defined_on_class_and_aliased_test.rb +0 -67
  111. data/test/acceptance/stub_instance_method_defined_on_class_test.rb +0 -67
  112. data/test/acceptance/stub_instance_method_defined_on_kernel_module_test.rb +0 -136
  113. data/test/acceptance/stub_instance_method_defined_on_module_test.rb +0 -76
  114. data/test/acceptance/stub_instance_method_defined_on_object_class_test.rb +0 -75
  115. data/test/acceptance/stub_instance_method_defined_on_singleton_class_test.rb +0 -67
  116. data/test/acceptance/stub_instance_method_defined_on_superclass_test.rb +0 -70
  117. data/test/acceptance/stub_method_defined_on_module_and_aliased_test.rb +0 -38
  118. data/test/acceptance/stub_module_method_test.rb +0 -200
  119. data/test/acceptance/stub_test.rb +0 -49
  120. data/test/acceptance/stubba_example_test.rb +0 -103
  121. data/test/acceptance/stubba_test_result_test.rb +0 -71
  122. data/test/acceptance/stubbing_error_backtrace_test.rb +0 -63
  123. data/test/acceptance/stubbing_frozen_object_test.rb +0 -88
  124. data/test/acceptance/stubbing_method_accepting_block_parameter_test.rb +0 -52
  125. data/test/acceptance/stubbing_method_unnecessarily_test.rb +0 -63
  126. data/test/acceptance/stubbing_nil_test.rb +0 -60
  127. data/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +0 -157
  128. data/test/acceptance/stubbing_non_existent_class_method_test.rb +0 -157
  129. data/test/acceptance/stubbing_non_existent_instance_method_test.rb +0 -145
  130. data/test/acceptance/stubbing_non_public_any_instance_method_test.rb +0 -128
  131. data/test/acceptance/stubbing_non_public_class_method_test.rb +0 -163
  132. data/test/acceptance/stubbing_non_public_instance_method_test.rb +0 -141
  133. data/test/acceptance/stubbing_on_non_mock_object_test.rb +0 -70
  134. data/test/acceptance/stubbing_same_class_method_on_parent_and_child_classes_test.rb +0 -33
  135. data/test/acceptance/throw_test.rb +0 -42
  136. data/test/acceptance/unexpected_invocation_test.rb +0 -23
  137. data/test/acceptance/unstubbing_test.rb +0 -194
  138. data/test/acceptance/yielding_test.rb +0 -80
  139. data/test/assertions.rb +0 -8
  140. data/test/deprecation_disabler.rb +0 -15
  141. data/test/execution_point.rb +0 -36
  142. data/test/integration/mini_test_test.rb +0 -8
  143. data/test/integration/shared_tests.rb +0 -178
  144. data/test/integration/test_unit_test.rb +0 -8
  145. data/test/method_definer.rb +0 -16
  146. data/test/mini_test_result.rb +0 -96
  147. data/test/minitest_result.rb +0 -48
  148. data/test/simple_counter.rb +0 -11
  149. data/test/test_helper.rb +0 -58
  150. data/test/test_runner.rb +0 -58
  151. data/test/test_unit_result.rb +0 -22
  152. data/test/unit/any_instance_method_test.rb +0 -175
  153. data/test/unit/array_inspect_test.rb +0 -14
  154. data/test/unit/backtrace_filter_test.rb +0 -17
  155. data/test/unit/cardinality_test.rb +0 -72
  156. data/test/unit/central_test.rb +0 -98
  157. data/test/unit/change_state_side_effect_test.rb +0 -37
  158. data/test/unit/class_methods_test.rb +0 -69
  159. data/test/unit/configuration_test.rb +0 -37
  160. data/test/unit/date_time_inspect_test.rb +0 -19
  161. data/test/unit/exception_raiser_test.rb +0 -45
  162. data/test/unit/expectation_list_test.rb +0 -82
  163. data/test/unit/expectation_test.rb +0 -504
  164. data/test/unit/hash_inspect_test.rb +0 -14
  165. data/test/unit/hooks_test.rb +0 -35
  166. data/test/unit/in_state_ordering_constraint_test.rb +0 -39
  167. data/test/unit/instance_method_test.rb +0 -282
  168. data/test/unit/method_matcher_test.rb +0 -26
  169. data/test/unit/mock_test.rb +0 -372
  170. data/test/unit/mockery_test.rb +0 -171
  171. data/test/unit/module_methods_test.rb +0 -16
  172. data/test/unit/object_inspect_test.rb +0 -60
  173. data/test/unit/object_methods_test.rb +0 -63
  174. data/test/unit/parameter_matchers/all_of_test.rb +0 -24
  175. data/test/unit/parameter_matchers/any_of_test.rb +0 -24
  176. data/test/unit/parameter_matchers/anything_test.rb +0 -19
  177. data/test/unit/parameter_matchers/equals_test.rb +0 -23
  178. data/test/unit/parameter_matchers/equivalent_uri_test.rb +0 -41
  179. data/test/unit/parameter_matchers/has_entries_test.rb +0 -51
  180. data/test/unit/parameter_matchers/has_entry_test.rb +0 -128
  181. data/test/unit/parameter_matchers/has_key_test.rb +0 -54
  182. data/test/unit/parameter_matchers/has_value_test.rb +0 -55
  183. data/test/unit/parameter_matchers/includes_test.rb +0 -106
  184. data/test/unit/parameter_matchers/instance_of_test.rb +0 -23
  185. data/test/unit/parameter_matchers/is_a_test.rb +0 -23
  186. data/test/unit/parameter_matchers/kind_of_test.rb +0 -23
  187. data/test/unit/parameter_matchers/not_test.rb +0 -24
  188. data/test/unit/parameter_matchers/regexp_matches_test.rb +0 -45
  189. data/test/unit/parameter_matchers/responds_with_test.rb +0 -38
  190. data/test/unit/parameter_matchers/stub_matcher.rb +0 -23
  191. data/test/unit/parameter_matchers/yaml_equivalent_test.rb +0 -23
  192. data/test/unit/parameters_matcher_test.rb +0 -119
  193. data/test/unit/receivers_test.rb +0 -96
  194. data/test/unit/return_values_test.rb +0 -66
  195. data/test/unit/sequence_test.rb +0 -100
  196. data/test/unit/single_return_value_test.rb +0 -17
  197. data/test/unit/state_machine_test.rb +0 -96
  198. data/test/unit/string_inspect_test.rb +0 -9
  199. data/test/unit/thrower_test.rb +0 -23
  200. data/test/unit/yield_parameters_test.rb +0 -73
@@ -1,14 +0,0 @@
1
- require File.expand_path('../../test_helper', __FILE__)
2
- require 'mocha/inspect'
3
-
4
- class HashInspectTest < Mocha::TestCase
5
- def test_should_keep_spacing_between_key_value
6
- hash = { :a => true }
7
- assert_equal '{:a => true}', hash.mocha_inspect
8
- end
9
-
10
- def test_should_use_mocha_inspect_on_each_item
11
- hash = { :a => 'mocha' }
12
- assert_equal %({:a => "mocha"}), hash.mocha_inspect
13
- end
14
- end
@@ -1,35 +0,0 @@
1
- require File.expand_path('../../test_helper', __FILE__)
2
- require 'mocha/hooks'
3
- require 'mocha/mockery'
4
-
5
- class HooksTest < Mocha::TestCase
6
- # rubocop:disable Style/ClassAndModuleChildren
7
- class Mocha::Mockery
8
- class << self
9
- attr_writer :instances
10
- end
11
- end
12
- # rubocop:enable Style/ClassAndModuleChildren
13
-
14
- class FakeMockery
15
- def verify(*args); end
16
-
17
- def teardown
18
- raise 'exception within Mockery#teardown'
19
- end
20
- end
21
-
22
- def test_ensure_mockery_instance_is_reset_even_when_an_exception_is_raised_in_mockery_teardown
23
- fake_test_case = Object.new.extend(Mocha::Hooks)
24
- mockery = FakeMockery.new
25
- Mocha::Mockery.instances = [mockery]
26
-
27
- begin
28
- fake_test_case.mocha_teardown
29
- rescue StandardError
30
- nil
31
- end
32
-
33
- assert_kind_of Mocha::Mockery::Null, Mocha::Mockery.instance
34
- end
35
- end
@@ -1,39 +0,0 @@
1
- require File.expand_path('../../test_helper', __FILE__)
2
-
3
- require 'mocha/in_state_ordering_constraint'
4
-
5
- class InStateOrderingConstraintTest < Mocha::TestCase
6
- include Mocha
7
-
8
- class FakeStatePredicate
9
- attr_writer :active, :description
10
-
11
- def active?
12
- @active
13
- end
14
-
15
- def mocha_inspect
16
- @description
17
- end
18
- end
19
-
20
- def test_should_allow_invocation_when_state_is_active
21
- state_predicate = FakeStatePredicate.new
22
- ordering_constraint = InStateOrderingConstraint.new(state_predicate)
23
-
24
- state_predicate.active = true
25
- assert ordering_constraint.allows_invocation_now?
26
-
27
- state_predicate.active = false
28
- assert !ordering_constraint.allows_invocation_now?
29
- end
30
-
31
- def test_should_describe_itself_in_terms_of_the_state_predicates_description
32
- state_predicate = FakeStatePredicate.new
33
- ordering_constraint = InStateOrderingConstraint.new(state_predicate)
34
-
35
- state_predicate.description = 'the-state-predicate'
36
-
37
- assert_equal 'when the-state-predicate', ordering_constraint.mocha_inspect
38
- end
39
- end
@@ -1,282 +0,0 @@
1
- require File.expand_path('../../test_helper', __FILE__)
2
- require 'method_definer'
3
- require 'mocha/class_methods'
4
- require 'mocha/mock'
5
- require 'mocha/singleton_class'
6
-
7
- require 'mocha/instance_method'
8
-
9
- class InstanceMethodTest < Mocha::TestCase
10
- include MethodDefiner
11
- include Mocha
12
-
13
- def class_with_method(method, result = nil)
14
- Class.new do
15
- extend ClassMethods
16
- singleton_class.extend(ClassMethods)
17
- singleton_class.send(:define_method, method) { result } if method
18
- end
19
- end
20
-
21
- unless RUBY_V2_PLUS
22
- def test_should_hide_original_method
23
- klass = class_with_method(:method_x)
24
- klass.singleton_class.send(:alias_method, :_method, :method)
25
- method = InstanceMethod.new(klass, :method_x)
26
-
27
- method.hide_original_method
28
-
29
- assert_equal false, klass.respond_to?(:method_x)
30
- end
31
- end
32
-
33
- def test_should_not_raise_error_hiding_method_that_isnt_defined
34
- klass = class_with_method(:irrelevant)
35
- method = InstanceMethod.new(klass, :method_x)
36
-
37
- assert_nothing_raised { method.hide_original_method }
38
- end
39
-
40
- def test_should_not_raise_error_hiding_method_in_class_that_implements_method_called_method
41
- klass = class_with_method(:method)
42
- method = InstanceMethod.new(klass, :method)
43
-
44
- assert_nothing_raised { method.hide_original_method }
45
- end
46
-
47
- def test_should_define_a_new_method_which_should_call_mocha_method_missing
48
- klass = class_with_method(:method_x)
49
- mocha = build_mock
50
- define_instance_method(klass, :mocha) { mocha }
51
- mocha.expects(:method_x).with(:param1, :param2).returns(:result)
52
- method = InstanceMethod.new(klass, :method_x)
53
-
54
- method.hide_original_method
55
- method.define_new_method
56
- result = klass.method_x(:param1, :param2)
57
-
58
- assert_equal :result, result
59
- assert mocha.__verified__?
60
- end
61
-
62
- def test_should_include_the_filename_and_line_number_in_exceptions
63
- klass = class_with_method(:method_x)
64
- mocha = build_mock
65
- define_instance_method(klass, :mocha) { mocha }
66
- mocha.stubs(:method_x).raises(Exception)
67
- method = InstanceMethod.new(klass, :method_x)
68
-
69
- method.hide_original_method
70
- method.define_new_method
71
-
72
- expected_filename = 'stubbed_method.rb'
73
- expected_line_number = 61
74
-
75
- exception = assert_raises(Exception) { klass.method_x }
76
- matching_line = exception.backtrace.find do |line|
77
- filename, line_number, _context = line.split(':')
78
- filename.include?(expected_filename) && line_number.to_i == expected_line_number
79
- end
80
-
81
- assert_not_nil matching_line, "Expected to find #{expected_filename}:#{expected_line_number} in the backtrace:\n #{exception.backtrace.join("\n")}"
82
- end
83
-
84
- def test_should_remove_new_method
85
- klass = class_with_method(:method_x)
86
- method = InstanceMethod.new(klass, :method_x)
87
-
88
- method.remove_new_method
89
-
90
- assert_equal false, klass.respond_to?(:method_x)
91
- end
92
-
93
- def test_should_restore_original_method
94
- klass = class_with_method(:method_x, :original_result)
95
- klass.singleton_class.send(:alias_method, :_method, :method)
96
- method = InstanceMethod.new(klass, :method_x)
97
-
98
- method.hide_original_method
99
- method.define_new_method
100
- method.remove_new_method
101
- method.restore_original_method
102
-
103
- assert klass.respond_to?(:method_x)
104
- assert_equal :original_result, klass.method_x
105
- end
106
-
107
- def test_should_restore_original_method_accepting_a_block_parameter
108
- klass = Class.new do
109
- extend ClassMethods
110
- singleton_class.extend(ClassMethods)
111
- def self.method_x(&block)
112
- block.call if block_given?
113
- end
114
- end
115
- klass.singleton_class.send(:alias_method, :_method, :method)
116
- method = InstanceMethod.new(klass, :method_x)
117
-
118
- method.hide_original_method
119
- method.define_new_method
120
- method.remove_new_method
121
- method.restore_original_method
122
-
123
- block_called = false
124
- klass.method_x { block_called = true }
125
- assert block_called
126
- end
127
-
128
- def test_should_not_restore_original_method_if_none_was_defined_in_first_place
129
- klass = class_with_method(:method_x, :new_result)
130
- method = InstanceMethod.new(klass, :method_x)
131
-
132
- method.restore_original_method
133
-
134
- assert_equal :new_result, klass.method_x
135
- end
136
-
137
- def test_should_call_hide_original_method
138
- klass = class_with_method(:method_x)
139
- method = InstanceMethod.new(klass, :method_x)
140
- method.hide_original_method
141
- define_instance_accessor(method, :hide_called)
142
- replace_instance_method(method, :hide_original_method) { self.hide_called = true }
143
-
144
- method.stub
145
-
146
- assert method.hide_called
147
- end
148
-
149
- def test_should_call_define_new_method
150
- klass = class_with_method(:method_x)
151
- method = InstanceMethod.new(klass, :method_x)
152
- define_instance_accessor(method, :define_called)
153
- replace_instance_method(method, :define_new_method) { self.define_called = true }
154
-
155
- method.stub
156
-
157
- assert method.define_called
158
- end
159
-
160
- def test_should_call_remove_new_method
161
- klass = class_with_method(:method_x)
162
- method = InstanceMethod.new(klass, :method_x)
163
- mocha = build_mock
164
- define_instance_method(klass, :mocha) { mocha }
165
- replace_instance_method(method, :reset_mocha) {}
166
- define_instance_accessor(method, :remove_called)
167
- replace_instance_method(method, :remove_new_method) { self.remove_called = true }
168
-
169
- method.unstub
170
-
171
- assert method.remove_called
172
- end
173
-
174
- def test_should_call_restore_original_method
175
- klass = class_with_method(:method_x)
176
- mocha = build_mock
177
- define_instance_method(klass, :mocha) { mocha }
178
- method = InstanceMethod.new(klass, :method_x)
179
- replace_instance_method(method, :reset_mocha) {}
180
- define_instance_accessor(method, :restore_called)
181
- replace_instance_method(method, :restore_original_method) { self.restore_called = true }
182
-
183
- method.unstub
184
-
185
- assert method.restore_called
186
- end
187
-
188
- def test_should_call_mocha_unstub
189
- klass = class_with_method(:method_x)
190
- method = InstanceMethod.new(klass, :method_x)
191
- replace_instance_method(method, :restore_original_method) {}
192
- mocha = Class.new do
193
- class << self
194
- attr_accessor :unstub_method
195
- end
196
- def self.unstub(method)
197
- self.unstub_method = method
198
- end
199
- end
200
- define_instance_method(mocha, :any_expectations?) { true }
201
- replace_instance_method(method, :mock) { mocha }
202
-
203
- method.unstub
204
- assert_equal mocha.unstub_method, :method_x
205
- end
206
-
207
- def test_should_call_stubbee_reset_mocha_if_no_expectations_remaining
208
- klass = class_with_method(:method_x)
209
- method = InstanceMethod.new(klass, :method_x)
210
- replace_instance_method(method, :remove_new_method) {}
211
- replace_instance_method(method, :restore_original_method) {}
212
- mocha = Class.new
213
- define_instance_method(mocha, :unstub) { |method_name| }
214
- define_instance_method(mocha, :any_expectations?) { false }
215
- replace_instance_method(method, :mock) { mocha }
216
- stubbee = Class.new do
217
- attr_accessor :reset_mocha_called
218
- def reset_mocha
219
- self.reset_mocha_called = true
220
- end
221
- end.new
222
- replace_instance_method(method, :stubbee) { stubbee }
223
-
224
- method.unstub
225
-
226
- assert stubbee.reset_mocha_called
227
- end
228
-
229
- def test_should_return_mock_for_stubbee
230
- mocha = Object.new
231
- stubbee = Object.new
232
- define_instance_method(stubbee, :mocha) { mocha }
233
- method = InstanceMethod.new(stubbee, :method_name)
234
- assert_equal mocha, method.mock
235
- end
236
-
237
- def test_should_not_match_if_other_object_has_a_different_class
238
- method = InstanceMethod.new(Object.new, :method)
239
- other_object = Object.new
240
- assert !method.matches?(other_object)
241
- end
242
-
243
- def test_should_not_match_if_other_instance_method_has_different_stubbee
244
- stubbee1 = Object.new
245
- stubbee2 = Object.new
246
- method1 = InstanceMethod.new(stubbee1, :method)
247
- method2 = InstanceMethod.new(stubbee2, :method)
248
- assert !method1.matches?(method2)
249
- end
250
-
251
- def test_should_not_match_if_other_instance_method_has_different_method
252
- stubbee = Object.new
253
- method1 = InstanceMethod.new(stubbee, :method_1)
254
- method2 = InstanceMethod.new(stubbee, :method_2)
255
- assert !method1.matches?(method2)
256
- end
257
-
258
- def test_should_match_if_other_instance_method_has_same_stubbee_and_same_method_so_no_attempt_is_made_to_stub_a_method_twice
259
- stubbee = Object.new
260
- method1 = InstanceMethod.new(stubbee, :method)
261
- method2 = InstanceMethod.new(stubbee, :method)
262
- assert method1.matches?(method2)
263
- end
264
-
265
- def test_should_match_if_other_instance_method_has_same_stubbee_and_same_method_but_stubbee_equal_method_lies_like_active_record_association_proxy
266
- stubbee = Class.new do
267
- extend ClassMethods
268
- def equal?(_other)
269
- false
270
- end
271
- end.new
272
- method1 = InstanceMethod.new(stubbee, :method)
273
- method2 = InstanceMethod.new(stubbee, :method)
274
- assert method1.matches?(method2)
275
- end
276
-
277
- private
278
-
279
- def build_mock
280
- Mock.new(nil)
281
- end
282
- end
@@ -1,26 +0,0 @@
1
- require File.expand_path('../../test_helper', __FILE__)
2
- require 'mocha/method_matcher'
3
-
4
- class MethodMatcherTest < Mocha::TestCase
5
- include Mocha
6
-
7
- def test_should_match_if_actual_method_name_is_same_as_expected_method_name
8
- method_matcher = MethodMatcher.new(:method_name)
9
- assert method_matcher.match?(:method_name)
10
- end
11
-
12
- def test_should_match_if_actual_method_name_is_expected_method_name_as_string
13
- method_matcher = MethodMatcher.new(:method_name)
14
- assert method_matcher.match?('method_name')
15
- end
16
-
17
- def test_should_not_match_if_actual_method_name_is_not_same_as_expected_method_name
18
- method_matcher = MethodMatcher.new(:method_name)
19
- assert !method_matcher.match?(:different_method_name)
20
- end
21
-
22
- def test_should_describe_what_method_is_expected
23
- method_matcher = MethodMatcher.new(:method_name)
24
- assert_equal 'method_name', method_matcher.mocha_inspect
25
- end
26
- end
@@ -1,372 +0,0 @@
1
- require File.expand_path('../../test_helper', __FILE__)
2
- require 'mocha/ruby_version'
3
- require 'mocha/macos_version'
4
- require 'mocha/mock'
5
- require 'mocha/expectation_error_factory'
6
- require 'set'
7
- require 'simple_counter'
8
-
9
- class MockTest < Mocha::TestCase
10
- include Mocha
11
-
12
- def test_should_set_single_expectation
13
- mock = build_mock
14
- mock.expects(:method1).returns(1)
15
- assert_nothing_raised(ExpectationErrorFactory.exception_class) do
16
- assert_equal 1, mock.method1
17
- end
18
- end
19
-
20
- def test_should_build_and_store_expectations
21
- mock = build_mock
22
- expectation = mock.expects(:method1)
23
- assert_not_nil expectation
24
- assert_equal [expectation], mock.__expectations__.to_a
25
- end
26
-
27
- def test_should_not_stub_everything_by_default
28
- mock = build_mock
29
- assert_equal false, mock.everything_stubbed
30
- end
31
-
32
- def test_should_stub_everything
33
- mock = build_mock
34
- mock.stub_everything
35
- assert_equal true, mock.everything_stubbed
36
- end
37
-
38
- def test_should_be_able_to_extend_mock_object_with_module
39
- mock = build_mock
40
- assert_nothing_raised(ExpectationErrorFactory.exception_class) { mock.extend(Module.new) }
41
- end
42
-
43
- def test_should_be_equal
44
- mock = build_mock
45
- assert_equal true, mock.eql?(mock)
46
- end
47
-
48
- PRE_RUBY_V19_EXCLUDED_METHODS = %w[
49
- method_missing
50
- singleton_method_undefined
51
- initialize
52
- ].freeze
53
-
54
- MACOS_EXCLUDED_METHODS =
55
- MACOS && MACOS_VERSION >= MACOS_MOJAVE_VERSION ? [:syscall] : []
56
-
57
- RUBY_V19_AND_LATER_EXCLUDED_METHODS = [
58
- :object_id,
59
- :method_missing,
60
- :singleton_method_undefined,
61
- :initialize,
62
- :String,
63
- :singleton_method_added,
64
- *MACOS_EXCLUDED_METHODS
65
- ].freeze
66
-
67
- OBJECT_METHODS = STANDARD_OBJECT_PUBLIC_INSTANCE_METHODS.reject do |m|
68
- (m =~ /^__.*__$/) ||
69
- (PRE_RUBY_V19 && PRE_RUBY_V19_EXCLUDED_METHODS.include?(m)) ||
70
- (!PRE_RUBY_V19 && RUBY_V19_AND_LATER_EXCLUDED_METHODS.include?(m))
71
- end
72
-
73
- def test_should_be_able_to_mock_standard_object_methods
74
- mock = build_mock
75
- OBJECT_METHODS.each { |method| mock.__expects__(method.to_sym).returns(method) }
76
- OBJECT_METHODS.each { |method| assert_equal method, mock.__send__(method.to_sym) }
77
- assert mock.__verified__?
78
- end
79
-
80
- def test_should_be_able_to_stub_standard_object_methods
81
- mock = build_mock
82
- OBJECT_METHODS.each { |method| mock.__stubs__(method.to_sym).returns(method) }
83
- OBJECT_METHODS.each { |method| assert_equal method, mock.__send__(method.to_sym) }
84
- end
85
-
86
- def test_should_create_and_add_expectations
87
- mock = build_mock
88
- expectation1 = mock.expects(:method1)
89
- expectation2 = mock.expects(:method2)
90
- assert_equal [expectation1, expectation2].to_set, mock.__expectations__.to_set
91
- end
92
-
93
- def test_should_pass_backtrace_into_expectation
94
- mock = build_mock
95
- backtrace = Object.new
96
- expectation = mock.expects(:method1, backtrace)
97
- assert_equal backtrace, expectation.backtrace
98
- end
99
-
100
- def test_should_pass_backtrace_into_stub
101
- mock = build_mock
102
- backtrace = Object.new
103
- stub = mock.stubs(:method1, backtrace)
104
- assert_equal backtrace, stub.backtrace
105
- end
106
-
107
- def test_should_create_and_add_stubs
108
- mock = build_mock
109
- stub1 = mock.stubs(:method1)
110
- stub2 = mock.stubs(:method2)
111
- assert_equal [stub1, stub2].to_set, mock.__expectations__.to_set
112
- end
113
-
114
- def test_should_invoke_expectation_and_return_result
115
- mock = build_mock
116
- mock.expects(:my_method).returns(:result)
117
- result = mock.my_method
118
- assert_equal :result, result
119
- end
120
-
121
- def test_should_not_raise_error_if_stubbing_everything
122
- mock = build_mock
123
- mock.stub_everything
124
- result = nil
125
- assert_nothing_raised(ExpectationErrorFactory.exception_class) do
126
- result = mock.unexpected_method
127
- end
128
- assert_nil result
129
- end
130
-
131
- def test_should_raise_assertion_error_for_unexpected_method_call
132
- mock = build_mock
133
- error = assert_raises(ExpectationErrorFactory.exception_class) do
134
- mock.unexpected_method_called(:my_method, :argument1, :argument2)
135
- end
136
- assert_match(/unexpected invocation/, error.message)
137
- assert_match(/my_method/, error.message)
138
- assert_match(/argument1/, error.message)
139
- assert_match(/argument2/, error.message)
140
- end
141
-
142
- def test_should_not_verify_successfully_because_not_all_expectations_have_been_satisfied
143
- mock = build_mock
144
- mock.expects(:method1)
145
- mock.expects(:method2)
146
- mock.method1
147
- assert !mock.__verified__?
148
- end
149
-
150
- def test_should_increment_assertion_counter_for_every_verified_expectation
151
- mock = build_mock
152
-
153
- mock.expects(:method1)
154
- mock.method1
155
-
156
- mock.expects(:method2)
157
- mock.method2
158
-
159
- assertion_counter = SimpleCounter.new
160
-
161
- mock.__verified__?(assertion_counter)
162
-
163
- assert_equal 2, assertion_counter.count
164
- end
165
-
166
- def test_should_yield_supplied_parameters_to_block
167
- mock = build_mock
168
- parameters_for_yield = [1, 2, 3]
169
- mock.expects(:method1).yields(*parameters_for_yield)
170
- yielded_parameters = nil
171
- mock.method1 { |*parameters| yielded_parameters = parameters }
172
- assert_equal parameters_for_yield, yielded_parameters
173
- end
174
-
175
- def test_should_set_up_multiple_expectations_with_return_values
176
- mock = build_mock
177
- mock.expects(:method1 => :result1, :method2 => :result2)
178
- assert_equal :result1, mock.method1
179
- assert_equal :result2, mock.method2
180
- end
181
-
182
- def test_should_set_up_multiple_stubs_with_return_values
183
- mock = build_mock
184
- mock.stubs(:method1 => :result1, :method2 => :result2)
185
- assert_equal :result1, mock.method1
186
- assert_equal :result2, mock.method2
187
- end
188
-
189
- def test_should_keep_returning_specified_value_for_stubs
190
- mock = build_mock
191
- mock.stubs(:method1).returns(1)
192
- assert_equal 1, mock.method1
193
- assert_equal 1, mock.method1
194
- end
195
-
196
- def test_should_keep_returning_specified_value_for_expects
197
- mock = build_mock
198
- mock.expects(:method1).times(2).returns(1)
199
- assert_equal 1, mock.method1
200
- assert_equal 1, mock.method1
201
- end
202
-
203
- def test_should_match_most_recent_call_to_expects
204
- mock = build_mock
205
- mock.expects(:method1).returns(0)
206
- mock.expects(:method1).returns(1)
207
- assert_equal 1, mock.method1
208
- end
209
-
210
- def test_should_match_most_recent_call_to_stubs
211
- mock = build_mock
212
- mock.stubs(:method1).returns(0)
213
- mock.stubs(:method1).returns(1)
214
- assert_equal 1, mock.method1
215
- end
216
-
217
- def test_should_match_most_recent_call_to_stubs_or_expects
218
- mock = build_mock
219
- mock.stubs(:method1).returns(0)
220
- mock.expects(:method1).returns(1)
221
- assert_equal 1, mock.method1
222
- end
223
-
224
- def test_should_match_most_recent_call_to_expects_or_stubs
225
- mock = build_mock
226
- mock.expects(:method1).returns(0)
227
- mock.stubs(:method1).returns(1)
228
- assert_equal 1, mock.method1
229
- end
230
-
231
- def test_should_respond_to_expected_method
232
- mock = build_mock
233
- mock.expects(:method1)
234
- assert_equal true, mock.respond_to?(:method1)
235
- end
236
-
237
- def test_should_respond_to_expected_method_as_string
238
- mock = build_mock
239
- mock.expects(:method1)
240
- assert_equal true, mock.respond_to?('method1')
241
- end
242
-
243
- def test_should_not_respond_to_unexpected_method
244
- mock = build_mock
245
- assert_equal false, mock.respond_to?(:method1)
246
- end
247
-
248
- def test_should_respond_to_methods_which_the_responder_does_responds_to
249
- instance = Class.new do
250
- define_method(:respond_to?) { |_symbol| true }
251
- end.new
252
- mock = build_mock
253
- mock.responds_like(instance)
254
- assert_equal true, mock.respond_to?(:invoked_method)
255
- end
256
-
257
- def test_should_not_respond_to_methods_which_the_responder_does_not_responds_to
258
- instance = Class.new do
259
- define_method(:respond_to?) { |_symbol| false }
260
- end.new
261
- mock = build_mock
262
- mock.responds_like(instance)
263
- assert_equal false, mock.respond_to?(:invoked_method)
264
- end
265
-
266
- def test_should_respond_to_methods_which_the_responder_instance_does_responds_to
267
- klass = Class.new do
268
- define_method(:respond_to?) { |_symbol| true }
269
- end
270
- mock = build_mock
271
- mock.responds_like_instance_of(klass)
272
- assert_equal true, mock.respond_to?(:invoked_method)
273
- end
274
-
275
- def test_should_not_respond_to_methods_which_the_responder_instance_does_not_responds_to
276
- klass = Class.new do
277
- define_method(:respond_to?) { |_symbol| false }
278
- end
279
- mock = build_mock
280
- mock.responds_like_instance_of(klass)
281
- assert_equal false, mock.respond_to?(:invoked_method)
282
- end
283
-
284
- def test_respond_like_should_return_itself_to_allow_method_chaining
285
- mock = build_mock
286
- assert_same mock.responds_like(Object.new), mock
287
- end
288
-
289
- def test_respond_like_instance_of_should_return_itself_to_allow_method_chaining
290
- mock = build_mock
291
- assert_same mock.responds_like_instance_of(Object), mock
292
- end
293
-
294
- def test_should_not_raise_no_method_error_if_mock_is_not_restricted_to_respond_like_a_responder
295
- mock = build_mock
296
- mock.stubs(:invoked_method)
297
- assert_nothing_raised(NoMethodError) { mock.invoked_method }
298
- end
299
-
300
- def test_should_not_raise_no_method_error_if_responder_does_respond_to_invoked_method
301
- instance = Class.new do
302
- define_method(:respond_to?) { |_symbol| true }
303
- end.new
304
- mock = build_mock
305
- mock.responds_like(instance)
306
- mock.stubs(:invoked_method)
307
- assert_nothing_raised(NoMethodError) { mock.invoked_method }
308
- end
309
-
310
- def test_should_raise_no_method_error_if_responder_does_not_respond_to_invoked_method
311
- instance = Class.new do
312
- define_method(:respond_to?) { |_symbol| false }
313
- define_method(:mocha_inspect) { 'mocha_inspect' }
314
- end.new
315
- mock = build_mock
316
- mock.responds_like(instance)
317
- mock.stubs(:invoked_method)
318
- assert_raises(NoMethodError) { mock.invoked_method }
319
- end
320
-
321
- def test_should_raise_no_method_error_with_message_indicating_that_mock_is_constrained_to_respond_like_responder
322
- instance = Class.new do
323
- define_method(:respond_to?) { |_symbol| false }
324
- define_method(:mocha_inspect) { 'mocha_inspect' }
325
- end.new
326
- mock = build_mock
327
- mock.responds_like(instance)
328
- mock.stubs(:invoked_method)
329
- begin
330
- mock.invoked_method
331
- rescue NoMethodError => e
332
- assert_match(/which responds like mocha_inspect/, e.message)
333
- end
334
- end
335
-
336
- def test_should_handle_respond_to_with_private_methods_param_without_error
337
- mock = build_mock
338
- assert_nothing_raised { mock.respond_to?(:object_id, false) }
339
- end
340
-
341
- def test_should_respond_to_any_method_if_stubbing_everything
342
- mock = build_mock
343
- mock.stub_everything
344
- assert mock.respond_to?(:abc)
345
- assert mock.respond_to?(:xyz)
346
- end
347
-
348
- def test_should_remove_expectations_for_unstubbed_methods
349
- mock = build_mock
350
- mock.expects(:method1)
351
- mock.expects(:method2)
352
- mock.unstub(:method1, :method2)
353
- e = assert_raises(ExpectationErrorFactory.exception_class) { mock.method1 }
354
- assert_match(/unexpected invocation/, e.message)
355
- e = assert_raises(ExpectationErrorFactory.exception_class) { mock.method2 }
356
- assert_match(/unexpected invocation/, e.message)
357
- end
358
-
359
- unless PRE_RUBY_V19
360
- def test_expectation_is_defined_on_mock
361
- mock = build_mock
362
- mock.expects(:method1)
363
- assert defined? mock.method1
364
- end
365
- end
366
-
367
- private
368
-
369
- def build_mock
370
- Mock.new(nil)
371
- end
372
- end