mocha 1.8.0 → 1.11.0
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 +40 -13
- data/RELEASE.md +97 -0
- data/Rakefile +10 -7
- data/bin/build-matrix +1 -2
- data/docs/CNAME +1 -1
- 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 +46 -16
- data/docs/file.RELEASE.html +130 -3
- data/docs/frames.html +1 -1
- data/docs/index.html +46 -16
- data/docs/js/app.js +11 -0
- 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 +11 -53
- 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/stubbed_method.rb +127 -0
- 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 +99 -0
- data/test/acceptance/mock_test.rb +47 -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 +56 -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 +78 -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 +60 -35
- 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 +117 -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 +18 -35
- data/docs/Mocha/UnexpectedInvocation.html +0 -140
- data/lib/mocha/class_method.rb +0 -113
- 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 -254
- 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
@@ -1,10 +1,12 @@
|
|
1
1
|
require File.expand_path('../../test_helper', __FILE__)
|
2
2
|
require 'mocha/expectation_list'
|
3
3
|
require 'mocha/expectation'
|
4
|
+
require 'mocha/invocation'
|
4
5
|
require 'set'
|
5
6
|
require 'method_definer'
|
6
7
|
|
7
8
|
class ExpectationListTest < Mocha::TestCase
|
9
|
+
include MethodDefiner
|
8
10
|
include Mocha
|
9
11
|
|
10
12
|
def test_should_return_added_expectation
|
@@ -19,7 +21,7 @@ class ExpectationListTest < Mocha::TestCase
|
|
19
21
|
expectation2 = Expectation.new(nil, :my_method).with(:argument3, :argument4)
|
20
22
|
expectation_list.add(expectation1)
|
21
23
|
expectation_list.add(expectation2)
|
22
|
-
assert_same expectation1, expectation_list.match(:my_method, :argument1, :argument2)
|
24
|
+
assert_same expectation1, expectation_list.match(Invocation.new(:irrelevant, :my_method, :argument1, :argument2))
|
23
25
|
end
|
24
26
|
|
25
27
|
def test_should_remove_all_expectations_matching_method_name
|
@@ -31,9 +33,9 @@ class ExpectationListTest < Mocha::TestCase
|
|
31
33
|
expectation_list.add(expectation2)
|
32
34
|
expectation_list.add(expectation3)
|
33
35
|
expectation_list.remove_all_matching_method(:method_one)
|
34
|
-
assert_nil expectation_list.match(:method_one, :argument1, :argument2)
|
35
|
-
assert_nil expectation_list.match(:method_one, :argument3, :argument4)
|
36
|
-
assert_same expectation3, expectation_list.match(:method_two)
|
36
|
+
assert_nil expectation_list.match(Invocation.new(:irrelevant, :method_one, :argument1, :argument2))
|
37
|
+
assert_nil expectation_list.match(Invocation.new(:irrelevant, :method_one, :argument3, :argument4))
|
38
|
+
assert_same expectation3, expectation_list.match(Invocation.new(:irrelevant, :method_two))
|
37
39
|
end
|
38
40
|
|
39
41
|
def test_should_find_most_recent_matching_expectation
|
@@ -42,29 +44,29 @@ class ExpectationListTest < Mocha::TestCase
|
|
42
44
|
expectation2 = Expectation.new(nil, :my_method).with(:argument1, :argument2)
|
43
45
|
expectation_list.add(expectation1)
|
44
46
|
expectation_list.add(expectation2)
|
45
|
-
assert_same expectation2, expectation_list.match(:my_method, :argument1, :argument2)
|
47
|
+
assert_same expectation2, expectation_list.match(Invocation.new(:irrelevant, :my_method, :argument1, :argument2))
|
46
48
|
end
|
47
49
|
|
48
50
|
def test_should_find_matching_expectation_allowing_invocation
|
49
51
|
expectation_list = ExpectationList.new
|
50
52
|
expectation1 = Expectation.new(nil, :my_method).with(:argument1, :argument2)
|
51
53
|
expectation2 = Expectation.new(nil, :my_method).with(:argument3, :argument4)
|
52
|
-
|
53
|
-
|
54
|
+
define_instance_method(expectation1, :invocations_allowed?) { true }
|
55
|
+
define_instance_method(expectation2, :invocations_allowed?) { true }
|
54
56
|
expectation_list.add(expectation1)
|
55
57
|
expectation_list.add(expectation2)
|
56
|
-
assert_same expectation1, expectation_list.match_allowing_invocation(:my_method, :argument1, :argument2)
|
58
|
+
assert_same expectation1, expectation_list.match_allowing_invocation(Invocation.new(:irrelevant, :my_method, :argument1, :argument2))
|
57
59
|
end
|
58
60
|
|
59
61
|
def test_should_find_most_recent_matching_expectation_allowing_invocation
|
60
62
|
expectation_list = ExpectationList.new
|
61
63
|
expectation1 = Expectation.new(nil, :my_method)
|
62
64
|
expectation2 = Expectation.new(nil, :my_method)
|
63
|
-
|
64
|
-
|
65
|
+
define_instance_method(expectation1, :invocations_allowed?) { true }
|
66
|
+
define_instance_method(expectation2, :invocations_allowed?) { false }
|
65
67
|
expectation_list.add(expectation1)
|
66
68
|
expectation_list.add(expectation2)
|
67
|
-
assert_same expectation1, expectation_list.match_allowing_invocation(:my_method)
|
69
|
+
assert_same expectation1, expectation_list.match_allowing_invocation(Invocation.new(:irrelevant, :my_method))
|
68
70
|
end
|
69
71
|
|
70
72
|
def test_should_combine_two_expectation_lists_into_one
|
@@ -1,10 +1,14 @@
|
|
1
1
|
require File.expand_path('../../test_helper', __FILE__)
|
2
|
-
require 'method_definer'
|
3
2
|
require 'mocha/expectation'
|
3
|
+
require 'mocha/invocation'
|
4
4
|
require 'mocha/sequence'
|
5
|
+
require 'mocha/configuration'
|
6
|
+
require 'mocha/deprecation'
|
5
7
|
require 'execution_point'
|
6
8
|
require 'simple_counter'
|
9
|
+
require 'deprecation_disabler'
|
7
10
|
|
11
|
+
# rubocop:disable Metrics/ClassLength
|
8
12
|
class ExpectationTest < Mocha::TestCase
|
9
13
|
include Mocha
|
10
14
|
|
@@ -12,90 +16,85 @@ class ExpectationTest < Mocha::TestCase
|
|
12
16
|
Expectation.new(nil, :expected_method)
|
13
17
|
end
|
14
18
|
|
19
|
+
def invoke(expectation, &block)
|
20
|
+
expectation.invoke(Invocation.new(:irrelevant, :expected_method, &block))
|
21
|
+
end
|
22
|
+
|
15
23
|
def test_should_match_calls_to_same_method_with_any_parameters
|
16
|
-
assert new_expectation.match?(:expected_method, 1, 2, 3)
|
24
|
+
assert new_expectation.match?(Invocation.new(:irrelevant, :expected_method, 1, 2, 3))
|
17
25
|
end
|
18
26
|
|
19
27
|
def test_should_match_calls_to_same_method_with_exactly_zero_parameters
|
20
|
-
|
21
|
-
assert expectation.match?(:expected_method)
|
28
|
+
assert new_expectation.with.match?(Invocation.new(:irrelevant, :expected_method))
|
22
29
|
end
|
23
30
|
|
24
31
|
def test_should_not_match_calls_to_same_method_with_more_than_zero_parameters
|
25
|
-
|
26
|
-
assert !expectation.match?(:expected_method, 1, 2, 3)
|
32
|
+
assert !new_expectation.with.match?(Invocation.new(:irrelevant, :expected_method, 1, 2, 3))
|
27
33
|
end
|
28
34
|
|
29
35
|
def test_should_match_calls_to_same_method_with_expected_parameter_values
|
30
|
-
|
31
|
-
assert expectation.match?(:expected_method, 1, 2, 3)
|
36
|
+
assert new_expectation.with(1, 2, 3).match?(Invocation.new(:irrelevant, :expected_method, 1, 2, 3))
|
32
37
|
end
|
33
38
|
|
34
39
|
def test_should_match_calls_to_same_method_with_parameters_constrained_as_expected
|
35
40
|
expectation = new_expectation.with { |x, y, z| x + y == z }
|
36
|
-
assert expectation.match?(:expected_method, 1, 2, 3)
|
41
|
+
assert expectation.match?(Invocation.new(:irrelevant, :expected_method, 1, 2, 3))
|
37
42
|
end
|
38
43
|
|
39
44
|
def test_should_not_match_calls_to_different_method_with_parameters_constrained_as_expected
|
40
45
|
expectation = new_expectation.with { |x, y, z| x + y == z }
|
41
|
-
assert !expectation.match?(:different_method, 1, 2, 3)
|
46
|
+
assert !expectation.match?(Invocation.new(:irrelevant, :different_method, 1, 2, 3))
|
42
47
|
end
|
43
48
|
|
44
49
|
def test_should_not_match_calls_to_different_methods_with_no_parameters
|
45
|
-
assert !new_expectation.match?(:unexpected_method)
|
50
|
+
assert !new_expectation.match?(Invocation.new(:irrelevant, :unexpected_method))
|
46
51
|
end
|
47
52
|
|
48
53
|
def test_should_not_match_calls_to_same_method_with_too_few_parameters
|
49
|
-
|
50
|
-
assert !expectation.match?(:unexpected_method, 1, 2)
|
54
|
+
assert !new_expectation.with(1, 2, 3).match?(Invocation.new(:irrelevant, :expected_method, 1, 2))
|
51
55
|
end
|
52
56
|
|
53
57
|
def test_should_not_match_calls_to_same_method_with_too_many_parameters
|
54
|
-
|
55
|
-
assert !expectation.match?(:unexpected_method, 1, 2, 3)
|
58
|
+
assert !new_expectation.with(1, 2).match?(Invocation.new(:irrelevant, :expected_method, 1, 2, 3))
|
56
59
|
end
|
57
60
|
|
58
61
|
def test_should_not_match_calls_to_same_method_with_unexpected_parameter_values
|
59
|
-
|
60
|
-
assert !expectation.match?(:unexpected_method, 1, 0, 3)
|
62
|
+
assert !new_expectation.with(1, 2, 3).match?(Invocation.new(:irrelevant, :expected_method, 1, 0, 3))
|
61
63
|
end
|
62
64
|
|
63
65
|
def test_should_not_match_calls_to_same_method_with_parameters_not_constrained_as_expected
|
64
66
|
expectation = new_expectation.with { |x, y, z| x + y == z }
|
65
|
-
assert !expectation.match?(:expected_method, 1, 0, 3)
|
67
|
+
assert !expectation.match?(Invocation.new(:irrelevant, :expected_method, 1, 0, 3))
|
66
68
|
end
|
67
69
|
|
68
70
|
def test_should_allow_invocations_until_expected_invocation_count_is_one_and_actual_invocation_count_would_be_two
|
69
71
|
expectation = new_expectation.times(1)
|
70
72
|
assert expectation.invocations_allowed?
|
71
|
-
expectation
|
73
|
+
invoke(expectation)
|
72
74
|
assert !expectation.invocations_allowed?
|
73
75
|
end
|
74
76
|
|
75
77
|
def test_should_allow_invocations_until_expected_invocation_count_is_two_and_actual_invocation_count_would_be_three
|
76
78
|
expectation = new_expectation.times(2)
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
79
|
+
2.times do
|
80
|
+
assert expectation.invocations_allowed?
|
81
|
+
invoke(expectation)
|
82
|
+
end
|
81
83
|
assert !expectation.invocations_allowed?
|
82
84
|
end
|
83
85
|
|
84
86
|
def test_should_allow_invocations_until_expected_invocation_count_is_a_range_from_two_to_three_and_actual_invocation_count_would_be_four
|
85
87
|
expectation = new_expectation.times(2..3)
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
assert expectation.invocations_allowed?
|
91
|
-
expectation.invoke
|
88
|
+
3.times do
|
89
|
+
assert expectation.invocations_allowed?
|
90
|
+
invoke(expectation)
|
91
|
+
end
|
92
92
|
assert !expectation.invocations_allowed?
|
93
93
|
end
|
94
94
|
|
95
95
|
def test_should_store_provided_backtrace
|
96
96
|
backtrace = Object.new
|
97
|
-
|
98
|
-
assert_equal backtrace, expectation.backtrace
|
97
|
+
assert_equal backtrace, Expectation.new(nil, :expected_method, backtrace).backtrace
|
99
98
|
end
|
100
99
|
|
101
100
|
# rubocop:disable Style/Semicolon
|
@@ -107,217 +106,233 @@ class ExpectationTest < Mocha::TestCase
|
|
107
106
|
|
108
107
|
def test_should_not_yield
|
109
108
|
yielded = false
|
110
|
-
new_expectation
|
109
|
+
invoke(new_expectation) { yielded = true }
|
111
110
|
assert_equal false, yielded
|
112
111
|
end
|
113
112
|
|
114
113
|
def test_should_yield_no_parameters
|
115
|
-
expectation = new_expectation.yields
|
116
114
|
yielded_parameters = nil
|
117
|
-
|
115
|
+
invoke(new_expectation.yields) { |*parameters| yielded_parameters = parameters }
|
118
116
|
assert_equal [], yielded_parameters
|
119
117
|
end
|
120
118
|
|
119
|
+
def test_yield_should_fail_when_the_caller_does_not_provide_a_block
|
120
|
+
assert_raises(LocalJumpError) { invoke(new_expectation.yields(:foo)) }
|
121
|
+
end
|
122
|
+
|
123
|
+
def test_yields_should_display_warning_when_caller_does_not_provide_block_and_behaviour_from_v1_9_retained
|
124
|
+
Mocha::Configuration.override(:reinstate_undocumented_behaviour_from_v1_9 => true) do
|
125
|
+
DeprecationDisabler.disable_deprecations do
|
126
|
+
invoke(new_expectation.yields(:foo, 1, [2, 3]))
|
127
|
+
end
|
128
|
+
end
|
129
|
+
assert message = Deprecation.messages.last
|
130
|
+
assert message.include?('Stubbed method was instructed to yield (:foo, 1, [2, 3])')
|
131
|
+
assert message.include?('but no block was given by invocation: :irrelevant.expected_method()')
|
132
|
+
assert message.include?('This will raise a LocalJumpError in the future.')
|
133
|
+
end
|
134
|
+
|
135
|
+
def test_multiple_yields_should_display_warning_when_caller_does_not_provide_block_and_behaviour_from_v1_9_retained
|
136
|
+
Mocha::Configuration.override(:reinstate_undocumented_behaviour_from_v1_9 => true) do
|
137
|
+
DeprecationDisabler.disable_deprecations do
|
138
|
+
invoke(new_expectation.multiple_yields(:foo, 1, [2, 3]))
|
139
|
+
end
|
140
|
+
end
|
141
|
+
assert message = Deprecation.messages.last
|
142
|
+
assert message.include?('Stubbed method was instructed to yield (2, 3)')
|
143
|
+
assert message.include?('but no block was given by invocation: :irrelevant.expected_method()')
|
144
|
+
assert message.include?('This will raise a LocalJumpError in the future.')
|
145
|
+
end
|
146
|
+
|
121
147
|
def test_should_yield_with_specified_parameters
|
122
|
-
expectation = new_expectation.yields(1, 2, 3)
|
123
148
|
yielded_parameters = nil
|
124
|
-
|
149
|
+
invoke(new_expectation.yields(1, 2, 3)) { |*parameters| yielded_parameters = parameters }
|
125
150
|
assert_equal [1, 2, 3], yielded_parameters
|
126
151
|
end
|
127
152
|
|
128
153
|
def test_should_yield_different_parameters_on_consecutive_invocations
|
129
154
|
expectation = new_expectation.yields(1, 2, 3).yields(4, 5)
|
130
155
|
yielded_parameters = []
|
131
|
-
expectation
|
132
|
-
expectation
|
156
|
+
invoke(expectation) { |*parameters| yielded_parameters << parameters }
|
157
|
+
invoke(expectation) { |*parameters| yielded_parameters << parameters }
|
133
158
|
assert_equal [[1, 2, 3], [4, 5]], yielded_parameters
|
134
159
|
end
|
135
160
|
|
136
161
|
def test_should_yield_multiple_times_for_single_invocation
|
137
|
-
expectation = new_expectation.multiple_yields([1, 2, 3], [4, 5])
|
138
162
|
yielded_parameters = []
|
139
|
-
|
163
|
+
invoke(new_expectation.multiple_yields([1, 2, 3], [4, 5])) { |*parameters| yielded_parameters << parameters }
|
140
164
|
assert_equal [[1, 2, 3], [4, 5]], yielded_parameters
|
141
165
|
end
|
142
166
|
|
143
167
|
def test_should_yield_multiple_times_for_first_invocation_and_once_for_second_invocation
|
144
168
|
expectation = new_expectation.multiple_yields([1, 2, 3], [4, 5]).then.yields(6, 7)
|
145
169
|
yielded_parameters = []
|
146
|
-
expectation
|
147
|
-
expectation
|
170
|
+
invoke(expectation) { |*parameters| yielded_parameters << parameters }
|
171
|
+
invoke(expectation) { |*parameters| yielded_parameters << parameters }
|
148
172
|
assert_equal [[1, 2, 3], [4, 5], [6, 7]], yielded_parameters
|
149
173
|
end
|
150
174
|
|
151
175
|
def test_should_return_specified_value
|
152
|
-
|
153
|
-
assert_equal 99, expectation.invoke
|
176
|
+
assert_equal 99, invoke(new_expectation.returns(99))
|
154
177
|
end
|
155
178
|
|
156
179
|
def test_should_return_same_specified_value_multiple_times
|
157
180
|
expectation = new_expectation.returns(99)
|
158
|
-
assert_equal 99, expectation
|
159
|
-
assert_equal 99, expectation
|
181
|
+
assert_equal 99, invoke(expectation)
|
182
|
+
assert_equal 99, invoke(expectation)
|
160
183
|
end
|
161
184
|
|
162
185
|
def test_should_return_specified_values_on_consecutive_calls
|
163
186
|
expectation = new_expectation.returns(99, 100, 101)
|
164
|
-
assert_equal 99, expectation
|
165
|
-
assert_equal 100, expectation
|
166
|
-
assert_equal 101, expectation
|
187
|
+
assert_equal 99, invoke(expectation)
|
188
|
+
assert_equal 100, invoke(expectation)
|
189
|
+
assert_equal 101, invoke(expectation)
|
167
190
|
end
|
168
191
|
|
169
192
|
def test_should_return_specified_values_on_consecutive_calls_even_if_values_are_modified
|
170
193
|
values = [99, 100, 101]
|
171
194
|
expectation = new_expectation.returns(*values)
|
172
195
|
values.shift
|
173
|
-
assert_equal 99, expectation
|
174
|
-
assert_equal 100, expectation
|
175
|
-
assert_equal 101, expectation
|
196
|
+
assert_equal 99, invoke(expectation)
|
197
|
+
assert_equal 100, invoke(expectation)
|
198
|
+
assert_equal 101, invoke(expectation)
|
176
199
|
end
|
177
200
|
|
178
201
|
def test_should_return_nil_by_default
|
179
|
-
assert_nil new_expectation
|
202
|
+
assert_nil invoke(new_expectation)
|
180
203
|
end
|
181
204
|
|
182
205
|
def test_should_return_nil_if_no_value_specified
|
183
|
-
|
184
|
-
assert_nil expectation.invoke
|
206
|
+
assert_nil invoke(new_expectation.returns)
|
185
207
|
end
|
186
208
|
|
187
209
|
def test_should_raise_runtime_exception
|
188
|
-
|
189
|
-
assert_raise(RuntimeError) { expectation.invoke }
|
210
|
+
assert_raises(RuntimeError) { invoke(new_expectation.raises) }
|
190
211
|
end
|
191
212
|
|
192
213
|
def test_should_raise_custom_exception
|
193
214
|
exception = Class.new(Exception)
|
194
|
-
|
195
|
-
assert_raise(exception) { expectation.invoke }
|
215
|
+
assert_raises(exception) { invoke(new_expectation.raises(exception)) }
|
196
216
|
end
|
197
217
|
|
198
218
|
def test_should_raise_same_instance_of_custom_exception
|
199
219
|
exception_klass = Class.new(StandardError)
|
200
220
|
expected_exception = exception_klass.new
|
201
|
-
|
202
|
-
actual_exception = assert_raise(exception_klass) { expectation.invoke }
|
221
|
+
actual_exception = assert_raises(exception_klass) { invoke(new_expectation.raises(expected_exception)) }
|
203
222
|
assert_same expected_exception, actual_exception
|
204
223
|
end
|
205
224
|
|
206
225
|
def test_should_use_the_default_exception_message
|
207
|
-
|
208
|
-
exception = assert_raise(Exception) { expectation.invoke }
|
226
|
+
exception = assert_raises(Exception) { invoke(new_expectation.raises(Exception)) }
|
209
227
|
assert_equal Exception.new.message, exception.message
|
210
228
|
end
|
211
229
|
|
212
230
|
def test_should_raise_custom_exception_with_message
|
213
231
|
exception_msg = 'exception message'
|
214
|
-
|
215
|
-
exception = assert_raise(Exception) { expectation.invoke }
|
232
|
+
exception = assert_raises(Exception) { invoke(new_expectation.raises(Exception, exception_msg)) }
|
216
233
|
assert_equal exception_msg, exception.message
|
217
234
|
end
|
218
235
|
|
219
236
|
def test_should_return_values_then_raise_exception
|
220
237
|
expectation = new_expectation.returns(1, 2).then.raises
|
221
|
-
assert_equal 1, expectation
|
222
|
-
assert_equal 2, expectation
|
223
|
-
|
238
|
+
assert_equal 1, invoke(expectation)
|
239
|
+
assert_equal 2, invoke(expectation)
|
240
|
+
assert_raises(RuntimeError) { invoke(expectation) }
|
224
241
|
end
|
225
242
|
|
226
243
|
def test_should_raise_exception_then_return_values
|
227
244
|
expectation = new_expectation.raises.then.returns(1, 2)
|
228
|
-
|
229
|
-
assert_equal 1, expectation
|
230
|
-
assert_equal 2, expectation
|
245
|
+
assert_raises(RuntimeError) { invoke(expectation) }
|
246
|
+
assert_equal 1, invoke(expectation)
|
247
|
+
assert_equal 2, invoke(expectation)
|
231
248
|
end
|
232
249
|
|
233
250
|
def test_should_verify_successfully_if_expected_call_was_made
|
234
251
|
expectation = new_expectation
|
235
|
-
expectation
|
252
|
+
invoke(expectation)
|
236
253
|
assert expectation.verified?
|
237
254
|
end
|
238
255
|
|
239
256
|
def test_should_not_verify_successfully_if_call_expected_once_but_invoked_twice
|
240
257
|
expectation = new_expectation.once
|
241
|
-
expectation
|
242
|
-
expectation
|
258
|
+
invoke(expectation)
|
259
|
+
invoke(expectation)
|
243
260
|
assert !expectation.verified?
|
244
261
|
end
|
245
262
|
|
246
263
|
def test_should_not_verify_successfully_if_call_expected_once_but_not_invoked
|
247
|
-
|
248
|
-
assert !expectation.verified?
|
264
|
+
assert !new_expectation.once.verified?
|
249
265
|
end
|
250
266
|
|
251
267
|
def test_should_verify_successfully_if_call_expected_once_and_invoked_once
|
252
268
|
expectation = new_expectation.once
|
253
|
-
expectation
|
269
|
+
invoke(expectation)
|
254
270
|
assert expectation.verified?
|
255
271
|
end
|
256
272
|
|
257
273
|
def test_should_not_verify_successfully_if_call_expected_twice_and_invoked_three_times
|
258
274
|
expectation = new_expectation.twice
|
259
|
-
expectation
|
260
|
-
expectation
|
261
|
-
expectation
|
275
|
+
invoke(expectation)
|
276
|
+
invoke(expectation)
|
277
|
+
invoke(expectation)
|
262
278
|
assert !expectation.verified?
|
263
279
|
end
|
264
280
|
|
265
281
|
def test_should_not_verify_successfully_if_call_expected_twice_but_invoked_once
|
266
282
|
expectation = new_expectation.twice
|
267
|
-
expectation
|
283
|
+
invoke(expectation)
|
268
284
|
assert !expectation.verified?
|
269
285
|
end
|
270
286
|
|
271
287
|
def test_should_verify_successfully_if_call_expected_twice_and_invoked_twice
|
272
288
|
expectation = new_expectation.twice
|
273
|
-
expectation
|
274
|
-
expectation
|
289
|
+
invoke(expectation)
|
290
|
+
invoke(expectation)
|
275
291
|
assert expectation.verified?
|
276
292
|
end
|
277
293
|
|
278
294
|
def test_should_verify_successfully_if_expected_call_was_made_at_least_once
|
279
295
|
expectation = new_expectation.at_least_once
|
280
|
-
3.times { expectation
|
296
|
+
3.times { invoke(expectation) }
|
281
297
|
assert expectation.verified?
|
282
298
|
end
|
283
299
|
|
284
300
|
def test_should_not_verify_successfully_if_expected_call_was_not_made_at_least_once
|
285
301
|
expectation = new_expectation.with(1, 2, 3).at_least_once
|
286
302
|
assert !expectation.verified?
|
287
|
-
assert_match(/expected at least once,
|
303
|
+
assert_match(/expected at least once, invoked never/i, expectation.mocha_inspect)
|
288
304
|
end
|
289
305
|
|
290
306
|
def test_should_verify_successfully_if_expected_call_was_made_expected_number_of_times
|
291
307
|
expectation = new_expectation.times(2)
|
292
|
-
2.times { expectation
|
308
|
+
2.times { invoke(expectation) }
|
293
309
|
assert expectation.verified?
|
294
310
|
end
|
295
311
|
|
296
312
|
def test_should_not_verify_successfully_if_expected_call_was_made_too_few_times
|
297
313
|
expectation = new_expectation.times(2)
|
298
|
-
1.times { expectation
|
314
|
+
1.times { invoke(expectation) }
|
299
315
|
assert !expectation.verified?
|
300
316
|
assert_match(/expected exactly twice, invoked once/i, expectation.mocha_inspect)
|
301
317
|
end
|
302
318
|
|
303
319
|
def test_should_not_verify_successfully_if_expected_call_was_made_too_many_times
|
304
320
|
expectation = new_expectation.times(2)
|
305
|
-
3.times { expectation
|
321
|
+
3.times { invoke(expectation) }
|
306
322
|
assert !expectation.verified?
|
307
323
|
end
|
308
324
|
|
309
325
|
def test_should_increment_assertion_counter_for_expectation_because_it_does_need_verifyng
|
310
326
|
expectation = new_expectation
|
311
|
-
expectation
|
327
|
+
invoke(expectation)
|
312
328
|
assertion_counter = SimpleCounter.new
|
313
329
|
expectation.verified?(assertion_counter)
|
314
330
|
assert_equal 1, assertion_counter.count
|
315
331
|
end
|
316
332
|
|
317
333
|
def test_should_not_increment_assertion_counter_for_stub_because_it_does_not_need_verifying
|
318
|
-
stub = Expectation.new(nil, :expected_method).at_least(0)
|
319
334
|
assertion_counter = SimpleCounter.new
|
320
|
-
|
335
|
+
Expectation.new(nil, :expected_method).at_least(0).verified?(assertion_counter)
|
321
336
|
assert_equal 0, assertion_counter.count
|
322
337
|
end
|
323
338
|
|
@@ -381,7 +396,7 @@ class ExpectationTest < Mocha::TestCase
|
|
381
396
|
expectation = Expectation.new(nil, :method_one)
|
382
397
|
expectation.add_ordering_constraint(constraint_one)
|
383
398
|
expectation.add_ordering_constraint(constraint_two)
|
384
|
-
assert expectation.match?(:method_one)
|
399
|
+
assert expectation.match?(Invocation.new(:irrelevant, :method_one))
|
385
400
|
end
|
386
401
|
|
387
402
|
def test_should_not_match_if_one_ordering_constraints_does_not_allow_invocation_now
|
@@ -390,29 +405,28 @@ class ExpectationTest < Mocha::TestCase
|
|
390
405
|
expectation = Expectation.new(nil, :method_one)
|
391
406
|
expectation.add_ordering_constraint(constraint_one)
|
392
407
|
expectation.add_ordering_constraint(constraint_two)
|
393
|
-
assert !expectation.match?(:method_one)
|
408
|
+
assert !expectation.match?(Invocation.new(:irrelevant, :method_one))
|
394
409
|
end
|
395
410
|
|
396
411
|
def test_should_not_be_satisfied_when_required_invocation_has_not_been_made
|
397
|
-
|
398
|
-
assert !expectation.satisfied?
|
412
|
+
assert !Expectation.new(nil, :method_one).times(1).satisfied?
|
399
413
|
end
|
400
414
|
|
401
415
|
def test_should_be_satisfied_when_required_invocation_has_been_made
|
402
416
|
expectation = Expectation.new(nil, :method_one).times(1)
|
403
|
-
expectation
|
417
|
+
invoke(expectation)
|
404
418
|
assert expectation.satisfied?
|
405
419
|
end
|
406
420
|
|
407
421
|
def test_should_not_be_satisfied_when_minimum_number_of_invocations_has_not_been_made
|
408
422
|
expectation = Expectation.new(nil, :method_one).at_least(2)
|
409
|
-
expectation
|
423
|
+
invoke(expectation)
|
410
424
|
assert !expectation.satisfied?
|
411
425
|
end
|
412
426
|
|
413
427
|
def test_should_be_satisfied_when_minimum_number_of_invocations_has_been_made
|
414
428
|
expectation = Expectation.new(nil, :method_one).at_least(2)
|
415
|
-
2.times { expectation
|
429
|
+
2.times { invoke(expectation) }
|
416
430
|
assert expectation.satisfied?
|
417
431
|
end
|
418
432
|
|
@@ -457,7 +471,7 @@ class ExpectationTest < Mocha::TestCase
|
|
457
471
|
|
458
472
|
expectation.then(state)
|
459
473
|
|
460
|
-
expectation
|
474
|
+
invoke(expectation)
|
461
475
|
assert state.active?
|
462
476
|
end
|
463
477
|
|
@@ -466,10 +480,10 @@ class ExpectationTest < Mocha::TestCase
|
|
466
480
|
expectation = Expectation.new(nil, :method_one)
|
467
481
|
|
468
482
|
expectation.when(state)
|
469
|
-
assert !expectation.match?(:method_one)
|
483
|
+
assert !expectation.match?(Invocation.new(:irrelevant, :method_one))
|
470
484
|
|
471
485
|
state.activate
|
472
|
-
assert expectation.match?(:method_one)
|
486
|
+
assert expectation.match?(Invocation.new(:irrelevant, :method_one))
|
473
487
|
end
|
474
488
|
|
475
489
|
def test_should_include_default_representation_of_object_in_inspect
|
@@ -477,8 +491,7 @@ class ExpectationTest < Mocha::TestCase
|
|
477
491
|
class << object
|
478
492
|
define_method(:inspect) { 'mock' }
|
479
493
|
end
|
480
|
-
|
481
|
-
assert_match Regexp.new('^#<Expectation:0x[0-9A-Fa-f]{1,12} .* >$'), expectation.inspect
|
494
|
+
assert_match Regexp.new('^#<Expectation:0x[0-9A-Fa-f]{1,12} .* >$'), Expectation.new(object, :method_one).inspect
|
482
495
|
end
|
483
496
|
|
484
497
|
def test_should_include_output_of_mocha_inspect_in_inspect
|
@@ -490,3 +503,4 @@ class ExpectationTest < Mocha::TestCase
|
|
490
503
|
assert expectation.inspect.include?(expectation.mocha_inspect)
|
491
504
|
end
|
492
505
|
end
|
506
|
+
# rubocop:enable Metrics/ClassLength
|