mocha 0.5.6 → 3.1.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 (191) hide show
  1. checksums.yaml +7 -0
  2. data/.gemtest +0 -0
  3. data/.github/FUNDING.yml +1 -0
  4. data/.rubocop.yml +92 -0
  5. data/.rubocop_todo.yml +39 -0
  6. data/.yardopts +25 -0
  7. data/CONTRIBUTING.md +7 -0
  8. data/COPYING.md +3 -0
  9. data/Gemfile +17 -0
  10. data/{MIT-LICENSE → MIT-LICENSE.md} +2 -2
  11. data/README.md +361 -0
  12. data/RELEASE.md +1247 -0
  13. data/Rakefile +165 -123
  14. data/gemfiles/Gemfile.minitest.latest +8 -0
  15. data/gemfiles/Gemfile.rubocop +9 -0
  16. data/gemfiles/Gemfile.test-unit.latest +8 -0
  17. data/lib/mocha/any_instance_method.rb +12 -26
  18. data/lib/mocha/any_instance_receiver.rb +20 -0
  19. data/lib/mocha/api.rb +213 -0
  20. data/lib/mocha/argument_iterator.rb +17 -0
  21. data/lib/mocha/backtrace_filter.rb +25 -0
  22. data/lib/mocha/block_matchers.rb +33 -0
  23. data/lib/mocha/cardinality.rb +110 -0
  24. data/lib/mocha/central.rb +33 -22
  25. data/lib/mocha/change_state_side_effect.rb +17 -0
  26. data/lib/mocha/class_methods.rb +67 -0
  27. data/lib/mocha/configuration.rb +338 -0
  28. data/lib/mocha/default_name.rb +15 -0
  29. data/lib/mocha/default_receiver.rb +13 -0
  30. data/lib/mocha/deprecation.rb +6 -15
  31. data/lib/mocha/detection/minitest.rb +25 -0
  32. data/lib/mocha/detection/test_unit.rb +30 -0
  33. data/lib/mocha/error_with_filtered_backtrace.rb +15 -0
  34. data/lib/mocha/exception_raiser.rb +11 -10
  35. data/lib/mocha/expectation.rb +562 -171
  36. data/lib/mocha/expectation_error.rb +9 -14
  37. data/lib/mocha/expectation_error_factory.rb +37 -0
  38. data/lib/mocha/expectation_list.rb +30 -14
  39. data/lib/mocha/hooks.rb +55 -0
  40. data/lib/mocha/ignoring_warning.rb +20 -0
  41. data/lib/mocha/impersonating_any_instance_name.rb +13 -0
  42. data/lib/mocha/impersonating_name.rb +13 -0
  43. data/lib/mocha/in_state_ordering_constraint.rb +17 -0
  44. data/lib/mocha/inspect.rb +54 -30
  45. data/lib/mocha/instance_method.rb +17 -4
  46. data/lib/mocha/integration/assertion_counter.rb +15 -0
  47. data/lib/mocha/integration/minitest/adapter.rb +71 -0
  48. data/lib/mocha/integration/minitest.rb +29 -0
  49. data/lib/mocha/integration/monkey_patcher.rb +26 -0
  50. data/lib/mocha/integration/test_unit/adapter.rb +61 -0
  51. data/lib/mocha/integration/test_unit.rb +29 -0
  52. data/lib/mocha/integration.rb +5 -0
  53. data/lib/mocha/invocation.rb +76 -0
  54. data/lib/mocha/logger.rb +26 -0
  55. data/lib/mocha/macos_version.rb +7 -0
  56. data/lib/mocha/method_matcher.rb +8 -10
  57. data/lib/mocha/minitest.rb +7 -0
  58. data/lib/mocha/mock.rb +333 -108
  59. data/lib/mocha/mockery.rb +192 -0
  60. data/lib/mocha/name.rb +13 -0
  61. data/lib/mocha/not_initialized_error.rb +9 -0
  62. data/lib/mocha/object_methods.rb +183 -0
  63. data/lib/mocha/object_receiver.rb +20 -0
  64. data/lib/mocha/parameter_matchers/all_of.rb +38 -28
  65. data/lib/mocha/parameter_matchers/any_of.rb +44 -33
  66. data/lib/mocha/parameter_matchers/any_parameters.rb +33 -26
  67. data/lib/mocha/parameter_matchers/anything.rb +31 -22
  68. data/lib/mocha/parameter_matchers/base_methods.rb +64 -0
  69. data/lib/mocha/parameter_matchers/equals.rb +36 -25
  70. data/lib/mocha/parameter_matchers/equivalent_uri.rb +65 -0
  71. data/lib/mocha/parameter_matchers/has_entries.rb +48 -29
  72. data/lib/mocha/parameter_matchers/has_entry.rb +90 -42
  73. data/lib/mocha/parameter_matchers/has_key.rb +39 -26
  74. data/lib/mocha/parameter_matchers/has_keys.rb +59 -0
  75. data/lib/mocha/parameter_matchers/has_value.rb +39 -26
  76. data/lib/mocha/parameter_matchers/includes.rb +88 -23
  77. data/lib/mocha/parameter_matchers/instance_methods.rb +28 -0
  78. data/lib/mocha/parameter_matchers/instance_of.rb +37 -26
  79. data/lib/mocha/parameter_matchers/is_a.rb +38 -26
  80. data/lib/mocha/parameter_matchers/kind_of.rb +39 -26
  81. data/lib/mocha/parameter_matchers/not.rb +37 -26
  82. data/lib/mocha/parameter_matchers/optionally.rb +52 -17
  83. data/lib/mocha/parameter_matchers/positional_or_keyword_hash.rb +91 -0
  84. data/lib/mocha/parameter_matchers/regexp_matches.rb +37 -25
  85. data/lib/mocha/parameter_matchers/responds_with.rb +82 -0
  86. data/lib/mocha/parameter_matchers/yaml_equivalent.rb +55 -0
  87. data/lib/mocha/parameter_matchers.rb +12 -5
  88. data/lib/mocha/parameters_matcher.rb +28 -19
  89. data/lib/mocha/raised_exception.rb +13 -0
  90. data/lib/mocha/return_values.rb +13 -18
  91. data/lib/mocha/ruby_version.rb +7 -0
  92. data/lib/mocha/sequence.rb +23 -17
  93. data/lib/mocha/single_return_value.rb +8 -18
  94. data/lib/mocha/state_machine.rb +95 -0
  95. data/lib/mocha/stubbed_method.rb +96 -0
  96. data/lib/mocha/stubbing_error.rb +10 -0
  97. data/lib/mocha/test_unit.rb +7 -0
  98. data/lib/mocha/thrower.rb +15 -0
  99. data/lib/mocha/thrown_object.rb +14 -0
  100. data/lib/mocha/version.rb +5 -0
  101. data/lib/mocha/yield_parameters.rb +12 -20
  102. data/lib/mocha.rb +19 -17
  103. data/mocha.gemspec +40 -0
  104. metadata +129 -145
  105. data/COPYING +0 -3
  106. data/README +0 -35
  107. data/RELEASE +0 -188
  108. data/examples/misc.rb +0 -44
  109. data/examples/mocha.rb +0 -26
  110. data/examples/stubba.rb +0 -65
  111. data/lib/mocha/auto_verify.rb +0 -118
  112. data/lib/mocha/class_method.rb +0 -66
  113. data/lib/mocha/infinite_range.rb +0 -25
  114. data/lib/mocha/is_a.rb +0 -9
  115. data/lib/mocha/metaclass.rb +0 -7
  116. data/lib/mocha/missing_expectation.rb +0 -17
  117. data/lib/mocha/multiple_yields.rb +0 -20
  118. data/lib/mocha/no_yields.rb +0 -11
  119. data/lib/mocha/object.rb +0 -110
  120. data/lib/mocha/parameter_matchers/base.rb +0 -15
  121. data/lib/mocha/parameter_matchers/object.rb +0 -9
  122. data/lib/mocha/pretty_parameters.rb +0 -28
  123. data/lib/mocha/setup_and_teardown.rb +0 -23
  124. data/lib/mocha/single_yield.rb +0 -18
  125. data/lib/mocha/standalone.rb +0 -32
  126. data/lib/mocha/stub.rb +0 -18
  127. data/lib/mocha/test_case_adapter.rb +0 -49
  128. data/lib/mocha_standalone.rb +0 -2
  129. data/lib/stubba.rb +0 -2
  130. data/test/acceptance/expected_invocation_count_acceptance_test.rb +0 -187
  131. data/test/acceptance/mocha_acceptance_test.rb +0 -98
  132. data/test/acceptance/mock_with_initializer_block_acceptance_test.rb +0 -44
  133. data/test/acceptance/mocked_methods_dispatch_acceptance_test.rb +0 -71
  134. data/test/acceptance/optional_parameters_acceptance_test.rb +0 -63
  135. data/test/acceptance/parameter_matcher_acceptance_test.rb +0 -117
  136. data/test/acceptance/partial_mocks_acceptance_test.rb +0 -40
  137. data/test/acceptance/sequence_acceptance_test.rb +0 -179
  138. data/test/acceptance/standalone_acceptance_test.rb +0 -131
  139. data/test/acceptance/stubba_acceptance_test.rb +0 -102
  140. data/test/active_record_test_case.rb +0 -36
  141. data/test/deprecation_disabler.rb +0 -15
  142. data/test/execution_point.rb +0 -34
  143. data/test/integration/mocha_test_result_integration_test.rb +0 -105
  144. data/test/integration/stubba_integration_test.rb +0 -89
  145. data/test/integration/stubba_test_result_integration_test.rb +0 -85
  146. data/test/method_definer.rb +0 -18
  147. data/test/test_helper.rb +0 -12
  148. data/test/test_runner.rb +0 -31
  149. data/test/unit/any_instance_method_test.rb +0 -126
  150. data/test/unit/array_inspect_test.rb +0 -16
  151. data/test/unit/auto_verify_test.rb +0 -129
  152. data/test/unit/central_test.rb +0 -124
  153. data/test/unit/class_method_test.rb +0 -200
  154. data/test/unit/date_time_inspect_test.rb +0 -21
  155. data/test/unit/expectation_error_test.rb +0 -24
  156. data/test/unit/expectation_list_test.rb +0 -75
  157. data/test/unit/expectation_raiser_test.rb +0 -28
  158. data/test/unit/expectation_test.rb +0 -483
  159. data/test/unit/hash_inspect_test.rb +0 -16
  160. data/test/unit/infinite_range_test.rb +0 -53
  161. data/test/unit/metaclass_test.rb +0 -22
  162. data/test/unit/method_matcher_test.rb +0 -23
  163. data/test/unit/missing_expectation_test.rb +0 -42
  164. data/test/unit/mock_test.rb +0 -323
  165. data/test/unit/multiple_yields_test.rb +0 -18
  166. data/test/unit/no_yield_test.rb +0 -18
  167. data/test/unit/object_inspect_test.rb +0 -37
  168. data/test/unit/object_test.rb +0 -165
  169. data/test/unit/parameter_matchers/all_of_test.rb +0 -26
  170. data/test/unit/parameter_matchers/any_of_test.rb +0 -26
  171. data/test/unit/parameter_matchers/anything_test.rb +0 -21
  172. data/test/unit/parameter_matchers/has_entries_test.rb +0 -30
  173. data/test/unit/parameter_matchers/has_entry_test.rb +0 -40
  174. data/test/unit/parameter_matchers/has_key_test.rb +0 -25
  175. data/test/unit/parameter_matchers/has_value_test.rb +0 -25
  176. data/test/unit/parameter_matchers/includes_test.rb +0 -25
  177. data/test/unit/parameter_matchers/instance_of_test.rb +0 -25
  178. data/test/unit/parameter_matchers/is_a_test.rb +0 -25
  179. data/test/unit/parameter_matchers/kind_of_test.rb +0 -25
  180. data/test/unit/parameter_matchers/not_test.rb +0 -26
  181. data/test/unit/parameter_matchers/regexp_matches_test.rb +0 -25
  182. data/test/unit/parameter_matchers/stub_matcher.rb +0 -23
  183. data/test/unit/parameters_matcher_test.rb +0 -121
  184. data/test/unit/return_values_test.rb +0 -63
  185. data/test/unit/sequence_test.rb +0 -104
  186. data/test/unit/setup_and_teardown_test.rb +0 -76
  187. data/test/unit/single_return_value_test.rb +0 -33
  188. data/test/unit/single_yield_test.rb +0 -18
  189. data/test/unit/string_inspect_test.rb +0 -11
  190. data/test/unit/stub_test.rb +0 -24
  191. data/test/unit/yield_parameters_test.rb +0 -93
@@ -1,483 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "..", "test_helper")
2
- require 'method_definer'
3
- require 'mocha/expectation'
4
- require 'mocha/sequence'
5
- require 'execution_point'
6
- require 'deprecation_disabler'
7
-
8
- class ExpectationTest < Test::Unit::TestCase
9
-
10
- include Mocha
11
- include DeprecationDisabler
12
-
13
- def new_expectation
14
- Expectation.new(nil, :expected_method)
15
- end
16
-
17
- def test_should_match_calls_to_same_method_with_any_parameters
18
- assert new_expectation.match?(:expected_method, 1, 2, 3)
19
- end
20
-
21
- def test_should_match_calls_to_same_method_with_exactly_zero_parameters
22
- expectation = new_expectation.with()
23
- assert expectation.match?(:expected_method)
24
- end
25
-
26
- def test_should_not_match_calls_to_same_method_with_more_than_zero_parameters
27
- expectation = new_expectation.with()
28
- assert !expectation.match?(:expected_method, 1, 2, 3)
29
- end
30
-
31
- def test_should_match_calls_to_same_method_with_expected_parameter_values
32
- expectation = new_expectation.with(1, 2, 3)
33
- assert expectation.match?(:expected_method, 1, 2, 3)
34
- end
35
-
36
- def test_should_match_calls_to_same_method_with_parameters_constrained_as_expected
37
- expectation = new_expectation.with() {|x, y, z| x + y == z}
38
- assert expectation.match?(:expected_method, 1, 2, 3)
39
- end
40
-
41
- def test_should_not_match_calls_to_different_method_with_parameters_constrained_as_expected
42
- expectation = new_expectation.with() {|x, y, z| x + y == z}
43
- assert !expectation.match?(:different_method, 1, 2, 3)
44
- end
45
-
46
- def test_should_not_match_calls_to_different_methods_with_no_parameters
47
- assert !new_expectation.match?(:unexpected_method)
48
- end
49
-
50
- def test_should_not_match_calls_to_same_method_with_too_few_parameters
51
- expectation = new_expectation.with(1, 2, 3)
52
- assert !expectation.match?(:unexpected_method, 1, 2)
53
- end
54
-
55
- def test_should_not_match_calls_to_same_method_with_too_many_parameters
56
- expectation = new_expectation.with(1, 2)
57
- assert !expectation.match?(:unexpected_method, 1, 2, 3)
58
- end
59
-
60
- def test_should_not_match_calls_to_same_method_with_unexpected_parameter_values
61
- expectation = new_expectation.with(1, 2, 3)
62
- assert !expectation.match?(:unexpected_method, 1, 0, 3)
63
- end
64
-
65
- def test_should_not_match_calls_to_same_method_with_parameters_not_constrained_as_expected
66
- expectation = new_expectation.with() {|x, y, z| x + y == z}
67
- assert !expectation.match?(:expected_method, 1, 0, 3)
68
- end
69
-
70
- def test_should_allow_invocations_until_expected_invocation_count_is_one_and_actual_invocation_count_would_be_two
71
- expectation = new_expectation.times(1)
72
- assert expectation.invocations_allowed?
73
- expectation.invoke
74
- assert !expectation.invocations_allowed?
75
- end
76
-
77
- def test_should_allow_invocations_until_expected_invocation_count_is_two_and_actual_invocation_count_would_be_three
78
- expectation = new_expectation.times(2)
79
- assert expectation.invocations_allowed?
80
- expectation.invoke
81
- assert expectation.invocations_allowed?
82
- expectation.invoke
83
- assert !expectation.invocations_allowed?
84
- end
85
-
86
- def test_should_allow_invocations_until_expected_invocation_count_is_a_range_from_two_to_three_and_actual_invocation_count_would_be_four
87
- expectation = new_expectation.times(2..3)
88
- assert expectation.invocations_allowed?
89
- expectation.invoke
90
- assert expectation.invocations_allowed?
91
- expectation.invoke
92
- assert expectation.invocations_allowed?
93
- expectation.invoke
94
- assert !expectation.invocations_allowed?
95
- end
96
-
97
- def test_should_store_provided_backtrace
98
- backtrace = Object.new
99
- expectation = Expectation.new(nil, :expected_method, backtrace)
100
- assert_equal backtrace, expectation.backtrace
101
- end
102
-
103
- def test_should_default_backtrace_to_caller
104
- execution_point = ExecutionPoint.current; expectation = Expectation.new(nil, :expected_method)
105
- assert_equal execution_point, ExecutionPoint.new(expectation.backtrace)
106
- end
107
-
108
- def test_should_not_yield
109
- yielded = false
110
- new_expectation.invoke() { yielded = true }
111
- assert_equal false, yielded
112
- end
113
-
114
- def test_should_yield_no_parameters
115
- expectation = new_expectation().yields()
116
- yielded_parameters = nil
117
- expectation.invoke() { |*parameters| yielded_parameters = parameters }
118
- assert_equal Array.new, yielded_parameters
119
- end
120
-
121
- def test_should_yield_with_specified_parameters
122
- expectation = new_expectation().yields(1, 2, 3)
123
- yielded_parameters = nil
124
- expectation.invoke() { |*parameters| yielded_parameters = parameters }
125
- assert_equal [1, 2, 3], yielded_parameters
126
- end
127
-
128
- def test_should_yield_different_parameters_on_consecutive_invocations
129
- expectation = new_expectation().yields(1, 2, 3).yields(4, 5)
130
- yielded_parameters = []
131
- expectation.invoke() { |*parameters| yielded_parameters << parameters }
132
- expectation.invoke() { |*parameters| yielded_parameters << parameters }
133
- assert_equal [[1, 2, 3], [4, 5]], yielded_parameters
134
- end
135
-
136
- def test_should_yield_multiple_times_for_single_invocation
137
- expectation = new_expectation().multiple_yields([1, 2, 3], [4, 5])
138
- yielded_parameters = []
139
- expectation.invoke() { |*parameters| yielded_parameters << parameters }
140
- assert_equal [[1, 2, 3], [4, 5]], yielded_parameters
141
- end
142
-
143
- def test_should_yield_multiple_times_for_first_invocation_and_once_for_second_invocation
144
- expectation = new_expectation().multiple_yields([1, 2, 3], [4, 5]).then.yields(6, 7)
145
- yielded_parameters = []
146
- expectation.invoke() { |*parameters| yielded_parameters << parameters }
147
- expectation.invoke() { |*parameters| yielded_parameters << parameters }
148
- assert_equal [[1, 2, 3], [4, 5], [6, 7]], yielded_parameters
149
- end
150
-
151
- def test_should_return_specified_value
152
- expectation = new_expectation.returns(99)
153
- assert_equal 99, expectation.invoke
154
- end
155
-
156
- def test_should_return_same_specified_value_multiple_times
157
- expectation = new_expectation.returns(99)
158
- assert_equal 99, expectation.invoke
159
- assert_equal 99, expectation.invoke
160
- end
161
-
162
- def test_should_return_specified_values_on_consecutive_calls
163
- expectation = new_expectation.returns(99, 100, 101)
164
- assert_equal 99, expectation.invoke
165
- assert_equal 100, expectation.invoke
166
- assert_equal 101, expectation.invoke
167
- end
168
-
169
- def test_should_return_specified_values_on_consecutive_calls_even_if_values_are_modified
170
- values = [99, 100, 101]
171
- expectation = new_expectation.returns(*values)
172
- values.shift
173
- assert_equal 99, expectation.invoke
174
- assert_equal 100, expectation.invoke
175
- assert_equal 101, expectation.invoke
176
- end
177
-
178
- def test_should_return_nil_by_default
179
- assert_nil new_expectation.invoke
180
- end
181
-
182
- def test_should_return_nil_if_no_value_specified
183
- expectation = new_expectation.returns()
184
- assert_nil expectation.invoke
185
- end
186
-
187
- def test_should_return_evaluated_proc
188
- proc = lambda { 99 }
189
- expectation = new_expectation.returns(proc)
190
- result = nil
191
- disable_deprecations { result = expectation.invoke }
192
- assert_equal 99, result
193
- end
194
-
195
- def test_should_return_evaluated_proc_without_using_is_a_method
196
- proc = lambda { 99 }
197
- proc.define_instance_accessor(:called)
198
- proc.called = false
199
- proc.replace_instance_method(:is_a?) { self.called = true; true}
200
- expectation = new_expectation.returns(proc)
201
- disable_deprecations { expectation.invoke }
202
- assert_equal false, proc.called
203
- end
204
-
205
- def test_should_raise_runtime_exception
206
- expectation = new_expectation.raises
207
- assert_raise(RuntimeError) { expectation.invoke }
208
- end
209
-
210
- def test_should_raise_custom_exception
211
- exception = Class.new(Exception)
212
- expectation = new_expectation.raises(exception)
213
- assert_raise(exception) { expectation.invoke }
214
- end
215
-
216
- def test_should_raise_same_instance_of_custom_exception
217
- exception_klass = Class.new(StandardError)
218
- expected_exception = exception_klass.new
219
- expectation = new_expectation.raises(expected_exception)
220
- actual_exception = assert_raise(exception_klass) { expectation.invoke }
221
- assert_same expected_exception, actual_exception
222
- end
223
-
224
- def test_should_use_the_default_exception_message
225
- expectation = new_expectation.raises(Exception)
226
- exception = assert_raise(Exception) { expectation.invoke }
227
- assert_equal Exception.new.message, exception.message
228
- end
229
-
230
- def test_should_raise_custom_exception_with_message
231
- exception_msg = "exception message"
232
- expectation = new_expectation.raises(Exception, exception_msg)
233
- exception = assert_raise(Exception) { expectation.invoke }
234
- assert_equal exception_msg, exception.message
235
- end
236
-
237
- def test_should_return_values_then_raise_exception
238
- expectation = new_expectation.returns(1, 2).then.raises()
239
- assert_equal 1, expectation.invoke
240
- assert_equal 2, expectation.invoke
241
- assert_raise(RuntimeError) { expectation.invoke }
242
- end
243
-
244
- def test_should_raise_exception_then_return_values
245
- expectation = new_expectation.raises().then.returns(1, 2)
246
- assert_raise(RuntimeError) { expectation.invoke }
247
- assert_equal 1, expectation.invoke
248
- assert_equal 2, expectation.invoke
249
- end
250
-
251
- def test_should_not_raise_error_on_verify_if_expected_call_was_made
252
- expectation = new_expectation
253
- expectation.invoke
254
- assert_nothing_raised(ExpectationError) {
255
- expectation.verify
256
- }
257
- end
258
-
259
- def test_should_raise_error_on_verify_if_call_expected_once_but_invoked_twice
260
- expectation = new_expectation.once
261
- expectation.invoke
262
- expectation.invoke
263
- assert_raises(ExpectationError) {
264
- expectation.verify
265
- }
266
- end
267
-
268
- def test_should_raise_error_on_verify_if_call_expected_once_but_not_invoked
269
- expectation = new_expectation.once
270
- assert_raises(ExpectationError) {
271
- expectation.verify
272
- }
273
- end
274
-
275
- def test_should_not_raise_error_on_verify_if_call_expected_once_and_invoked_once
276
- expectation = new_expectation.once
277
- expectation.invoke
278
- assert_nothing_raised(ExpectationError) {
279
- expectation.verify
280
- }
281
- end
282
-
283
- def test_should_not_raise_error_on_verify_if_expected_call_was_made_at_least_once
284
- expectation = new_expectation.at_least_once
285
- 3.times {expectation.invoke}
286
- assert_nothing_raised(ExpectationError) {
287
- expectation.verify
288
- }
289
- end
290
-
291
- def test_should_raise_error_on_verify_if_expected_call_was_not_made_at_least_once
292
- expectation = new_expectation.with(1, 2, 3).at_least_once
293
- e = assert_raise(ExpectationError) {
294
- expectation.verify
295
- }
296
- assert_match(/expected calls: at least 1, actual calls: 0/i, e.message)
297
- end
298
-
299
- def test_should_not_raise_error_on_verify_if_expected_call_was_made_expected_number_of_times
300
- expectation = new_expectation.times(2)
301
- 2.times {expectation.invoke}
302
- assert_nothing_raised(ExpectationError) {
303
- expectation.verify
304
- }
305
- end
306
-
307
- def test_should_expect_call_not_to_be_made
308
- expectation = new_expectation
309
- expectation.define_instance_accessor(:how_many_times)
310
- expectation.replace_instance_method(:times) { |how_many_times| self.how_many_times = how_many_times }
311
- expectation.never
312
- assert_equal 0, expectation.how_many_times
313
- end
314
-
315
- def test_should_raise_error_on_verify_if_expected_call_was_made_too_few_times
316
- expectation = new_expectation.times(2)
317
- 1.times {expectation.invoke}
318
- e = assert_raise(ExpectationError) {
319
- expectation.verify
320
- }
321
- assert_match(/expected calls: 2, actual calls: 1/i, e.message)
322
- end
323
-
324
- def test_should_raise_error_on_verify_if_expected_call_was_made_too_many_times
325
- expectation = new_expectation.times(2)
326
- 3.times {expectation.invoke}
327
- assert_raise(ExpectationError) {
328
- expectation.verify
329
- }
330
- end
331
-
332
- def test_should_yield_self_to_block
333
- expectation = new_expectation
334
- expectation.invoke
335
- yielded_expectation = nil
336
- expectation.verify { |x| yielded_expectation = x }
337
- assert_equal expectation, yielded_expectation
338
- end
339
-
340
- def test_should_yield_to_block_before_raising_exception
341
- yielded = false
342
- assert_raise(ExpectationError) {
343
- new_expectation.verify { |x| yielded = true }
344
- }
345
- assert yielded
346
- end
347
-
348
- def test_should_store_backtrace_from_point_where_expectation_was_created
349
- execution_point = ExecutionPoint.current; expectation = Expectation.new(nil, :expected_method)
350
- assert_equal execution_point, ExecutionPoint.new(expectation.backtrace)
351
- end
352
-
353
- def test_should_set_backtrace_on_assertion_failed_error_to_point_where_expectation_was_created
354
- execution_point = ExecutionPoint.current; expectation = Expectation.new(nil, :expected_method)
355
- error = assert_raise(ExpectationError) {
356
- expectation.verify
357
- }
358
- assert_equal execution_point, ExecutionPoint.new(error.backtrace)
359
- end
360
-
361
- def test_should_display_expectation_in_exception_message
362
- options = [:a, :b, {:c => 1, :d => 2}]
363
- expectation = new_expectation.with(*options)
364
- exception = assert_raise(ExpectationError) { expectation.verify }
365
- assert exception.message.include?(expectation.method_signature)
366
- end
367
-
368
- class FakeMock
369
-
370
- def initialize(name)
371
- @name = name
372
- end
373
-
374
- def mocha_inspect
375
- @name
376
- end
377
-
378
- end
379
-
380
- def test_should_raise_error_with_message_indicating_which_method_was_expected_to_be_called_on_which_mock_object_with_which_parameters_and_in_what_sequences
381
- mock = FakeMock.new('mock')
382
- sequence_one = Sequence.new('one')
383
- sequence_two = Sequence.new('two')
384
- expectation = Expectation.new(mock, :expected_method).with(1, 2, {'a' => true, :b => false}, [1, 2, 3]).in_sequence(sequence_one, sequence_two)
385
- e = assert_raise(ExpectationError) { expectation.verify }
386
- assert_match "mock.expected_method(1, 2, {'a' => true, :b => false}, [1, 2, 3]); in sequence 'one'; in sequence 'two'", e.message
387
- end
388
-
389
- class FakeConstraint
390
-
391
- def initialize(allows_invocation_now)
392
- @allows_invocation_now = allows_invocation_now
393
- end
394
-
395
- def allows_invocation_now?
396
- @allows_invocation_now
397
- end
398
-
399
- end
400
-
401
- def test_should_be_in_correct_order_if_all_ordering_constraints_allow_invocation_now
402
- constraint_one = FakeConstraint.new(allows_invocation_now = true)
403
- constraint_two = FakeConstraint.new(allows_invocation_now = true)
404
- expectation = Expectation.new(nil, :method_one)
405
- expectation.add_ordering_constraint(constraint_one)
406
- expectation.add_ordering_constraint(constraint_two)
407
- assert expectation.in_correct_order?
408
- end
409
-
410
- def test_should_not_be_in_correct_order_if_one_ordering_constraint_does_not_allow_invocation_now
411
- constraint_one = FakeConstraint.new(allows_invocation_now = true)
412
- constraint_two = FakeConstraint.new(allows_invocation_now = false)
413
- expectation = Expectation.new(nil, :method_one)
414
- expectation.add_ordering_constraint(constraint_one)
415
- expectation.add_ordering_constraint(constraint_two)
416
- assert !expectation.in_correct_order?
417
- end
418
-
419
- def test_should_match_if_all_ordering_constraints_allow_invocation_now
420
- constraint_one = FakeConstraint.new(allows_invocation_now = true)
421
- constraint_two = FakeConstraint.new(allows_invocation_now = true)
422
- expectation = Expectation.new(nil, :method_one)
423
- expectation.add_ordering_constraint(constraint_one)
424
- expectation.add_ordering_constraint(constraint_two)
425
- assert expectation.match?(:method_one)
426
- end
427
-
428
- def test_should_not_match_if_one_ordering_constraints_does_not_allow_invocation_now
429
- constraint_one = FakeConstraint.new(allows_invocation_now = true)
430
- constraint_two = FakeConstraint.new(allows_invocation_now = false)
431
- expectation = Expectation.new(nil, :method_one)
432
- expectation.add_ordering_constraint(constraint_one)
433
- expectation.add_ordering_constraint(constraint_two)
434
- assert !expectation.match?(:method_one)
435
- end
436
-
437
- def test_should_not_be_satisfied_when_required_invocation_has_not_been_made
438
- expectation = Expectation.new(nil, :method_one).times(1)
439
- assert !expectation.satisfied?
440
- end
441
-
442
- def test_should_be_satisfied_when_required_invocation_has_been_made
443
- expectation = Expectation.new(nil, :method_one).times(1)
444
- expectation.invoke
445
- assert expectation.satisfied?
446
- end
447
-
448
- def test_should_not_be_satisfied_when_minimum_number_of_invocations_has_not_been_made
449
- expectation = Expectation.new(nil, :method_one).at_least(2)
450
- expectation.invoke
451
- assert !expectation.satisfied?
452
- end
453
-
454
- def test_should_be_satisfied_when_minimum_number_of_invocations_has_been_made
455
- expectation = Expectation.new(nil, :method_one).at_least(2)
456
- 2.times { expectation.invoke }
457
- assert expectation.satisfied?
458
- end
459
-
460
- class FakeSequence
461
-
462
- attr_reader :expectations
463
-
464
- def initialize
465
- @expectations = []
466
- end
467
-
468
- def constrain_as_next_in_sequence(expectation)
469
- @expectations << expectation
470
- end
471
-
472
- end
473
-
474
- def test_should_tell_sequences_to_constrain_expectation_as_next_in_sequence
475
- sequence_one = FakeSequence.new
476
- sequence_two = FakeSequence.new
477
- expectation = Expectation.new(nil, :method_one)
478
- assert_equal expectation, expectation.in_sequence(sequence_one, sequence_two)
479
- assert_equal [expectation], sequence_one.expectations
480
- assert_equal [expectation], sequence_two.expectations
481
- end
482
-
483
- end
@@ -1,16 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "..", "test_helper")
2
- require 'mocha/inspect'
3
-
4
- class HashInspectTest < Test::Unit::TestCase
5
-
6
- def test_should_keep_spacing_between_key_value
7
- hash = {:a => true}
8
- assert_equal '{:a => true}', hash.mocha_inspect
9
- end
10
-
11
- def test_should_use_mocha_inspect_on_each_item
12
- hash = {:a => 'mocha'}
13
- assert_equal "{:a => 'mocha'}", hash.mocha_inspect
14
- end
15
-
16
- end
@@ -1,53 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "..", "test_helper")
2
- require 'mocha/infinite_range'
3
- require 'date'
4
-
5
- class InfiniteRangeTest < Test::Unit::TestCase
6
-
7
- def test_should_include_values_at_or_above_minimum
8
- range = Range.at_least(10)
9
- assert(range === 10)
10
- assert(range === 11)
11
- assert(range === 1000000)
12
- end
13
-
14
- def test_should_not_include_values_below_minimum
15
- range = Range.at_least(10)
16
- assert_false(range === 0)
17
- assert_false(range === 9)
18
- assert_false(range === -11)
19
- end
20
-
21
- def test_should_be_human_readable_description_for_at_least
22
- assert_equal "at least 10", Range.at_least(10).mocha_inspect
23
- end
24
-
25
- def test_should_include_values_at_or_below_maximum
26
- range = Range.at_most(10)
27
- assert(range === 10)
28
- assert(range === 0)
29
- assert(range === -1000000)
30
- end
31
-
32
- def test_should_not_include_values_above_maximum
33
- range = Range.at_most(10)
34
- assert_false(range === 11)
35
- assert_false(range === 1000000)
36
- end
37
-
38
- def test_should_be_human_readable_description_for_at_most
39
- assert_equal "at most 10", Range.at_most(10).mocha_inspect
40
- end
41
-
42
- def test_should_be_same_as_standard_to_string
43
- assert_equal((1..10).to_s, (1..10).mocha_inspect)
44
- assert_equal((1...10).to_s, (1...10).mocha_inspect)
45
- date_range = Range.new(Date.parse('2006-01-01'), Date.parse('2007-01-01'))
46
- assert_equal date_range.to_s, date_range.mocha_inspect
47
- end
48
-
49
- def assert_false(condition)
50
- assert(!condition)
51
- end
52
-
53
- end
@@ -1,22 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "..", "test_helper")
2
- require 'mocha/metaclass'
3
-
4
- class MetaclassTest < Test::Unit::TestCase
5
-
6
- def test_should_return_objects_singleton_class
7
- object = Object.new
8
- assert_raises(NoMethodError) { object.success? }
9
-
10
- object = Object.new
11
- assert object.__metaclass__.ancestors.include?(Object)
12
- assert object.__metaclass__.ancestors.include?(Kernel)
13
- assert object.__metaclass__.is_a?(Class)
14
-
15
- object.__metaclass__.class_eval { def success?; true; end }
16
- assert object.success?
17
-
18
- object = Object.new
19
- assert_raises(NoMethodError) { object.success? }
20
- end
21
-
22
- end
@@ -1,23 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "..", "test_helper")
2
- require 'mocha/method_matcher'
3
-
4
- class MethodMatcherTest < Test::Unit::TestCase
5
-
6
- include Mocha
7
-
8
- def test_should_match_if_actual_method_name_is_same_as_expected_method_name
9
- method_matcher = MethodMatcher.new(:method_name)
10
- assert method_matcher.match?(:method_name)
11
- end
12
-
13
- def test_should_not_match_if_actual_method_name_is_not_same_as_expected_method_name
14
- method_matcher = MethodMatcher.new(:method_name)
15
- assert !method_matcher.match?(:different_method_name)
16
- end
17
-
18
- def test_should_describe_what_method_is_expected
19
- method_matcher = MethodMatcher.new(:method_name)
20
- assert_equal "method_name", method_matcher.mocha_inspect
21
- end
22
-
23
- end
@@ -1,42 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "..", "test_helper")
2
-
3
- require 'mocha/missing_expectation'
4
- require 'mocha/mock'
5
-
6
- class MissingExpectationTest < Test::Unit::TestCase
7
-
8
- include Mocha
9
-
10
- def test_should_report_similar_expectations
11
- mock = Mock.new
12
- expectation_1 = mock.expects(:method_one).with(1)
13
- expectation_2 = mock.expects(:method_one).with(1, 1)
14
- expectation_3 = mock.expects(:method_two).with(2)
15
-
16
- missing_expectation = MissingExpectation.new(mock, :method_one)
17
- exception = assert_raise(ExpectationError) { missing_expectation.verify }
18
-
19
- expected_message = [
20
- "#{missing_expectation.error_message(0, 1)}",
21
- "Similar expectations:",
22
- "#{expectation_1.method_signature}",
23
- "#{expectation_2.method_signature}"
24
- ].join("\n")
25
-
26
- assert_equal expected_message, exception.message
27
- end
28
-
29
- def test_should_not_report_similar_expectations_if_there_are_none
30
- mock = Mock.new
31
- mock.expects(:method_two).with(2)
32
- mock.expects(:method_two).with(2, 2)
33
-
34
- missing_expectation = MissingExpectation.new(mock, :method_one)
35
- exception = assert_raise(ExpectationError) { missing_expectation.verify }
36
-
37
- expected_message = "#{missing_expectation.error_message(0, 1)}"
38
-
39
- assert_equal expected_message, exception.message
40
- end
41
-
42
- end