mocha 1.9.0 → 1.11.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/FUNDING.yml +1 -0
- data/.rubocop.yml +3 -0
- data/.rubocop_todo.yml +7 -30
- data/.yardopts +1 -0
- data/README.md +30 -11
- data/RELEASE.md +93 -0
- data/Rakefile +10 -7
- data/docs/CNAME +1 -0
- data/docs/Mocha.html +132 -5
- data/docs/Mocha/API.html +437 -206
- data/docs/Mocha/ClassMethods.html +13 -16
- data/docs/Mocha/Configuration.html +1126 -227
- data/docs/Mocha/Expectation.html +420 -267
- data/docs/Mocha/ExpectationError.html +5 -10
- data/docs/Mocha/ExpectationErrorFactory.html +9 -18
- data/docs/Mocha/Hooks.html +12 -27
- data/docs/Mocha/Integration.html +3 -3
- data/docs/Mocha/Integration/MiniTest.html +3 -3
- data/docs/Mocha/Integration/MiniTest/Adapter.html +5 -6
- data/docs/Mocha/Integration/TestUnit.html +3 -3
- data/docs/Mocha/Integration/TestUnit/Adapter.html +5 -6
- data/docs/Mocha/Mock.html +202 -162
- data/docs/Mocha/ObjectMethods.html +121 -68
- data/docs/Mocha/ParameterMatchers.html +21 -109
- data/docs/Mocha/ParameterMatchers/AllOf.html +4 -5
- data/docs/Mocha/ParameterMatchers/AnyOf.html +4 -5
- data/docs/Mocha/ParameterMatchers/AnyParameters.html +3 -3
- data/docs/Mocha/ParameterMatchers/Anything.html +3 -3
- data/docs/Mocha/ParameterMatchers/Base.html +8 -15
- data/docs/Mocha/ParameterMatchers/Equals.html +4 -5
- data/docs/Mocha/ParameterMatchers/EquivalentUri.html +3 -3
- data/docs/Mocha/ParameterMatchers/HasEntries.html +4 -5
- data/docs/Mocha/ParameterMatchers/HasEntry.html +4 -5
- data/docs/Mocha/ParameterMatchers/HasKey.html +4 -5
- data/docs/Mocha/ParameterMatchers/HasValue.html +4 -5
- data/docs/Mocha/ParameterMatchers/Includes.html +4 -5
- data/docs/Mocha/ParameterMatchers/InstanceOf.html +4 -5
- data/docs/Mocha/ParameterMatchers/IsA.html +3 -3
- data/docs/Mocha/ParameterMatchers/KindOf.html +4 -5
- data/docs/Mocha/ParameterMatchers/Not.html +4 -5
- data/docs/Mocha/ParameterMatchers/Optionally.html +3 -3
- data/docs/Mocha/ParameterMatchers/RegexpMatches.html +4 -5
- data/docs/Mocha/ParameterMatchers/RespondsWith.html +4 -5
- data/docs/Mocha/ParameterMatchers/YamlEquivalent.html +4 -5
- data/docs/Mocha/Sequence.html +3 -3
- data/docs/Mocha/StateMachine.html +13 -25
- data/docs/Mocha/StateMachine/State.html +4 -5
- data/docs/Mocha/StateMachine/StatePredicate.html +4 -5
- data/docs/Mocha/StubbingError.html +3 -3
- data/docs/_index.html +4 -22
- data/docs/class_list.html +1 -1
- data/docs/file.COPYING.html +3 -3
- data/docs/file.MIT-LICENSE.html +3 -3
- data/docs/file.README.html +36 -14
- data/docs/file.RELEASE.html +126 -3
- data/docs/frames.html +1 -1
- data/docs/index.html +36 -14
- data/docs/method_list.html +117 -37
- data/docs/top-level-namespace.html +3 -3
- data/gemfiles/Gemfile.minitest.5.11.3 +7 -0
- data/init.rb +1 -3
- data/lib/mocha.rb +8 -0
- data/lib/mocha/any_instance_method.rb +9 -25
- data/lib/mocha/api.rb +120 -56
- data/lib/mocha/block_matcher.rb +31 -0
- data/lib/mocha/cardinality.rb +26 -11
- data/lib/mocha/class_methods.rb +17 -15
- data/lib/mocha/configuration.rb +351 -67
- data/lib/mocha/deprecation.rb +2 -1
- data/lib/mocha/detection/test_unit.rb +1 -3
- data/lib/mocha/exception_raiser.rb +2 -1
- data/lib/mocha/expectation.rb +102 -63
- data/lib/mocha/expectation_error.rb +1 -3
- data/lib/mocha/expectation_list.rb +6 -6
- data/lib/mocha/inspect.rb +28 -26
- data/lib/mocha/instance_method.rb +19 -2
- data/lib/mocha/integration.rb +1 -3
- data/lib/mocha/integration/mini_test.rb +7 -0
- data/lib/mocha/integration/test_unit.rb +7 -0
- data/lib/mocha/invocation.rb +77 -0
- data/lib/mocha/macos_version.rb +5 -0
- data/lib/mocha/minitest.rb +6 -1
- data/lib/mocha/mock.rb +46 -31
- data/lib/mocha/mockery.rb +25 -61
- data/lib/mocha/names.rb +1 -1
- data/lib/mocha/object_methods.rb +13 -19
- data/lib/mocha/parameter_matchers.rb +1 -1
- data/lib/mocha/parameter_matchers/all_of.rb +1 -1
- data/lib/mocha/parameter_matchers/any_of.rb +1 -1
- data/lib/mocha/parameter_matchers/equivalent_uri.rb +0 -9
- data/lib/mocha/parameter_matchers/includes.rb +2 -0
- data/lib/mocha/parameter_matchers/instance_methods.rb +18 -0
- data/lib/mocha/raised_exception.rb +11 -0
- data/lib/mocha/return_values.rb +3 -3
- data/lib/mocha/setup.rb +5 -0
- data/lib/mocha/single_return_value.rb +2 -1
- data/lib/mocha/singleton_class.rb +9 -0
- data/lib/mocha/{class_method.rb → stubbed_method.rb} +23 -43
- data/lib/mocha/test_unit.rb +6 -1
- data/lib/mocha/thrower.rb +2 -1
- data/lib/mocha/thrown_object.rb +12 -0
- data/lib/mocha/version.rb +1 -1
- data/lib/mocha/yield_parameters.rb +5 -11
- data/mocha.gemspec +1 -3
- data/test/acceptance/acceptance_test_helper.rb +7 -0
- data/test/acceptance/bug_18914_test.rb +0 -1
- data/test/acceptance/bug_21465_test.rb +0 -1
- data/test/acceptance/bug_21563_test.rb +0 -1
- data/test/acceptance/display_matching_invocations_alongside_expectations_test.rb +69 -0
- data/test/acceptance/exception_rescue_test.rb +1 -2
- data/test/acceptance/expectations_on_multiple_methods_test.rb +0 -1
- data/test/acceptance/expected_invocation_count_test.rb +2 -3
- data/test/acceptance/failure_messages_test.rb +16 -1
- data/test/acceptance/issue_272_test.rb +1 -2
- data/test/acceptance/issue_65_test.rb +0 -1
- data/test/acceptance/issue_70_test.rb +0 -1
- data/test/acceptance/mocha_example_test.rb +0 -1
- data/test/acceptance/mocha_test_result_test.rb +0 -1
- data/test/acceptance/mock_built_with_first_argument_type_being_string_test.rb +98 -0
- data/test/acceptance/mock_test.rb +63 -6
- data/test/acceptance/mocked_methods_dispatch_test.rb +0 -1
- data/test/acceptance/multiple_expectations_failure_message_test.rb +0 -1
- data/test/acceptance/multiple_yielding_test.rb +59 -0
- data/test/acceptance/optional_parameters_test.rb +0 -1
- data/test/acceptance/parameter_matcher_test.rb +0 -1
- data/test/acceptance/partial_mocks_test.rb +0 -1
- data/test/acceptance/prepend_test.rb +0 -1
- data/test/acceptance/prevent_use_of_mocha_outside_test_test.rb +0 -1
- data/test/acceptance/raise_exception_test.rb +0 -1
- data/test/acceptance/return_value_test.rb +0 -1
- data/test/acceptance/sequence_test.rb +0 -1
- data/test/acceptance/states_test.rb +0 -1
- data/test/acceptance/stub_any_instance_method_defined_on_superclass_test.rb +1 -2
- data/test/acceptance/stub_any_instance_method_test.rb +20 -1
- data/test/acceptance/stub_class_method_defined_on_active_record_association_proxy_test.rb +0 -1
- data/test/acceptance/stub_class_method_defined_on_class_test.rb +0 -1
- data/test/acceptance/stub_class_method_defined_on_module_test.rb +0 -1
- data/test/acceptance/stub_class_method_defined_on_superclass_test.rb +1 -2
- data/test/acceptance/stub_everything_test.rb +0 -1
- data/test/acceptance/stub_instance_method_defined_on_active_record_association_proxy_test.rb +0 -1
- data/test/acceptance/stub_instance_method_defined_on_class_and_aliased_test.rb +0 -1
- data/test/acceptance/stub_instance_method_defined_on_class_test.rb +0 -1
- data/test/acceptance/stub_instance_method_defined_on_kernel_module_test.rb +0 -1
- data/test/acceptance/stub_instance_method_defined_on_module_test.rb +0 -1
- data/test/acceptance/stub_instance_method_defined_on_object_class_test.rb +0 -1
- data/test/acceptance/stub_instance_method_defined_on_singleton_class_test.rb +0 -1
- data/test/acceptance/stub_instance_method_defined_on_superclass_test.rb +0 -1
- data/test/acceptance/stub_method_defined_on_module_and_aliased_test.rb +0 -1
- data/test/acceptance/stub_module_method_test.rb +0 -1
- data/test/acceptance/stub_test.rb +0 -1
- data/test/acceptance/stubba_example_test.rb +0 -1
- data/test/acceptance/stubba_test_result_test.rb +0 -1
- data/test/acceptance/stubbing_error_backtrace_test.rb +4 -5
- data/test/acceptance/stubbing_frozen_object_test.rb +0 -1
- data/test/acceptance/stubbing_method_accepting_block_parameter_test.rb +0 -1
- data/test/acceptance/stubbing_method_unnecessarily_test.rb +5 -5
- data/test/acceptance/stubbing_nil_test.rb +5 -5
- data/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +27 -11
- data/test/acceptance/stubbing_non_existent_class_method_test.rb +11 -11
- data/test/acceptance/stubbing_non_existent_instance_method_test.rb +11 -11
- data/test/acceptance/stubbing_non_public_any_instance_method_test.rb +8 -8
- data/test/acceptance/stubbing_non_public_class_method_test.rb +9 -9
- data/test/acceptance/stubbing_non_public_instance_method_test.rb +9 -9
- data/test/acceptance/stubbing_on_non_mock_object_test.rb +5 -5
- data/test/acceptance/stubbing_same_class_method_on_parent_and_child_classes_test.rb +0 -1
- data/test/acceptance/throw_test.rb +0 -1
- data/test/acceptance/unexpected_invocation_test.rb +0 -1
- data/test/acceptance/unstubbing_test.rb +0 -1
- data/test/acceptance/yielding_test.rb +80 -0
- data/test/integration/shared_tests.rb +5 -3
- data/test/method_definer.rb +11 -17
- data/test/test_runner.rb +2 -0
- data/test/unit/any_instance_method_test.rb +41 -40
- data/test/unit/cardinality_test.rb +41 -23
- data/test/unit/central_test.rb +0 -1
- data/test/unit/class_methods_test.rb +1 -1
- data/test/unit/configuration_test.rb +12 -12
- data/test/unit/exception_raiser_test.rb +10 -5
- data/test/unit/expectation_list_test.rb +13 -11
- data/test/unit/expectation_test.rb +115 -103
- data/test/unit/instance_method_test.rb +282 -0
- data/test/unit/mock_test.rb +28 -19
- data/test/unit/mockery_test.rb +8 -11
- data/test/unit/module_methods_test.rb +2 -3
- data/test/unit/object_inspect_test.rb +6 -4
- data/test/unit/object_methods_test.rb +3 -2
- data/test/unit/parameter_matchers/equivalent_uri_test.rb +0 -9
- data/test/unit/parameter_matchers/has_entries_test.rb +1 -1
- data/test/unit/parameter_matchers/has_entry_test.rb +1 -1
- data/test/unit/parameter_matchers/has_key_test.rb +1 -1
- data/test/unit/parameter_matchers/has_value_test.rb +1 -1
- data/test/unit/parameter_matchers/includes_test.rb +1 -1
- data/test/unit/parameter_matchers/responds_with_test.rb +1 -1
- data/test/unit/return_values_test.rb +25 -20
- data/test/unit/single_return_value_test.rb +6 -1
- data/test/unit/thrower_test.rb +7 -2
- data/test/unit/yield_parameters_test.rb +35 -53
- metadata +19 -34
- data/docs/Mocha/UnexpectedInvocation.html +0 -140
- data/lib/mocha/mini_test.rb +0 -5
- data/lib/mocha/module_method.rb +0 -6
- data/lib/mocha/module_methods.rb +0 -10
- data/lib/mocha/multiple_yields.rb +0 -15
- data/lib/mocha/no_yields.rb +0 -5
- data/lib/mocha/parameter_matchers/object.rb +0 -15
- data/lib/mocha/single_yield.rb +0 -13
- data/lib/mocha/standalone.rb +0 -4
- data/lib/mocha/unexpected_invocation.rb +0 -24
- data/lib/mocha_standalone.rb +0 -4
- data/test/acceptance/mock_with_initializer_block_test.rb +0 -56
- data/test/unit/class_method_test.rb +0 -276
- data/test/unit/multiple_yields_test.rb +0 -16
- data/test/unit/no_yields_test.rb +0 -16
- data/test/unit/single_yield_test.rb +0 -16
data/lib/mocha/deprecation.rb
CHANGED
@@ -14,10 +14,8 @@ module Mocha
|
|
14
14
|
if testcase
|
15
15
|
begin
|
16
16
|
require 'test/unit/version'
|
17
|
-
# rubocop:disable Lint/HandleExceptions
|
18
|
-
rescue LoadError
|
17
|
+
rescue LoadError # rubocop:disable Lint/HandleExceptions
|
19
18
|
end
|
20
|
-
# rubocop:enable Lint/HandleExceptions
|
21
19
|
if defined?(::Test::Unit::VERSION)
|
22
20
|
version = ::Test::Unit::VERSION
|
23
21
|
end
|
@@ -5,7 +5,8 @@ module Mocha
|
|
5
5
|
@message = message
|
6
6
|
end
|
7
7
|
|
8
|
-
def evaluate
|
8
|
+
def evaluate(invocation)
|
9
|
+
invocation.raised(@exception)
|
9
10
|
raise @exception, @exception.to_s if @exception.is_a?(Module) && (@exception < Interrupt)
|
10
11
|
raise @exception, @message if @message
|
11
12
|
raise @exception
|
data/lib/mocha/expectation.rb
CHANGED
@@ -9,6 +9,8 @@ require 'mocha/is_a'
|
|
9
9
|
require 'mocha/in_state_ordering_constraint'
|
10
10
|
require 'mocha/change_state_side_effect'
|
11
11
|
require 'mocha/cardinality'
|
12
|
+
require 'mocha/configuration'
|
13
|
+
require 'mocha/block_matcher'
|
12
14
|
|
13
15
|
module Mocha
|
14
16
|
# Methods on expectations returned from {Mock#expects}, {Mock#stubs}, {ObjectMethods#expects} and {ObjectMethods#stubs}.
|
@@ -223,7 +225,45 @@ module Mocha
|
|
223
225
|
self
|
224
226
|
end
|
225
227
|
|
226
|
-
# Modifies expectation so that
|
228
|
+
# Modifies expectation so that the expected method must be called with a block.
|
229
|
+
#
|
230
|
+
# @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
|
231
|
+
#
|
232
|
+
# @example Expected method must be called with a block.
|
233
|
+
# object = mock()
|
234
|
+
# object.expects(:expected_method).with_block_given
|
235
|
+
# object.expected_method { 1 + 1 }
|
236
|
+
# # => verify succeeds
|
237
|
+
#
|
238
|
+
# object = mock()
|
239
|
+
# object.expects(:expected_method).with_block_given
|
240
|
+
# object.expected_method
|
241
|
+
# # => verify fails
|
242
|
+
def with_block_given
|
243
|
+
@block_matcher = BlockMatchers::BlockGiven.new
|
244
|
+
self
|
245
|
+
end
|
246
|
+
|
247
|
+
# Modifies expectation so that the expected method must be called without a block.
|
248
|
+
#
|
249
|
+
# @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
|
250
|
+
#
|
251
|
+
# @example Expected method must be called without a block.
|
252
|
+
# object = mock()
|
253
|
+
# object.expects(:expected_method).with_no_block_given
|
254
|
+
# object.expected_method
|
255
|
+
# # => verify succeeds
|
256
|
+
#
|
257
|
+
# object = mock()
|
258
|
+
# object.expects(:expected_method).with_block_given
|
259
|
+
# object.expected_method { 1 + 1 }
|
260
|
+
# # => verify fails
|
261
|
+
def with_no_block_given
|
262
|
+
@block_matcher = BlockMatchers::NoBlockGiven.new
|
263
|
+
self
|
264
|
+
end
|
265
|
+
|
266
|
+
# Modifies expectation so that when the expected method is called, it yields with the specified +parameters+ (even if no block is provided, in which case yielding will result in a +LocalJumpError+).
|
227
267
|
#
|
228
268
|
# May be called multiple times on the same expectation for consecutive invocations.
|
229
269
|
#
|
@@ -231,51 +271,57 @@ module Mocha
|
|
231
271
|
# @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
|
232
272
|
# @see #then
|
233
273
|
#
|
274
|
+
# @example Yield when expected method is invoked.
|
275
|
+
# benchmark = mock()
|
276
|
+
# benchmark.expects(:measure).yields.then.returns('0.350000 0.400000 0.750000 ( 0.835234)')
|
277
|
+
# yielded = false
|
278
|
+
# returned_value = benchmark.measure { yielded = true }
|
279
|
+
# yielded # => true
|
280
|
+
# returned_value # => '0.350000 0.400000 0.750000 ( 0.835234)'
|
281
|
+
#
|
234
282
|
# @example Yield parameters when expected method is invoked.
|
235
|
-
#
|
236
|
-
#
|
237
|
-
#
|
238
|
-
#
|
239
|
-
#
|
283
|
+
# fibonacci = mock()
|
284
|
+
# fibonacci.expects(:next_pair).yields(0, 1)
|
285
|
+
# sum = 0
|
286
|
+
# fibonacci.next_pair { |first, second| sum = first + second }
|
287
|
+
# sum # => 1
|
240
288
|
#
|
241
289
|
# @example Yield different parameters on different invocations of the expected method.
|
242
|
-
#
|
243
|
-
#
|
244
|
-
#
|
245
|
-
#
|
246
|
-
#
|
247
|
-
#
|
248
|
-
#
|
249
|
-
# yielded_values_from_second_invocation # => [2]
|
290
|
+
# fibonacci = mock()
|
291
|
+
# fibonacci.expects(:next_pair).yields(0, 1).then.yields(1, 1)
|
292
|
+
# sum = 0
|
293
|
+
# fibonacci.next_pair { |first, second| sum = first + second }
|
294
|
+
# sum # => 1
|
295
|
+
# fibonacci.next_pair { |first, second| sum = first + second }
|
296
|
+
# sum # => 2
|
250
297
|
def yields(*parameters)
|
251
|
-
|
252
|
-
self
|
298
|
+
multiple_yields(parameters)
|
253
299
|
end
|
254
300
|
|
255
|
-
# Modifies expectation so that when the expected method is called, it yields multiple times per invocation with the specified +parameter_groups
|
301
|
+
# Modifies expectation so that when the expected method is called, it yields multiple times per invocation with the specified +parameter_groups+ (even if no block is provided, in which case yielding will result in a +LocalJumpError+).
|
256
302
|
#
|
257
|
-
# @param [*Array<Array>] parameter_groups each element of +parameter_groups+ should iself be an +Array+ representing the parameters to be passed to the block for a single yield.
|
303
|
+
# @param [*Array<Array>] parameter_groups each element of +parameter_groups+ should iself be an +Array+ representing the parameters to be passed to the block for a single yield. Any element of +parameter_groups+ that is not an +Array+ is wrapped in an +Array+.
|
258
304
|
# @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
|
259
305
|
# @see #then
|
260
306
|
#
|
261
|
-
# @example When
|
262
|
-
#
|
263
|
-
#
|
264
|
-
#
|
265
|
-
#
|
266
|
-
#
|
267
|
-
#
|
268
|
-
# @example Yield different groups of parameters on different invocations of the expected method.
|
269
|
-
#
|
270
|
-
#
|
271
|
-
#
|
272
|
-
#
|
273
|
-
#
|
274
|
-
#
|
275
|
-
#
|
276
|
-
#
|
307
|
+
# @example When +foreach+ is called, the stub will invoke the block twice, the first time it passes ['row1_col1', 'row1_col2'] as the parameters, and the second time it passes ['row2_col1', ''] as the parameters.
|
308
|
+
# csv = mock()
|
309
|
+
# csv.expects(:foreach).with("path/to/file.csv").multiple_yields(['row1_col1', 'row1_col2'], ['row2_col1', ''])
|
310
|
+
# rows = []
|
311
|
+
# csv.foreach { |row| rows << row }
|
312
|
+
# rows # => [['row1_col1', 'row1_col2'], ['row2_col1', '']]
|
313
|
+
#
|
314
|
+
# @example Yield different groups of parameters on different invocations of the expected method. Simulating a situation where the CSV file at 'path/to/file.csv' has been modified between the two calls to +foreach+.
|
315
|
+
# csv = mock()
|
316
|
+
# csv.stubs(:foreach).with("path/to/file.csv").multiple_yields(['old_row1_col1', 'old_row1_col2'], ['old_row2_col1', '']).then.multiple_yields(['new_row1_col1', ''], ['new_row2_col1', 'new_row2_col2'])
|
317
|
+
# rows_from_first_invocation = []
|
318
|
+
# rows_from_second_invocation = []
|
319
|
+
# csv.foreach { |row| rows_from_first_invocation << row } # first invocation
|
320
|
+
# csv.foreach { |row| rows_from_second_invocation << row } # second invocation
|
321
|
+
# rows_from_first_invocation # => [['old_row1_col1', 'old_row1_col2'], ['old_row2_col1', '']]
|
322
|
+
# rows_from_second_invocation # => [['new_row1_col1', ''], ['new_row2_col1', 'new_row2_col2']]
|
277
323
|
def multiple_yields(*parameter_groups)
|
278
|
-
@yield_parameters.
|
324
|
+
@yield_parameters.add(*parameter_groups)
|
279
325
|
self
|
280
326
|
end
|
281
327
|
|
@@ -477,7 +523,8 @@ module Mocha
|
|
477
523
|
#
|
478
524
|
# An expected method can appear in multiple sequences.
|
479
525
|
#
|
480
|
-
# @param [
|
526
|
+
# @param [Sequence] sequence sequence in which expected method should appear.
|
527
|
+
# @param [*Array<Sequence>] sequences more sequences in which expected method should appear.
|
481
528
|
# @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
|
482
529
|
#
|
483
530
|
# @see API#sequence
|
@@ -489,8 +536,8 @@ module Mocha
|
|
489
536
|
# egg.expects(:crack).in_sequence(breakfast)
|
490
537
|
# egg.expects(:fry).in_sequence(breakfast)
|
491
538
|
# egg.expects(:eat).in_sequence(breakfast)
|
492
|
-
def in_sequence(*sequences)
|
493
|
-
sequences.each { |
|
539
|
+
def in_sequence(sequence, *sequences)
|
540
|
+
sequences.unshift(sequence).each { |seq| add_in_sequence_ordering_constraint(seq) }
|
494
541
|
self
|
495
542
|
end
|
496
543
|
|
@@ -502,10 +549,10 @@ module Mocha
|
|
502
549
|
@mock = mock
|
503
550
|
@method_matcher = MethodMatcher.new(expected_method_name.to_sym)
|
504
551
|
@parameters_matcher = ParametersMatcher.new
|
552
|
+
@block_matcher = BlockMatchers::OptionalBlock.new
|
505
553
|
@ordering_constraints = []
|
506
554
|
@side_effects = []
|
507
555
|
@cardinality = Cardinality.exactly(1)
|
508
|
-
@invocation_count = 0
|
509
556
|
@return_values = ReturnValues.new
|
510
557
|
@yield_parameters = YieldParameters.new
|
511
558
|
@backtrace = backtrace || caller
|
@@ -542,41 +589,36 @@ module Mocha
|
|
542
589
|
end
|
543
590
|
|
544
591
|
# @private
|
545
|
-
def match?(
|
546
|
-
@method_matcher.match?(
|
592
|
+
def match?(invocation)
|
593
|
+
@method_matcher.match?(invocation.method_name) && @parameters_matcher.match?(invocation.arguments) && @block_matcher.match?(invocation.block) && in_correct_order?
|
547
594
|
end
|
548
595
|
|
549
596
|
# @private
|
550
597
|
def invocations_allowed?
|
551
|
-
@cardinality.invocations_allowed?
|
598
|
+
@cardinality.invocations_allowed?
|
552
599
|
end
|
553
600
|
|
554
601
|
# @private
|
555
602
|
def satisfied?
|
556
|
-
@cardinality.satisfied?
|
603
|
+
@cardinality.satisfied?
|
557
604
|
end
|
558
605
|
|
559
606
|
# @private
|
560
|
-
def invoke
|
561
|
-
@invocation_count += 1
|
607
|
+
def invoke(invocation)
|
562
608
|
perform_side_effects
|
563
|
-
|
564
|
-
|
565
|
-
yield(*yield_parameters)
|
566
|
-
end
|
567
|
-
end
|
568
|
-
@return_values.next
|
609
|
+
@cardinality << invocation
|
610
|
+
invocation.call(@yield_parameters, @return_values)
|
569
611
|
end
|
570
612
|
|
571
613
|
# @private
|
572
614
|
def verified?(assertion_counter = nil)
|
573
615
|
assertion_counter.increment if assertion_counter && @cardinality.needs_verifying?
|
574
|
-
@cardinality.verified?
|
616
|
+
@cardinality.verified?
|
575
617
|
end
|
576
618
|
|
577
619
|
# @private
|
578
620
|
def used?
|
579
|
-
@cardinality.used?
|
621
|
+
@cardinality.used?
|
580
622
|
end
|
581
623
|
|
582
624
|
# @private
|
@@ -588,22 +630,19 @@ module Mocha
|
|
588
630
|
|
589
631
|
# @private
|
590
632
|
def mocha_inspect
|
591
|
-
message = "#{@cardinality.
|
592
|
-
message << case @invocation_count
|
593
|
-
when 0 then 'not yet invoked'
|
594
|
-
when 1 then 'invoked once'
|
595
|
-
when 2 then 'invoked twice'
|
596
|
-
else "invoked #{@invocation_count} times"
|
597
|
-
end
|
598
|
-
message << ': '
|
599
|
-
message << method_signature
|
633
|
+
message = "#{@cardinality.anticipated_times}, #{@cardinality.invoked_times}: #{method_signature}"
|
600
634
|
message << "; #{@ordering_constraints.map(&:mocha_inspect).join('; ')}" unless @ordering_constraints.empty?
|
635
|
+
if Mocha.configuration.display_matching_invocations_on_failure?
|
636
|
+
message << @cardinality.actual_invocations
|
637
|
+
end
|
601
638
|
message
|
602
639
|
end
|
603
640
|
|
604
641
|
# @private
|
605
642
|
def method_signature
|
606
|
-
"#{@mock.mocha_inspect}.#{@method_matcher.mocha_inspect}#{@parameters_matcher.mocha_inspect}"
|
643
|
+
signature = "#{@mock.mocha_inspect}.#{@method_matcher.mocha_inspect}#{@parameters_matcher.mocha_inspect}"
|
644
|
+
signature << " #{@block_matcher.mocha_inspect}" if @block_matcher.mocha_inspect
|
645
|
+
signature
|
607
646
|
end
|
608
647
|
end
|
609
648
|
end
|
@@ -4,7 +4,5 @@ module Mocha
|
|
4
4
|
# Authors of test libraries may use +Mocha::ExpectationErrorFactory+ to have Mocha raise a different exception.
|
5
5
|
#
|
6
6
|
# @see Mocha::ExpectationErrorFactory
|
7
|
-
# rubocop:disable Lint/InheritException
|
8
|
-
class ExpectationError < Exception; end
|
9
|
-
# rubocop:enable Lint/InheritException
|
7
|
+
class ExpectationError < Exception; end # rubocop:disable Lint/InheritException
|
10
8
|
end
|
@@ -17,12 +17,12 @@ module Mocha
|
|
17
17
|
@expectations.any? { |expectation| expectation.matches_method?(method_name) }
|
18
18
|
end
|
19
19
|
|
20
|
-
def match(
|
21
|
-
matching_expectations(
|
20
|
+
def match(invocation)
|
21
|
+
matching_expectations(invocation).first
|
22
22
|
end
|
23
23
|
|
24
|
-
def match_allowing_invocation(
|
25
|
-
matching_expectations(
|
24
|
+
def match_allowing_invocation(invocation)
|
25
|
+
matching_expectations(invocation).detect(&:invocations_allowed?)
|
26
26
|
end
|
27
27
|
|
28
28
|
def verified?(assertion_counter = nil)
|
@@ -51,8 +51,8 @@ module Mocha
|
|
51
51
|
|
52
52
|
private
|
53
53
|
|
54
|
-
def matching_expectations(
|
55
|
-
@expectations.select { |e| e.match?(
|
54
|
+
def matching_expectations(invocation)
|
55
|
+
@expectations.select { |e| e.match?(invocation) }
|
56
56
|
end
|
57
57
|
end
|
58
58
|
end
|
data/lib/mocha/inspect.rb
CHANGED
@@ -1,55 +1,57 @@
|
|
1
1
|
require 'date'
|
2
2
|
|
3
3
|
module Mocha
|
4
|
-
module
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
4
|
+
module Inspect
|
5
|
+
module ObjectMethods
|
6
|
+
def mocha_inspect
|
7
|
+
address = __id__ * 2
|
8
|
+
address += 0x100000000 if address < 0
|
9
|
+
inspect =~ /#</ ? "#<#{self.class}:0x#{Kernel.format('%x', address)}>" : inspect
|
10
|
+
end
|
9
11
|
end
|
10
|
-
end
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
13
|
+
module ArrayMethods
|
14
|
+
def mocha_inspect
|
15
|
+
"[#{collect(&:mocha_inspect).join(', ')}]"
|
16
|
+
end
|
15
17
|
end
|
16
|
-
end
|
17
18
|
|
18
|
-
|
19
|
-
|
20
|
-
|
19
|
+
module HashMethods
|
20
|
+
def mocha_inspect
|
21
|
+
"{#{collect { |key, value| "#{key.mocha_inspect} => #{value.mocha_inspect}" }.join(', ')}}"
|
22
|
+
end
|
21
23
|
end
|
22
|
-
end
|
23
24
|
|
24
|
-
|
25
|
-
|
26
|
-
|
25
|
+
module TimeMethods
|
26
|
+
def mocha_inspect
|
27
|
+
"#{inspect} (#{to_f} secs)"
|
28
|
+
end
|
27
29
|
end
|
28
|
-
end
|
29
30
|
|
30
|
-
|
31
|
-
|
32
|
-
|
31
|
+
module DateMethods
|
32
|
+
def mocha_inspect
|
33
|
+
to_s
|
34
|
+
end
|
33
35
|
end
|
34
36
|
end
|
35
37
|
end
|
36
38
|
|
37
39
|
class Object
|
38
|
-
include Mocha::ObjectMethods
|
40
|
+
include Mocha::Inspect::ObjectMethods
|
39
41
|
end
|
40
42
|
|
41
43
|
class Array
|
42
|
-
include Mocha::ArrayMethods
|
44
|
+
include Mocha::Inspect::ArrayMethods
|
43
45
|
end
|
44
46
|
|
45
47
|
class Hash
|
46
|
-
include Mocha::HashMethods
|
48
|
+
include Mocha::Inspect::HashMethods
|
47
49
|
end
|
48
50
|
|
49
51
|
class Time
|
50
|
-
include Mocha::TimeMethods
|
52
|
+
include Mocha::Inspect::TimeMethods
|
51
53
|
end
|
52
54
|
|
53
55
|
class Date
|
54
|
-
include Mocha::DateMethods
|
56
|
+
include Mocha::Inspect::DateMethods
|
55
57
|
end
|
@@ -1,6 +1,23 @@
|
|
1
|
-
require 'mocha/
|
1
|
+
require 'mocha/stubbed_method'
|
2
2
|
|
3
3
|
module Mocha
|
4
|
-
class InstanceMethod <
|
4
|
+
class InstanceMethod < StubbedMethod
|
5
|
+
private
|
6
|
+
|
7
|
+
def mock_owner
|
8
|
+
stubbee
|
9
|
+
end
|
10
|
+
|
11
|
+
def method_body(method)
|
12
|
+
PRE_RUBY_V19 ? proc { |*args, &block| method.call(*args, &block) } : method
|
13
|
+
end
|
14
|
+
|
15
|
+
def stubbee_method(method_name)
|
16
|
+
stubbee._method(method_name)
|
17
|
+
end
|
18
|
+
|
19
|
+
def original_method_owner
|
20
|
+
stubbee.singleton_class
|
21
|
+
end
|
5
22
|
end
|
6
23
|
end
|
data/lib/mocha/integration.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
require 'mocha/deprecation'
|
2
1
|
require 'mocha/integration/test_unit'
|
3
2
|
require 'mocha/integration/mini_test'
|
4
3
|
|
@@ -6,8 +5,7 @@ module Mocha
|
|
6
5
|
module Integration
|
7
6
|
def self.activate
|
8
7
|
return unless [Integration::TestUnit, Integration::MiniTest].map(&:activate).none?
|
9
|
-
|
10
|
-
Deprecation.warning("If you're integrating with a test library other than Test::Unit or MiniTest, you should use `require 'mocha/api'` instead of `require 'mocha/setup'`.")
|
8
|
+
raise "Test::Unit or Minitest must be loaded *before* `require 'mocha/setup'`."
|
11
9
|
end
|
12
10
|
end
|
13
11
|
end
|