mocha 1.9.0 → 1.10.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/.rubocop_todo.yml +7 -30
- data/README.md +30 -11
- data/RELEASE.md +58 -0
- data/Rakefile +9 -3
- data/docs/CNAME +1 -0
- data/docs/Mocha.html +132 -5
- data/docs/Mocha/API.html +313 -216
- data/docs/Mocha/ClassMethods.html +13 -16
- data/docs/Mocha/Configuration.html +931 -174
- data/docs/Mocha/Expectation.html +200 -255
- 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 +72 -3
- data/docs/frames.html +1 -1
- data/docs/index.html +36 -14
- data/docs/method_list.html +96 -40
- 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 +65 -66
- data/lib/mocha/cardinality.rb +26 -11
- data/lib/mocha/class_methods.rb +17 -15
- data/lib/mocha/configuration.rb +295 -67
- data/lib/mocha/detection/test_unit.rb +1 -3
- data/lib/mocha/exception_raiser.rb +2 -1
- data/lib/mocha/expectation.rb +56 -58
- 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 +61 -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 +28 -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/mocha.gemspec +1 -3
- data/test/acceptance/acceptance_test_helper.rb +6 -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 +0 -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_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/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 +11 -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/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 +88 -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
- metadata +14 -28
- 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/parameter_matchers/object.rb +0 -15
- 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
@@ -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,6 +1,6 @@
|
|
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
5
|
require 'execution_point'
|
6
6
|
require 'simple_counter'
|
@@ -12,90 +12,85 @@ class ExpectationTest < Mocha::TestCase
|
|
12
12
|
Expectation.new(nil, :expected_method)
|
13
13
|
end
|
14
14
|
|
15
|
+
def invoke(expectation, &block)
|
16
|
+
expectation.invoke(Invocation.new(:irrelevant, :expected_method), &block)
|
17
|
+
end
|
18
|
+
|
15
19
|
def test_should_match_calls_to_same_method_with_any_parameters
|
16
|
-
assert new_expectation.match?(:expected_method, 1, 2, 3)
|
20
|
+
assert new_expectation.match?(Invocation.new(:irrelevant, :expected_method, 1, 2, 3))
|
17
21
|
end
|
18
22
|
|
19
23
|
def test_should_match_calls_to_same_method_with_exactly_zero_parameters
|
20
|
-
|
21
|
-
assert expectation.match?(:expected_method)
|
24
|
+
assert new_expectation.with.match?(Invocation.new(:irrelevant, :expected_method))
|
22
25
|
end
|
23
26
|
|
24
27
|
def test_should_not_match_calls_to_same_method_with_more_than_zero_parameters
|
25
|
-
|
26
|
-
assert !expectation.match?(:expected_method, 1, 2, 3)
|
28
|
+
assert !new_expectation.with.match?(Invocation.new(:irrelevant, :expected_method, 1, 2, 3))
|
27
29
|
end
|
28
30
|
|
29
31
|
def test_should_match_calls_to_same_method_with_expected_parameter_values
|
30
|
-
|
31
|
-
assert expectation.match?(:expected_method, 1, 2, 3)
|
32
|
+
assert new_expectation.with(1, 2, 3).match?(Invocation.new(:irrelevant, :expected_method, 1, 2, 3))
|
32
33
|
end
|
33
34
|
|
34
35
|
def test_should_match_calls_to_same_method_with_parameters_constrained_as_expected
|
35
36
|
expectation = new_expectation.with { |x, y, z| x + y == z }
|
36
|
-
assert expectation.match?(:expected_method, 1, 2, 3)
|
37
|
+
assert expectation.match?(Invocation.new(:irrelevant, :expected_method, 1, 2, 3))
|
37
38
|
end
|
38
39
|
|
39
40
|
def test_should_not_match_calls_to_different_method_with_parameters_constrained_as_expected
|
40
41
|
expectation = new_expectation.with { |x, y, z| x + y == z }
|
41
|
-
assert !expectation.match?(:different_method, 1, 2, 3)
|
42
|
+
assert !expectation.match?(Invocation.new(:irrelevant, :different_method, 1, 2, 3))
|
42
43
|
end
|
43
44
|
|
44
45
|
def test_should_not_match_calls_to_different_methods_with_no_parameters
|
45
|
-
assert !new_expectation.match?(:unexpected_method)
|
46
|
+
assert !new_expectation.match?(Invocation.new(:irrelevant, :unexpected_method))
|
46
47
|
end
|
47
48
|
|
48
49
|
def test_should_not_match_calls_to_same_method_with_too_few_parameters
|
49
|
-
|
50
|
-
assert !expectation.match?(:unexpected_method, 1, 2)
|
50
|
+
assert !new_expectation.with(1, 2, 3).match?(Invocation.new(:irrelevant, :expected_method, 1, 2))
|
51
51
|
end
|
52
52
|
|
53
53
|
def test_should_not_match_calls_to_same_method_with_too_many_parameters
|
54
|
-
|
55
|
-
assert !expectation.match?(:unexpected_method, 1, 2, 3)
|
54
|
+
assert !new_expectation.with(1, 2).match?(Invocation.new(:irrelevant, :expected_method, 1, 2, 3))
|
56
55
|
end
|
57
56
|
|
58
57
|
def test_should_not_match_calls_to_same_method_with_unexpected_parameter_values
|
59
|
-
|
60
|
-
assert !expectation.match?(:unexpected_method, 1, 0, 3)
|
58
|
+
assert !new_expectation.with(1, 2, 3).match?(Invocation.new(:irrelevant, :expected_method, 1, 0, 3))
|
61
59
|
end
|
62
60
|
|
63
61
|
def test_should_not_match_calls_to_same_method_with_parameters_not_constrained_as_expected
|
64
62
|
expectation = new_expectation.with { |x, y, z| x + y == z }
|
65
|
-
assert !expectation.match?(:expected_method, 1, 0, 3)
|
63
|
+
assert !expectation.match?(Invocation.new(:irrelevant, :expected_method, 1, 0, 3))
|
66
64
|
end
|
67
65
|
|
68
66
|
def test_should_allow_invocations_until_expected_invocation_count_is_one_and_actual_invocation_count_would_be_two
|
69
67
|
expectation = new_expectation.times(1)
|
70
68
|
assert expectation.invocations_allowed?
|
71
|
-
expectation
|
69
|
+
invoke(expectation)
|
72
70
|
assert !expectation.invocations_allowed?
|
73
71
|
end
|
74
72
|
|
75
73
|
def test_should_allow_invocations_until_expected_invocation_count_is_two_and_actual_invocation_count_would_be_three
|
76
74
|
expectation = new_expectation.times(2)
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
75
|
+
2.times do
|
76
|
+
assert expectation.invocations_allowed?
|
77
|
+
invoke(expectation)
|
78
|
+
end
|
81
79
|
assert !expectation.invocations_allowed?
|
82
80
|
end
|
83
81
|
|
84
82
|
def test_should_allow_invocations_until_expected_invocation_count_is_a_range_from_two_to_three_and_actual_invocation_count_would_be_four
|
85
83
|
expectation = new_expectation.times(2..3)
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
assert expectation.invocations_allowed?
|
91
|
-
expectation.invoke
|
84
|
+
3.times do
|
85
|
+
assert expectation.invocations_allowed?
|
86
|
+
invoke(expectation)
|
87
|
+
end
|
92
88
|
assert !expectation.invocations_allowed?
|
93
89
|
end
|
94
90
|
|
95
91
|
def test_should_store_provided_backtrace
|
96
92
|
backtrace = Object.new
|
97
|
-
|
98
|
-
assert_equal backtrace, expectation.backtrace
|
93
|
+
assert_equal backtrace, Expectation.new(nil, :expected_method, backtrace).backtrace
|
99
94
|
end
|
100
95
|
|
101
96
|
# rubocop:disable Style/Semicolon
|
@@ -107,217 +102,209 @@ class ExpectationTest < Mocha::TestCase
|
|
107
102
|
|
108
103
|
def test_should_not_yield
|
109
104
|
yielded = false
|
110
|
-
new_expectation
|
105
|
+
invoke(new_expectation) { yielded = true }
|
111
106
|
assert_equal false, yielded
|
112
107
|
end
|
113
108
|
|
114
109
|
def test_should_yield_no_parameters
|
115
|
-
expectation = new_expectation.yields
|
116
110
|
yielded_parameters = nil
|
117
|
-
|
111
|
+
invoke(new_expectation.yields) { |*parameters| yielded_parameters = parameters }
|
118
112
|
assert_equal [], yielded_parameters
|
119
113
|
end
|
120
114
|
|
115
|
+
def test_yield_should_fail_when_the_caller_does_not_provide_a_block
|
116
|
+
assert_raises(LocalJumpError) { invoke(new_expectation.yields(:foo)) }
|
117
|
+
end
|
118
|
+
|
121
119
|
def test_should_yield_with_specified_parameters
|
122
|
-
expectation = new_expectation.yields(1, 2, 3)
|
123
120
|
yielded_parameters = nil
|
124
|
-
|
121
|
+
invoke(new_expectation.yields(1, 2, 3)) { |*parameters| yielded_parameters = parameters }
|
125
122
|
assert_equal [1, 2, 3], yielded_parameters
|
126
123
|
end
|
127
124
|
|
128
125
|
def test_should_yield_different_parameters_on_consecutive_invocations
|
129
126
|
expectation = new_expectation.yields(1, 2, 3).yields(4, 5)
|
130
127
|
yielded_parameters = []
|
131
|
-
expectation
|
132
|
-
expectation
|
128
|
+
invoke(expectation) { |*parameters| yielded_parameters << parameters }
|
129
|
+
invoke(expectation) { |*parameters| yielded_parameters << parameters }
|
133
130
|
assert_equal [[1, 2, 3], [4, 5]], yielded_parameters
|
134
131
|
end
|
135
132
|
|
136
133
|
def test_should_yield_multiple_times_for_single_invocation
|
137
|
-
expectation = new_expectation.multiple_yields([1, 2, 3], [4, 5])
|
138
134
|
yielded_parameters = []
|
139
|
-
|
135
|
+
invoke(new_expectation.multiple_yields([1, 2, 3], [4, 5])) { |*parameters| yielded_parameters << parameters }
|
140
136
|
assert_equal [[1, 2, 3], [4, 5]], yielded_parameters
|
141
137
|
end
|
142
138
|
|
143
139
|
def test_should_yield_multiple_times_for_first_invocation_and_once_for_second_invocation
|
144
140
|
expectation = new_expectation.multiple_yields([1, 2, 3], [4, 5]).then.yields(6, 7)
|
145
141
|
yielded_parameters = []
|
146
|
-
expectation
|
147
|
-
expectation
|
142
|
+
invoke(expectation) { |*parameters| yielded_parameters << parameters }
|
143
|
+
invoke(expectation) { |*parameters| yielded_parameters << parameters }
|
148
144
|
assert_equal [[1, 2, 3], [4, 5], [6, 7]], yielded_parameters
|
149
145
|
end
|
150
146
|
|
151
147
|
def test_should_return_specified_value
|
152
|
-
|
153
|
-
assert_equal 99, expectation.invoke
|
148
|
+
assert_equal 99, invoke(new_expectation.returns(99))
|
154
149
|
end
|
155
150
|
|
156
151
|
def test_should_return_same_specified_value_multiple_times
|
157
152
|
expectation = new_expectation.returns(99)
|
158
|
-
assert_equal 99, expectation
|
159
|
-
assert_equal 99, expectation
|
153
|
+
assert_equal 99, invoke(expectation)
|
154
|
+
assert_equal 99, invoke(expectation)
|
160
155
|
end
|
161
156
|
|
162
157
|
def test_should_return_specified_values_on_consecutive_calls
|
163
158
|
expectation = new_expectation.returns(99, 100, 101)
|
164
|
-
assert_equal 99, expectation
|
165
|
-
assert_equal 100, expectation
|
166
|
-
assert_equal 101, expectation
|
159
|
+
assert_equal 99, invoke(expectation)
|
160
|
+
assert_equal 100, invoke(expectation)
|
161
|
+
assert_equal 101, invoke(expectation)
|
167
162
|
end
|
168
163
|
|
169
164
|
def test_should_return_specified_values_on_consecutive_calls_even_if_values_are_modified
|
170
165
|
values = [99, 100, 101]
|
171
166
|
expectation = new_expectation.returns(*values)
|
172
167
|
values.shift
|
173
|
-
assert_equal 99, expectation
|
174
|
-
assert_equal 100, expectation
|
175
|
-
assert_equal 101, expectation
|
168
|
+
assert_equal 99, invoke(expectation)
|
169
|
+
assert_equal 100, invoke(expectation)
|
170
|
+
assert_equal 101, invoke(expectation)
|
176
171
|
end
|
177
172
|
|
178
173
|
def test_should_return_nil_by_default
|
179
|
-
assert_nil new_expectation
|
174
|
+
assert_nil invoke(new_expectation)
|
180
175
|
end
|
181
176
|
|
182
177
|
def test_should_return_nil_if_no_value_specified
|
183
|
-
|
184
|
-
assert_nil expectation.invoke
|
178
|
+
assert_nil invoke(new_expectation.returns)
|
185
179
|
end
|
186
180
|
|
187
181
|
def test_should_raise_runtime_exception
|
188
|
-
|
189
|
-
assert_raise(RuntimeError) { expectation.invoke }
|
182
|
+
assert_raises(RuntimeError) { invoke(new_expectation.raises) }
|
190
183
|
end
|
191
184
|
|
192
185
|
def test_should_raise_custom_exception
|
193
186
|
exception = Class.new(Exception)
|
194
|
-
|
195
|
-
assert_raise(exception) { expectation.invoke }
|
187
|
+
assert_raises(exception) { invoke(new_expectation.raises(exception)) }
|
196
188
|
end
|
197
189
|
|
198
190
|
def test_should_raise_same_instance_of_custom_exception
|
199
191
|
exception_klass = Class.new(StandardError)
|
200
192
|
expected_exception = exception_klass.new
|
201
|
-
|
202
|
-
actual_exception = assert_raise(exception_klass) { expectation.invoke }
|
193
|
+
actual_exception = assert_raises(exception_klass) { invoke(new_expectation.raises(expected_exception)) }
|
203
194
|
assert_same expected_exception, actual_exception
|
204
195
|
end
|
205
196
|
|
206
197
|
def test_should_use_the_default_exception_message
|
207
|
-
|
208
|
-
exception = assert_raise(Exception) { expectation.invoke }
|
198
|
+
exception = assert_raises(Exception) { invoke(new_expectation.raises(Exception)) }
|
209
199
|
assert_equal Exception.new.message, exception.message
|
210
200
|
end
|
211
201
|
|
212
202
|
def test_should_raise_custom_exception_with_message
|
213
203
|
exception_msg = 'exception message'
|
214
|
-
|
215
|
-
exception = assert_raise(Exception) { expectation.invoke }
|
204
|
+
exception = assert_raises(Exception) { invoke(new_expectation.raises(Exception, exception_msg)) }
|
216
205
|
assert_equal exception_msg, exception.message
|
217
206
|
end
|
218
207
|
|
219
208
|
def test_should_return_values_then_raise_exception
|
220
209
|
expectation = new_expectation.returns(1, 2).then.raises
|
221
|
-
assert_equal 1, expectation
|
222
|
-
assert_equal 2, expectation
|
223
|
-
|
210
|
+
assert_equal 1, invoke(expectation)
|
211
|
+
assert_equal 2, invoke(expectation)
|
212
|
+
assert_raises(RuntimeError) { invoke(expectation) }
|
224
213
|
end
|
225
214
|
|
226
215
|
def test_should_raise_exception_then_return_values
|
227
216
|
expectation = new_expectation.raises.then.returns(1, 2)
|
228
|
-
|
229
|
-
assert_equal 1, expectation
|
230
|
-
assert_equal 2, expectation
|
217
|
+
assert_raises(RuntimeError) { invoke(expectation) }
|
218
|
+
assert_equal 1, invoke(expectation)
|
219
|
+
assert_equal 2, invoke(expectation)
|
231
220
|
end
|
232
221
|
|
233
222
|
def test_should_verify_successfully_if_expected_call_was_made
|
234
223
|
expectation = new_expectation
|
235
|
-
expectation
|
224
|
+
invoke(expectation)
|
236
225
|
assert expectation.verified?
|
237
226
|
end
|
238
227
|
|
239
228
|
def test_should_not_verify_successfully_if_call_expected_once_but_invoked_twice
|
240
229
|
expectation = new_expectation.once
|
241
|
-
expectation
|
242
|
-
expectation
|
230
|
+
invoke(expectation)
|
231
|
+
invoke(expectation)
|
243
232
|
assert !expectation.verified?
|
244
233
|
end
|
245
234
|
|
246
235
|
def test_should_not_verify_successfully_if_call_expected_once_but_not_invoked
|
247
|
-
|
248
|
-
assert !expectation.verified?
|
236
|
+
assert !new_expectation.once.verified?
|
249
237
|
end
|
250
238
|
|
251
239
|
def test_should_verify_successfully_if_call_expected_once_and_invoked_once
|
252
240
|
expectation = new_expectation.once
|
253
|
-
expectation
|
241
|
+
invoke(expectation)
|
254
242
|
assert expectation.verified?
|
255
243
|
end
|
256
244
|
|
257
245
|
def test_should_not_verify_successfully_if_call_expected_twice_and_invoked_three_times
|
258
246
|
expectation = new_expectation.twice
|
259
|
-
expectation
|
260
|
-
expectation
|
261
|
-
expectation
|
247
|
+
invoke(expectation)
|
248
|
+
invoke(expectation)
|
249
|
+
invoke(expectation)
|
262
250
|
assert !expectation.verified?
|
263
251
|
end
|
264
252
|
|
265
253
|
def test_should_not_verify_successfully_if_call_expected_twice_but_invoked_once
|
266
254
|
expectation = new_expectation.twice
|
267
|
-
expectation
|
255
|
+
invoke(expectation)
|
268
256
|
assert !expectation.verified?
|
269
257
|
end
|
270
258
|
|
271
259
|
def test_should_verify_successfully_if_call_expected_twice_and_invoked_twice
|
272
260
|
expectation = new_expectation.twice
|
273
|
-
expectation
|
274
|
-
expectation
|
261
|
+
invoke(expectation)
|
262
|
+
invoke(expectation)
|
275
263
|
assert expectation.verified?
|
276
264
|
end
|
277
265
|
|
278
266
|
def test_should_verify_successfully_if_expected_call_was_made_at_least_once
|
279
267
|
expectation = new_expectation.at_least_once
|
280
|
-
3.times { expectation
|
268
|
+
3.times { invoke(expectation) }
|
281
269
|
assert expectation.verified?
|
282
270
|
end
|
283
271
|
|
284
272
|
def test_should_not_verify_successfully_if_expected_call_was_not_made_at_least_once
|
285
273
|
expectation = new_expectation.with(1, 2, 3).at_least_once
|
286
274
|
assert !expectation.verified?
|
287
|
-
assert_match(/expected at least once,
|
275
|
+
assert_match(/expected at least once, invoked never/i, expectation.mocha_inspect)
|
288
276
|
end
|
289
277
|
|
290
278
|
def test_should_verify_successfully_if_expected_call_was_made_expected_number_of_times
|
291
279
|
expectation = new_expectation.times(2)
|
292
|
-
2.times { expectation
|
280
|
+
2.times { invoke(expectation) }
|
293
281
|
assert expectation.verified?
|
294
282
|
end
|
295
283
|
|
296
284
|
def test_should_not_verify_successfully_if_expected_call_was_made_too_few_times
|
297
285
|
expectation = new_expectation.times(2)
|
298
|
-
1.times { expectation
|
286
|
+
1.times { invoke(expectation) }
|
299
287
|
assert !expectation.verified?
|
300
288
|
assert_match(/expected exactly twice, invoked once/i, expectation.mocha_inspect)
|
301
289
|
end
|
302
290
|
|
303
291
|
def test_should_not_verify_successfully_if_expected_call_was_made_too_many_times
|
304
292
|
expectation = new_expectation.times(2)
|
305
|
-
3.times { expectation
|
293
|
+
3.times { invoke(expectation) }
|
306
294
|
assert !expectation.verified?
|
307
295
|
end
|
308
296
|
|
309
297
|
def test_should_increment_assertion_counter_for_expectation_because_it_does_need_verifyng
|
310
298
|
expectation = new_expectation
|
311
|
-
expectation
|
299
|
+
invoke(expectation)
|
312
300
|
assertion_counter = SimpleCounter.new
|
313
301
|
expectation.verified?(assertion_counter)
|
314
302
|
assert_equal 1, assertion_counter.count
|
315
303
|
end
|
316
304
|
|
317
305
|
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
306
|
assertion_counter = SimpleCounter.new
|
320
|
-
|
307
|
+
Expectation.new(nil, :expected_method).at_least(0).verified?(assertion_counter)
|
321
308
|
assert_equal 0, assertion_counter.count
|
322
309
|
end
|
323
310
|
|
@@ -381,7 +368,7 @@ class ExpectationTest < Mocha::TestCase
|
|
381
368
|
expectation = Expectation.new(nil, :method_one)
|
382
369
|
expectation.add_ordering_constraint(constraint_one)
|
383
370
|
expectation.add_ordering_constraint(constraint_two)
|
384
|
-
assert expectation.match?(:method_one)
|
371
|
+
assert expectation.match?(Invocation.new(:irrelevant, :method_one))
|
385
372
|
end
|
386
373
|
|
387
374
|
def test_should_not_match_if_one_ordering_constraints_does_not_allow_invocation_now
|
@@ -390,29 +377,28 @@ class ExpectationTest < Mocha::TestCase
|
|
390
377
|
expectation = Expectation.new(nil, :method_one)
|
391
378
|
expectation.add_ordering_constraint(constraint_one)
|
392
379
|
expectation.add_ordering_constraint(constraint_two)
|
393
|
-
assert !expectation.match?(:method_one)
|
380
|
+
assert !expectation.match?(Invocation.new(:irrelevant, :method_one))
|
394
381
|
end
|
395
382
|
|
396
383
|
def test_should_not_be_satisfied_when_required_invocation_has_not_been_made
|
397
|
-
|
398
|
-
assert !expectation.satisfied?
|
384
|
+
assert !Expectation.new(nil, :method_one).times(1).satisfied?
|
399
385
|
end
|
400
386
|
|
401
387
|
def test_should_be_satisfied_when_required_invocation_has_been_made
|
402
388
|
expectation = Expectation.new(nil, :method_one).times(1)
|
403
|
-
expectation
|
389
|
+
invoke(expectation)
|
404
390
|
assert expectation.satisfied?
|
405
391
|
end
|
406
392
|
|
407
393
|
def test_should_not_be_satisfied_when_minimum_number_of_invocations_has_not_been_made
|
408
394
|
expectation = Expectation.new(nil, :method_one).at_least(2)
|
409
|
-
expectation
|
395
|
+
invoke(expectation)
|
410
396
|
assert !expectation.satisfied?
|
411
397
|
end
|
412
398
|
|
413
399
|
def test_should_be_satisfied_when_minimum_number_of_invocations_has_been_made
|
414
400
|
expectation = Expectation.new(nil, :method_one).at_least(2)
|
415
|
-
2.times { expectation
|
401
|
+
2.times { invoke(expectation) }
|
416
402
|
assert expectation.satisfied?
|
417
403
|
end
|
418
404
|
|
@@ -457,7 +443,7 @@ class ExpectationTest < Mocha::TestCase
|
|
457
443
|
|
458
444
|
expectation.then(state)
|
459
445
|
|
460
|
-
expectation
|
446
|
+
invoke(expectation)
|
461
447
|
assert state.active?
|
462
448
|
end
|
463
449
|
|
@@ -466,10 +452,10 @@ class ExpectationTest < Mocha::TestCase
|
|
466
452
|
expectation = Expectation.new(nil, :method_one)
|
467
453
|
|
468
454
|
expectation.when(state)
|
469
|
-
assert !expectation.match?(:method_one)
|
455
|
+
assert !expectation.match?(Invocation.new(:irrelevant, :method_one))
|
470
456
|
|
471
457
|
state.activate
|
472
|
-
assert expectation.match?(:method_one)
|
458
|
+
assert expectation.match?(Invocation.new(:irrelevant, :method_one))
|
473
459
|
end
|
474
460
|
|
475
461
|
def test_should_include_default_representation_of_object_in_inspect
|
@@ -477,8 +463,7 @@ class ExpectationTest < Mocha::TestCase
|
|
477
463
|
class << object
|
478
464
|
define_method(:inspect) { 'mock' }
|
479
465
|
end
|
480
|
-
|
481
|
-
assert_match Regexp.new('^#<Expectation:0x[0-9A-Fa-f]{1,12} .* >$'), expectation.inspect
|
466
|
+
assert_match Regexp.new('^#<Expectation:0x[0-9A-Fa-f]{1,12} .* >$'), Expectation.new(object, :method_one).inspect
|
482
467
|
end
|
483
468
|
|
484
469
|
def test_should_include_output_of_mocha_inspect_in_inspect
|