mocha 1.4.0 → 1.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/.rubocop.yml +58 -0
- data/.rubocop_todo.yml +50 -0
- data/README.md +12 -71
- data/RELEASE.md +19 -0
- data/Rakefile +35 -28
- data/bin/build-matrix +17 -18
- data/lib/mocha/any_instance_method.rb +19 -25
- data/lib/mocha/api.rb +1 -5
- data/lib/mocha/argument_iterator.rb +4 -8
- data/lib/mocha/backtrace_filter.rb +1 -5
- data/lib/mocha/cardinality.rb +20 -27
- data/lib/mocha/central.rb +21 -12
- data/lib/mocha/change_state_side_effect.rb +0 -4
- data/lib/mocha/class_method.rb +31 -37
- data/lib/mocha/class_methods.rb +11 -15
- data/lib/mocha/configuration.rb +69 -10
- data/lib/mocha/debug.rb +3 -2
- data/lib/mocha/deprecation.rb +4 -11
- data/lib/mocha/detection/mini_test.rb +0 -2
- data/lib/mocha/detection/test_unit.rb +4 -4
- data/lib/mocha/error_with_filtered_backtrace.rb +13 -0
- data/lib/mocha/exception_raiser.rb +2 -5
- data/lib/mocha/expectation.rb +15 -18
- data/lib/mocha/expectation_error.rb +2 -0
- data/lib/mocha/expectation_error_factory.rb +0 -1
- data/lib/mocha/expectation_list.rb +2 -6
- data/lib/mocha/hooks.rb +1 -3
- data/lib/mocha/in_state_ordering_constraint.rb +0 -4
- data/lib/mocha/inspect.rb +3 -5
- data/lib/mocha/instance_method.rb +0 -2
- data/lib/mocha/integration/mini_test/adapter.rb +2 -4
- data/lib/mocha/integration/mini_test/exception_translation.rb +1 -1
- data/lib/mocha/integration/mini_test/nothing.rb +4 -4
- data/lib/mocha/integration/mini_test/version_13.rb +4 -1
- data/lib/mocha/integration/mini_test/version_140.rb +4 -1
- data/lib/mocha/integration/mini_test/version_141.rb +4 -1
- data/lib/mocha/integration/mini_test/version_142_to_172.rb +4 -1
- data/lib/mocha/integration/mini_test/version_200.rb +4 -1
- data/lib/mocha/integration/mini_test/version_201_to_222.rb +4 -1
- data/lib/mocha/integration/mini_test/version_2110_to_2111.rb +4 -1
- data/lib/mocha/integration/mini_test/version_2112_to_320.rb +4 -1
- data/lib/mocha/integration/mini_test/version_230_to_2101.rb +4 -1
- data/lib/mocha/integration/monkey_patcher.rb +5 -7
- data/lib/mocha/integration/test_unit/adapter.rb +5 -6
- data/lib/mocha/integration/test_unit/gem_version_200.rb +5 -2
- data/lib/mocha/integration/test_unit/gem_version_201_to_202.rb +5 -2
- data/lib/mocha/integration/test_unit/gem_version_203_to_220.rb +5 -2
- data/lib/mocha/integration/test_unit/gem_version_230_to_250.rb +5 -2
- data/lib/mocha/integration/test_unit/nothing.rb +4 -4
- data/lib/mocha/integration/test_unit/ruby_version_185_and_below.rb +4 -1
- data/lib/mocha/integration/test_unit/ruby_version_186_and_above.rb +4 -1
- data/lib/mocha/integration.rb +3 -4
- data/lib/mocha/is_a.rb +0 -2
- data/lib/mocha/logger.rb +0 -4
- data/lib/mocha/method_matcher.rb +1 -5
- data/lib/mocha/minitest.rb +1 -1
- data/lib/mocha/mock.rb +25 -30
- data/lib/mocha/mockery.rb +62 -43
- data/lib/mocha/module_method.rb +0 -10
- data/lib/mocha/module_methods.rb +0 -4
- data/lib/mocha/multiple_yields.rb +0 -5
- data/lib/mocha/names.rb +1 -11
- data/lib/mocha/no_yields.rb +1 -7
- data/lib/mocha/not_initialized_error.rb +7 -0
- data/lib/mocha/object_methods.rb +16 -16
- data/lib/mocha/parameter_matchers/all_of.rb +1 -7
- data/lib/mocha/parameter_matchers/any_of.rb +1 -7
- data/lib/mocha/parameter_matchers/any_parameters.rb +3 -9
- data/lib/mocha/parameter_matchers/anything.rb +2 -8
- data/lib/mocha/parameter_matchers/base.rb +6 -12
- data/lib/mocha/parameter_matchers/equals.rb +0 -6
- data/lib/mocha/parameter_matchers/equivalent_uri.rb +6 -6
- data/lib/mocha/parameter_matchers/has_entries.rb +2 -6
- data/lib/mocha/parameter_matchers/has_entry.rb +8 -11
- data/lib/mocha/parameter_matchers/has_key.rb +2 -6
- data/lib/mocha/parameter_matchers/has_value.rb +2 -6
- data/lib/mocha/parameter_matchers/includes.rb +2 -6
- data/lib/mocha/parameter_matchers/instance_of.rb +0 -6
- data/lib/mocha/parameter_matchers/is_a.rb +2 -6
- data/lib/mocha/parameter_matchers/kind_of.rb +2 -6
- data/lib/mocha/parameter_matchers/not.rb +2 -6
- data/lib/mocha/parameter_matchers/object.rb +0 -2
- data/lib/mocha/parameter_matchers/optionally.rb +4 -10
- data/lib/mocha/parameter_matchers/regexp_matches.rb +0 -6
- data/lib/mocha/parameter_matchers/responds_with.rb +3 -8
- data/lib/mocha/parameter_matchers/yaml_equivalent.rb +2 -6
- data/lib/mocha/parameter_matchers.rb +0 -2
- data/lib/mocha/parameters_matcher.rb +6 -9
- data/lib/mocha/pretty_parameters.rb +0 -4
- data/lib/mocha/receivers.rb +10 -14
- data/lib/mocha/return_values.rb +3 -7
- data/lib/mocha/sequence.rb +4 -9
- data/lib/mocha/single_return_value.rb +0 -4
- data/lib/mocha/single_yield.rb +0 -5
- data/lib/mocha/state_machine.rb +6 -10
- data/lib/mocha/stubbing_error.rb +2 -13
- data/lib/mocha/test_unit.rb +1 -1
- data/lib/mocha/thrower.rb +2 -5
- data/lib/mocha/unexpected_invocation.rb +3 -5
- data/lib/mocha/version.rb +1 -1
- data/lib/mocha/yield_parameters.rb +3 -7
- data/mocha.gemspec +34 -58
- data/test/acceptance/acceptance_test_helper.rb +0 -6
- data/test/acceptance/bug_18914_test.rb +7 -12
- data/test/acceptance/bug_21465_test.rb +0 -2
- data/test/acceptance/bug_21563_test.rb +0 -2
- data/test/acceptance/exception_rescue_test.rb +7 -8
- data/test/acceptance/expectations_on_multiple_methods_test.rb +2 -1
- data/test/acceptance/expected_invocation_count_test.rb +27 -29
- data/test/acceptance/failure_messages_test.rb +3 -5
- data/test/acceptance/issue_272_test.rb +0 -1
- data/test/acceptance/issue_65_test.rb +15 -13
- data/test/acceptance/issue_70_test.rb +0 -1
- data/test/acceptance/mocha_example_test.rb +5 -6
- data/test/acceptance/mocha_test_result_test.rb +7 -7
- data/test/acceptance/mock_test.rb +2 -4
- data/test/acceptance/mock_with_initializer_block_test.rb +0 -2
- data/test/acceptance/mocked_methods_dispatch_test.rb +0 -2
- data/test/acceptance/multiple_expectations_failure_message_test.rb +16 -17
- data/test/acceptance/optional_parameters_test.rb +0 -2
- data/test/acceptance/parameter_matcher_test.rb +2 -3
- data/test/acceptance/partial_mocks_test.rb +4 -6
- data/test/acceptance/prepend_test.rb +14 -16
- data/test/acceptance/prevent_use_of_mocha_outside_test_test.rb +77 -0
- data/test/acceptance/raise_exception_test.rb +2 -4
- data/test/acceptance/return_value_test.rb +0 -2
- data/test/acceptance/sequence_test.rb +9 -11
- data/test/acceptance/states_test.rb +6 -4
- data/test/acceptance/stub_any_instance_method_defined_on_superclass_test.rb +33 -2
- data/test/acceptance/stub_any_instance_method_test.rb +19 -17
- data/test/acceptance/stub_class_method_defined_on_active_record_association_proxy_test.rb +8 -8
- data/test/acceptance/stub_class_method_defined_on_class_test.rb +2 -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 +34 -1
- data/test/acceptance/stub_everything_test.rb +1 -3
- data/test/acceptance/stub_instance_method_defined_on_active_record_association_proxy_test.rb +6 -7
- 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 +63 -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 +2 -1
- data/test/acceptance/stub_instance_method_defined_on_singleton_class_test.rb +0 -2
- data/test/acceptance/stub_instance_method_defined_on_superclass_test.rb +0 -1
- data/test/acceptance/stub_module_method_test.rb +13 -13
- data/test/acceptance/stub_test.rb +1 -3
- data/test/acceptance/stubba_example_test.rb +6 -14
- data/test/acceptance/stubba_test_result_test.rb +5 -8
- data/test/acceptance/stubbing_error_backtrace_test.rb +2 -2
- data/test/acceptance/stubbing_frozen_object_test.rb +2 -1
- data/test/acceptance/stubbing_method_accepting_block_parameter_test.rb +12 -7
- data/test/acceptance/stubbing_method_unnecessarily_test.rb +0 -2
- data/test/acceptance/stubbing_nil_test.rb +4 -5
- data/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +1 -3
- data/test/acceptance/stubbing_non_existent_class_method_test.rb +3 -3
- data/test/acceptance/stubbing_non_existent_instance_method_test.rb +1 -3
- data/test/acceptance/stubbing_non_public_any_instance_method_test.rb +0 -2
- data/test/acceptance/stubbing_non_public_class_method_test.rb +3 -3
- data/test/acceptance/stubbing_non_public_instance_method_test.rb +1 -3
- data/test/acceptance/stubbing_on_non_mock_object_test.rb +4 -10
- data/test/acceptance/stubbing_same_class_method_on_parent_and_child_classes_test.rb +3 -4
- data/test/acceptance/throw_test.rb +0 -2
- data/test/acceptance/unexpected_invocation_test.rb +2 -3
- data/test/acceptance/unstubbing_test.rb +41 -14
- data/test/deprecation_disabler.rb +0 -1
- data/test/execution_point.rb +2 -4
- data/test/integration/mini_test_test.rb +2 -2
- data/test/integration/shared_tests.rb +24 -22
- data/test/integration/test_unit_test.rb +2 -2
- data/test/method_definer.rb +1 -3
- data/test/mini_test_result.rb +17 -11
- data/test/minitest_result.rb +0 -1
- data/test/simple_counter.rb +0 -2
- data/test/test_helper.rb +13 -5
- data/test/test_runner.rb +2 -4
- data/test/test_unit_result.rb +4 -2
- data/test/unit/any_instance_method_test.rb +31 -17
- data/test/unit/array_inspect_test.rb +2 -4
- data/test/unit/backtrace_filter_test.rb +3 -5
- data/test/unit/cardinality_test.rb +0 -2
- data/test/unit/central_test.rb +26 -27
- data/test/unit/change_state_side_effect_test.rb +0 -4
- data/test/unit/class_method_test.rb +59 -34
- data/test/unit/class_methods_test.rb +34 -5
- data/test/unit/configuration_test.rb +1 -2
- data/test/unit/date_time_inspect_test.rb +1 -3
- data/test/unit/exception_raiser_test.rb +0 -2
- data/test/unit/expectation_list_test.rb +0 -2
- data/test/unit/expectation_test.rb +41 -46
- data/test/unit/hash_inspect_test.rb +3 -5
- data/test/unit/hooks_test.rb +14 -8
- data/test/unit/in_state_ordering_constraint_test.rb +0 -4
- data/test/unit/method_matcher_test.rb +1 -3
- data/test/unit/mock_test.rb +35 -22
- data/test/unit/mockery_test.rb +45 -28
- data/test/unit/module_methods_test.rb +0 -2
- data/test/unit/multiple_yields_test.rb +0 -2
- data/test/unit/no_yields_test.rb +0 -2
- data/test/unit/object_inspect_test.rb +16 -7
- data/test/unit/object_methods_test.rb +22 -6
- data/test/unit/parameter_matchers/all_of_test.rb +0 -2
- data/test/unit/parameter_matchers/any_of_test.rb +0 -2
- data/test/unit/parameter_matchers/anything_test.rb +2 -4
- data/test/unit/parameter_matchers/equals_test.rb +1 -3
- data/test/unit/parameter_matchers/equivalent_uri_test.rb +0 -1
- data/test/unit/parameter_matchers/has_entries_test.rb +2 -2
- data/test/unit/parameter_matchers/has_entry_test.rb +13 -14
- data/test/unit/parameter_matchers/has_key_test.rb +0 -1
- data/test/unit/parameter_matchers/has_value_test.rb +0 -2
- data/test/unit/parameter_matchers/includes_test.rb +8 -9
- data/test/unit/parameter_matchers/instance_of_test.rb +1 -3
- data/test/unit/parameter_matchers/is_a_test.rb +1 -3
- data/test/unit/parameter_matchers/kind_of_test.rb +1 -3
- data/test/unit/parameter_matchers/not_test.rb +0 -2
- data/test/unit/parameter_matchers/regexp_matches_test.rb +1 -2
- data/test/unit/parameter_matchers/responds_with_test.rb +9 -3
- data/test/unit/parameter_matchers/stub_matcher.rb +0 -4
- data/test/unit/parameter_matchers/yaml_equivalent_test.rb +1 -3
- data/test/unit/parameters_matcher_test.rb +2 -4
- data/test/unit/receivers_test.rb +35 -5
- data/test/unit/return_values_test.rb +3 -5
- data/test/unit/sequence_test.rb +1 -5
- data/test/unit/single_return_value_test.rb +0 -2
- data/test/unit/single_yield_test.rb +0 -2
- data/test/unit/state_machine_test.rb +1 -3
- data/test/unit/string_inspect_test.rb +2 -4
- data/test/unit/thrower_test.rb +0 -2
- data/test/unit/yield_parameters_test.rb +0 -2
- data/yard-templates/default/layout/html/setup.rb +2 -3
- metadata +25 -6
@@ -1,5 +1,5 @@
|
|
1
1
|
require File.expand_path('../../test_helper', __FILE__)
|
2
|
-
require
|
2
|
+
require 'mocha/configuration'
|
3
3
|
|
4
4
|
class ConfigurationTest < Mocha::TestCase
|
5
5
|
def test_allow_temporarily_changes_config_when_given_block
|
@@ -35,4 +35,3 @@ class ConfigurationTest < Mocha::TestCase
|
|
35
35
|
assert Mocha::Configuration.allow?(:stubbing_method_unnecessarily)
|
36
36
|
end
|
37
37
|
end
|
38
|
-
|
@@ -2,7 +2,6 @@ require File.expand_path('../../test_helper', __FILE__)
|
|
2
2
|
require 'mocha/inspect'
|
3
3
|
|
4
4
|
class DateTimeInspectTest < Mocha::TestCase
|
5
|
-
|
6
5
|
def test_should_use_include_date_in_seconds
|
7
6
|
time = Time.now
|
8
7
|
assert_equal "#{time.inspect} (#{time.to_f} secs)", time.mocha_inspect
|
@@ -14,8 +13,7 @@ class DateTimeInspectTest < Mocha::TestCase
|
|
14
13
|
end
|
15
14
|
|
16
15
|
def test_should_use_to_s_for_datetime
|
17
|
-
datetime = DateTime.new(2006, 1, 1)
|
16
|
+
datetime = DateTime.new(2006, 1, 1) # rubocop:disable Style/DateTime
|
18
17
|
assert_equal datetime.to_s, datetime.mocha_inspect
|
19
18
|
end
|
20
|
-
|
21
19
|
end
|
@@ -4,7 +4,6 @@ require 'mocha/exception_raiser'
|
|
4
4
|
require 'timeout'
|
5
5
|
|
6
6
|
class ExceptionRaiserTest < Mocha::TestCase
|
7
|
-
|
8
7
|
include Mocha
|
9
8
|
|
10
9
|
def test_should_raise_exception_with_specified_class_and_default_message
|
@@ -38,5 +37,4 @@ class ExceptionRaiserTest < Mocha::TestCase
|
|
38
37
|
raiser = ExceptionRaiser.new(exception_class, nil)
|
39
38
|
assert_raises(exception_class) { raiser.evaluate }
|
40
39
|
end
|
41
|
-
|
42
40
|
end
|
@@ -5,7 +5,6 @@ require 'set'
|
|
5
5
|
require 'method_definer'
|
6
6
|
|
7
7
|
class ExpectationListTest < Mocha::TestCase
|
8
|
-
|
9
8
|
include Mocha
|
10
9
|
|
11
10
|
def test_should_return_added_expectation
|
@@ -78,5 +77,4 @@ class ExpectationListTest < Mocha::TestCase
|
|
78
77
|
expectation_list = expectation_list1 + expectation_list2
|
79
78
|
assert_equal [expectation1, expectation2], expectation_list.to_a
|
80
79
|
end
|
81
|
-
|
82
80
|
end
|
@@ -6,7 +6,6 @@ require 'execution_point'
|
|
6
6
|
require 'simple_counter'
|
7
7
|
|
8
8
|
class ExpectationTest < Mocha::TestCase
|
9
|
-
|
10
9
|
include Mocha
|
11
10
|
|
12
11
|
def new_expectation
|
@@ -18,12 +17,12 @@ class ExpectationTest < Mocha::TestCase
|
|
18
17
|
end
|
19
18
|
|
20
19
|
def test_should_match_calls_to_same_method_with_exactly_zero_parameters
|
21
|
-
expectation = new_expectation.with
|
20
|
+
expectation = new_expectation.with
|
22
21
|
assert expectation.match?(:expected_method)
|
23
22
|
end
|
24
23
|
|
25
24
|
def test_should_not_match_calls_to_same_method_with_more_than_zero_parameters
|
26
|
-
expectation = new_expectation.with
|
25
|
+
expectation = new_expectation.with
|
27
26
|
assert !expectation.match?(:expected_method, 1, 2, 3)
|
28
27
|
end
|
29
28
|
|
@@ -33,12 +32,12 @@ class ExpectationTest < Mocha::TestCase
|
|
33
32
|
end
|
34
33
|
|
35
34
|
def test_should_match_calls_to_same_method_with_parameters_constrained_as_expected
|
36
|
-
expectation = new_expectation.with
|
35
|
+
expectation = new_expectation.with { |x, y, z| x + y == z }
|
37
36
|
assert expectation.match?(:expected_method, 1, 2, 3)
|
38
37
|
end
|
39
38
|
|
40
39
|
def test_should_not_match_calls_to_different_method_with_parameters_constrained_as_expected
|
41
|
-
expectation = new_expectation.with
|
40
|
+
expectation = new_expectation.with { |x, y, z| x + y == z }
|
42
41
|
assert !expectation.match?(:different_method, 1, 2, 3)
|
43
42
|
end
|
44
43
|
|
@@ -62,7 +61,7 @@ class ExpectationTest < Mocha::TestCase
|
|
62
61
|
end
|
63
62
|
|
64
63
|
def test_should_not_match_calls_to_same_method_with_parameters_not_constrained_as_expected
|
65
|
-
expectation = new_expectation.with
|
64
|
+
expectation = new_expectation.with { |x, y, z| x + y == z }
|
66
65
|
assert !expectation.match?(:expected_method, 1, 0, 3)
|
67
66
|
end
|
68
67
|
|
@@ -99,51 +98,53 @@ class ExpectationTest < Mocha::TestCase
|
|
99
98
|
assert_equal backtrace, expectation.backtrace
|
100
99
|
end
|
101
100
|
|
101
|
+
# rubocop:disable Style/Semicolon
|
102
102
|
def test_should_default_backtrace_to_caller
|
103
103
|
execution_point = ExecutionPoint.current; expectation = Expectation.new(nil, :expected_method)
|
104
104
|
assert_equal execution_point, ExecutionPoint.new(expectation.backtrace)
|
105
105
|
end
|
106
|
+
# rubocop:enable Style/Semicolon
|
106
107
|
|
107
108
|
def test_should_not_yield
|
108
109
|
yielded = false
|
109
|
-
new_expectation.invoke
|
110
|
+
new_expectation.invoke { yielded = true }
|
110
111
|
assert_equal false, yielded
|
111
112
|
end
|
112
113
|
|
113
114
|
def test_should_yield_no_parameters
|
114
|
-
expectation = new_expectation
|
115
|
+
expectation = new_expectation.yields
|
115
116
|
yielded_parameters = nil
|
116
|
-
expectation.invoke
|
117
|
-
assert_equal
|
117
|
+
expectation.invoke { |*parameters| yielded_parameters = parameters }
|
118
|
+
assert_equal [], yielded_parameters
|
118
119
|
end
|
119
120
|
|
120
121
|
def test_should_yield_with_specified_parameters
|
121
|
-
expectation = new_expectation
|
122
|
+
expectation = new_expectation.yields(1, 2, 3)
|
122
123
|
yielded_parameters = nil
|
123
|
-
expectation.invoke
|
124
|
+
expectation.invoke { |*parameters| yielded_parameters = parameters }
|
124
125
|
assert_equal [1, 2, 3], yielded_parameters
|
125
126
|
end
|
126
127
|
|
127
128
|
def test_should_yield_different_parameters_on_consecutive_invocations
|
128
|
-
expectation = new_expectation
|
129
|
+
expectation = new_expectation.yields(1, 2, 3).yields(4, 5)
|
129
130
|
yielded_parameters = []
|
130
|
-
expectation.invoke
|
131
|
-
expectation.invoke
|
131
|
+
expectation.invoke { |*parameters| yielded_parameters << parameters }
|
132
|
+
expectation.invoke { |*parameters| yielded_parameters << parameters }
|
132
133
|
assert_equal [[1, 2, 3], [4, 5]], yielded_parameters
|
133
134
|
end
|
134
135
|
|
135
136
|
def test_should_yield_multiple_times_for_single_invocation
|
136
|
-
expectation = new_expectation
|
137
|
+
expectation = new_expectation.multiple_yields([1, 2, 3], [4, 5])
|
137
138
|
yielded_parameters = []
|
138
|
-
expectation.invoke
|
139
|
+
expectation.invoke { |*parameters| yielded_parameters << parameters }
|
139
140
|
assert_equal [[1, 2, 3], [4, 5]], yielded_parameters
|
140
141
|
end
|
141
142
|
|
142
143
|
def test_should_yield_multiple_times_for_first_invocation_and_once_for_second_invocation
|
143
|
-
expectation = new_expectation
|
144
|
+
expectation = new_expectation.multiple_yields([1, 2, 3], [4, 5]).then.yields(6, 7)
|
144
145
|
yielded_parameters = []
|
145
|
-
expectation.invoke
|
146
|
-
expectation.invoke
|
146
|
+
expectation.invoke { |*parameters| yielded_parameters << parameters }
|
147
|
+
expectation.invoke { |*parameters| yielded_parameters << parameters }
|
147
148
|
assert_equal [[1, 2, 3], [4, 5], [6, 7]], yielded_parameters
|
148
149
|
end
|
149
150
|
|
@@ -179,7 +180,7 @@ class ExpectationTest < Mocha::TestCase
|
|
179
180
|
end
|
180
181
|
|
181
182
|
def test_should_return_nil_if_no_value_specified
|
182
|
-
expectation = new_expectation.returns
|
183
|
+
expectation = new_expectation.returns
|
183
184
|
assert_nil expectation.invoke
|
184
185
|
end
|
185
186
|
|
@@ -209,21 +210,21 @@ class ExpectationTest < Mocha::TestCase
|
|
209
210
|
end
|
210
211
|
|
211
212
|
def test_should_raise_custom_exception_with_message
|
212
|
-
exception_msg =
|
213
|
+
exception_msg = 'exception message'
|
213
214
|
expectation = new_expectation.raises(Exception, exception_msg)
|
214
215
|
exception = assert_raise(Exception) { expectation.invoke }
|
215
216
|
assert_equal exception_msg, exception.message
|
216
217
|
end
|
217
218
|
|
218
219
|
def test_should_return_values_then_raise_exception
|
219
|
-
expectation = new_expectation.returns(1, 2).then.raises
|
220
|
+
expectation = new_expectation.returns(1, 2).then.raises
|
220
221
|
assert_equal 1, expectation.invoke
|
221
222
|
assert_equal 2, expectation.invoke
|
222
223
|
assert_raise(RuntimeError) { expectation.invoke }
|
223
224
|
end
|
224
225
|
|
225
226
|
def test_should_raise_exception_then_return_values
|
226
|
-
expectation = new_expectation.raises
|
227
|
+
expectation = new_expectation.raises.then.returns(1, 2)
|
227
228
|
assert_raise(RuntimeError) { expectation.invoke }
|
228
229
|
assert_equal 1, expectation.invoke
|
229
230
|
assert_equal 2, expectation.invoke
|
@@ -276,7 +277,7 @@ class ExpectationTest < Mocha::TestCase
|
|
276
277
|
|
277
278
|
def test_should_verify_successfully_if_expected_call_was_made_at_least_once
|
278
279
|
expectation = new_expectation.at_least_once
|
279
|
-
3.times {expectation.invoke}
|
280
|
+
3.times { expectation.invoke }
|
280
281
|
assert expectation.verified?
|
281
282
|
end
|
282
283
|
|
@@ -288,20 +289,20 @@ class ExpectationTest < Mocha::TestCase
|
|
288
289
|
|
289
290
|
def test_should_verify_successfully_if_expected_call_was_made_expected_number_of_times
|
290
291
|
expectation = new_expectation.times(2)
|
291
|
-
2.times {expectation.invoke}
|
292
|
+
2.times { expectation.invoke }
|
292
293
|
assert expectation.verified?
|
293
294
|
end
|
294
295
|
|
295
296
|
def test_should_not_verify_successfully_if_expected_call_was_made_too_few_times
|
296
297
|
expectation = new_expectation.times(2)
|
297
|
-
1.times {expectation.invoke}
|
298
|
+
1.times { expectation.invoke }
|
298
299
|
assert !expectation.verified?
|
299
300
|
assert_match(/expected exactly twice, invoked once/i, expectation.mocha_inspect)
|
300
301
|
end
|
301
302
|
|
302
303
|
def test_should_not_verify_successfully_if_expected_call_was_made_too_many_times
|
303
304
|
expectation = new_expectation.times(2)
|
304
|
-
3.times {expectation.invoke}
|
305
|
+
3.times { expectation.invoke }
|
305
306
|
assert !expectation.verified?
|
306
307
|
end
|
307
308
|
|
@@ -320,13 +321,14 @@ class ExpectationTest < Mocha::TestCase
|
|
320
321
|
assert_equal 0, assertion_counter.count
|
321
322
|
end
|
322
323
|
|
324
|
+
# rubocop:disable Style/Semicolon
|
323
325
|
def test_should_store_backtrace_from_point_where_expectation_was_created
|
324
326
|
execution_point = ExecutionPoint.current; expectation = Expectation.new(nil, :expected_method)
|
325
327
|
assert_equal execution_point, ExecutionPoint.new(expectation.backtrace)
|
326
328
|
end
|
329
|
+
# rubocop:enable Style/Semicolon
|
327
330
|
|
328
331
|
class FakeMock
|
329
|
-
|
330
332
|
def initialize(name)
|
331
333
|
@name = name
|
332
334
|
end
|
@@ -334,20 +336,18 @@ class ExpectationTest < Mocha::TestCase
|
|
334
336
|
def mocha_inspect
|
335
337
|
@name
|
336
338
|
end
|
337
|
-
|
338
339
|
end
|
339
340
|
|
340
341
|
def test_should_raise_error_with_message_indicating_which_method_was_expected_to_be_called_on_which_mock_object_with_which_parameters_and_in_what_sequences
|
341
342
|
mock = FakeMock.new('mock')
|
342
343
|
sequence_one = Sequence.new('one')
|
343
344
|
sequence_two = Sequence.new('two')
|
344
|
-
expectation = Expectation.new(mock, :expected_method).with(1, 2, {'a' => true}, {:b => false}, [1, 2, 3]).in_sequence(sequence_one, sequence_two)
|
345
|
+
expectation = Expectation.new(mock, :expected_method).with(1, 2, { 'a' => true }, { :b => false }, [1, 2, 3]).in_sequence(sequence_one, sequence_two)
|
345
346
|
assert !expectation.verified?
|
346
347
|
assert_match %{mock.expected_method(1, 2, {"a" => true}, {:b => false}, [1, 2, 3]); in sequence "one"; in sequence "two"}, expectation.mocha_inspect
|
347
348
|
end
|
348
349
|
|
349
350
|
class FakeConstraint
|
350
|
-
|
351
351
|
def initialize(allows_invocation_now)
|
352
352
|
@allows_invocation_now = allows_invocation_now
|
353
353
|
end
|
@@ -355,12 +355,11 @@ class ExpectationTest < Mocha::TestCase
|
|
355
355
|
def allows_invocation_now?
|
356
356
|
@allows_invocation_now
|
357
357
|
end
|
358
|
-
|
359
358
|
end
|
360
359
|
|
361
360
|
def test_should_be_in_correct_order_if_all_ordering_constraints_allow_invocation_now
|
362
|
-
constraint_one = FakeConstraint.new(
|
363
|
-
constraint_two = FakeConstraint.new(
|
361
|
+
constraint_one = FakeConstraint.new(true)
|
362
|
+
constraint_two = FakeConstraint.new(true)
|
364
363
|
expectation = Expectation.new(nil, :method_one)
|
365
364
|
expectation.add_ordering_constraint(constraint_one)
|
366
365
|
expectation.add_ordering_constraint(constraint_two)
|
@@ -368,8 +367,8 @@ class ExpectationTest < Mocha::TestCase
|
|
368
367
|
end
|
369
368
|
|
370
369
|
def test_should_not_be_in_correct_order_if_one_ordering_constraint_does_not_allow_invocation_now
|
371
|
-
constraint_one = FakeConstraint.new(
|
372
|
-
constraint_two = FakeConstraint.new(
|
370
|
+
constraint_one = FakeConstraint.new(true)
|
371
|
+
constraint_two = FakeConstraint.new(false)
|
373
372
|
expectation = Expectation.new(nil, :method_one)
|
374
373
|
expectation.add_ordering_constraint(constraint_one)
|
375
374
|
expectation.add_ordering_constraint(constraint_two)
|
@@ -377,8 +376,8 @@ class ExpectationTest < Mocha::TestCase
|
|
377
376
|
end
|
378
377
|
|
379
378
|
def test_should_match_if_all_ordering_constraints_allow_invocation_now
|
380
|
-
constraint_one = FakeConstraint.new(
|
381
|
-
constraint_two = FakeConstraint.new(
|
379
|
+
constraint_one = FakeConstraint.new(true)
|
380
|
+
constraint_two = FakeConstraint.new(true)
|
382
381
|
expectation = Expectation.new(nil, :method_one)
|
383
382
|
expectation.add_ordering_constraint(constraint_one)
|
384
383
|
expectation.add_ordering_constraint(constraint_two)
|
@@ -386,8 +385,8 @@ class ExpectationTest < Mocha::TestCase
|
|
386
385
|
end
|
387
386
|
|
388
387
|
def test_should_not_match_if_one_ordering_constraints_does_not_allow_invocation_now
|
389
|
-
constraint_one = FakeConstraint.new(
|
390
|
-
constraint_two = FakeConstraint.new(
|
388
|
+
constraint_one = FakeConstraint.new(true)
|
389
|
+
constraint_two = FakeConstraint.new(false)
|
391
390
|
expectation = Expectation.new(nil, :method_one)
|
392
391
|
expectation.add_ordering_constraint(constraint_one)
|
393
392
|
expectation.add_ordering_constraint(constraint_two)
|
@@ -418,7 +417,6 @@ class ExpectationTest < Mocha::TestCase
|
|
418
417
|
end
|
419
418
|
|
420
419
|
class FakeSequence
|
421
|
-
|
422
420
|
attr_reader :expectations
|
423
421
|
|
424
422
|
def initialize
|
@@ -428,7 +426,6 @@ class ExpectationTest < Mocha::TestCase
|
|
428
426
|
def constrain_as_next_in_sequence(expectation)
|
429
427
|
@expectations << expectation
|
430
428
|
end
|
431
|
-
|
432
429
|
end
|
433
430
|
|
434
431
|
def test_should_tell_sequences_to_constrain_expectation_as_next_in_sequence
|
@@ -441,7 +438,6 @@ class ExpectationTest < Mocha::TestCase
|
|
441
438
|
end
|
442
439
|
|
443
440
|
class FakeState
|
444
|
-
|
445
441
|
def initialize
|
446
442
|
@active = false
|
447
443
|
end
|
@@ -453,7 +449,6 @@ class ExpectationTest < Mocha::TestCase
|
|
453
449
|
def active?
|
454
450
|
@active
|
455
451
|
end
|
456
|
-
|
457
452
|
end
|
458
453
|
|
459
454
|
def test_should_change_state_when_expectation_is_invoked
|
@@ -483,7 +478,7 @@ class ExpectationTest < Mocha::TestCase
|
|
483
478
|
define_method(:inspect) { 'mock' }
|
484
479
|
end
|
485
480
|
expectation = Expectation.new(object, :method_one)
|
486
|
-
assert_match Regexp.new(
|
481
|
+
assert_match Regexp.new('^#<Expectation:0x[0-9A-Fa-f]{1,12} .* >$'), expectation.inspect
|
487
482
|
end
|
488
483
|
|
489
484
|
def test_should_include_output_of_mocha_inspect_in_inspect
|
@@ -2,15 +2,13 @@ require File.expand_path('../../test_helper', __FILE__)
|
|
2
2
|
require 'mocha/inspect'
|
3
3
|
|
4
4
|
class HashInspectTest < Mocha::TestCase
|
5
|
-
|
6
5
|
def test_should_keep_spacing_between_key_value
|
7
|
-
hash = {:a => true}
|
6
|
+
hash = { :a => true }
|
8
7
|
assert_equal '{:a => true}', hash.mocha_inspect
|
9
8
|
end
|
10
9
|
|
11
10
|
def test_should_use_mocha_inspect_on_each_item
|
12
|
-
hash = {:a => 'mocha'}
|
13
|
-
assert_equal %{
|
11
|
+
hash = { :a => 'mocha' }
|
12
|
+
assert_equal %({:a => "mocha"}), hash.mocha_inspect
|
14
13
|
end
|
15
|
-
|
16
14
|
end
|
data/test/unit/hooks_test.rb
CHANGED
@@ -1,29 +1,35 @@
|
|
1
1
|
require File.expand_path('../../test_helper', __FILE__)
|
2
2
|
require 'mocha/hooks'
|
3
|
+
require 'mocha/mockery'
|
3
4
|
|
4
5
|
class HooksTest < Mocha::TestCase
|
6
|
+
# rubocop:disable Style/ClassAndModuleChildren
|
5
7
|
class Mocha::Mockery
|
6
8
|
class << self
|
7
|
-
attr_writer :
|
9
|
+
attr_writer :instances
|
8
10
|
end
|
9
11
|
end
|
12
|
+
# rubocop:enable Style/ClassAndModuleChildren
|
10
13
|
|
11
14
|
class FakeMockery
|
12
|
-
def verify(*args)
|
13
|
-
end
|
15
|
+
def verify(*args); end
|
14
16
|
|
15
17
|
def teardown
|
16
|
-
raise
|
18
|
+
raise 'exception within Mockery#teardown'
|
17
19
|
end
|
18
20
|
end
|
19
21
|
|
20
22
|
def test_ensure_mockery_instance_is_reset_even_when_an_exception_is_raised_in_mockery_teardown
|
21
23
|
fake_test_case = Object.new.extend(Mocha::Hooks)
|
22
|
-
|
23
|
-
Mocha::Mockery.
|
24
|
+
mockery = FakeMockery.new
|
25
|
+
Mocha::Mockery.instances = [mockery]
|
24
26
|
|
25
|
-
|
27
|
+
begin
|
28
|
+
fake_test_case.mocha_teardown
|
29
|
+
rescue StandardError
|
30
|
+
nil
|
31
|
+
end
|
26
32
|
|
27
|
-
|
33
|
+
assert_kind_of Mocha::Mockery::Null, Mocha::Mockery.instance
|
28
34
|
end
|
29
35
|
end
|
@@ -3,11 +3,9 @@ require File.expand_path('../../test_helper', __FILE__)
|
|
3
3
|
require 'mocha/in_state_ordering_constraint'
|
4
4
|
|
5
5
|
class InStateOrderingConstraintTest < Mocha::TestCase
|
6
|
-
|
7
6
|
include Mocha
|
8
7
|
|
9
8
|
class FakeStatePredicate
|
10
|
-
|
11
9
|
attr_writer :active, :description
|
12
10
|
|
13
11
|
def active?
|
@@ -17,7 +15,6 @@ class InStateOrderingConstraintTest < Mocha::TestCase
|
|
17
15
|
def mocha_inspect
|
18
16
|
@description
|
19
17
|
end
|
20
|
-
|
21
18
|
end
|
22
19
|
|
23
20
|
def test_should_allow_invocation_when_state_is_active
|
@@ -39,5 +36,4 @@ class InStateOrderingConstraintTest < Mocha::TestCase
|
|
39
36
|
|
40
37
|
assert_equal 'when the-state-predicate', ordering_constraint.mocha_inspect
|
41
38
|
end
|
42
|
-
|
43
39
|
end
|
@@ -2,7 +2,6 @@ require File.expand_path('../../test_helper', __FILE__)
|
|
2
2
|
require 'mocha/method_matcher'
|
3
3
|
|
4
4
|
class MethodMatcherTest < Mocha::TestCase
|
5
|
-
|
6
5
|
include Mocha
|
7
6
|
|
8
7
|
def test_should_match_if_actual_method_name_is_same_as_expected_method_name
|
@@ -22,7 +21,6 @@ class MethodMatcherTest < Mocha::TestCase
|
|
22
21
|
|
23
22
|
def test_should_describe_what_method_is_expected
|
24
23
|
method_matcher = MethodMatcher.new(:method_name)
|
25
|
-
assert_equal
|
24
|
+
assert_equal 'method_name', method_matcher.mocha_inspect
|
26
25
|
end
|
27
|
-
|
28
26
|
end
|
data/test/unit/mock_test.rb
CHANGED
@@ -6,22 +6,21 @@ require 'set'
|
|
6
6
|
require 'simple_counter'
|
7
7
|
|
8
8
|
class MockTest < Mocha::TestCase
|
9
|
-
|
10
9
|
include Mocha
|
11
10
|
|
12
11
|
def test_should_set_single_expectation
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
12
|
+
mock = build_mock
|
13
|
+
mock.expects(:method1).returns(1)
|
14
|
+
assert_nothing_raised(ExpectationErrorFactory.exception_class) do
|
15
|
+
assert_equal 1, mock.method1
|
16
|
+
end
|
18
17
|
end
|
19
18
|
|
20
19
|
def test_should_build_and_store_expectations
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
20
|
+
mock = build_mock
|
21
|
+
expectation = mock.expects(:method1)
|
22
|
+
assert_not_nil expectation
|
23
|
+
assert_equal [expectation], mock.__expectations__.to_a
|
25
24
|
end
|
26
25
|
|
27
26
|
def test_should_not_stub_everything_by_default
|
@@ -45,10 +44,24 @@ class MockTest < Mocha::TestCase
|
|
45
44
|
assert_equal true, mock.eql?(mock)
|
46
45
|
end
|
47
46
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
47
|
+
EXCLUDED_METHODS = {
|
48
|
+
true => %w[
|
49
|
+
method_missing
|
50
|
+
singleton_method_undefined
|
51
|
+
initialize
|
52
|
+
],
|
53
|
+
false => [
|
54
|
+
:object_id,
|
55
|
+
:method_missing,
|
56
|
+
:singleton_method_undefined,
|
57
|
+
:initialize,
|
58
|
+
:String,
|
59
|
+
:singleton_method_added
|
60
|
+
]
|
61
|
+
}.freeze
|
62
|
+
|
63
|
+
OBJECT_METHODS = STANDARD_OBJECT_PUBLIC_INSTANCE_METHODS.reject do |m|
|
64
|
+
m =~ /^__.*__$/ || EXCLUDED_METHODS[PRE_RUBY_V19].include?(m)
|
52
65
|
end
|
53
66
|
|
54
67
|
def test_should_be_able_to_mock_standard_object_methods
|
@@ -149,7 +162,7 @@ class MockTest < Mocha::TestCase
|
|
149
162
|
parameters_for_yield = [1, 2, 3]
|
150
163
|
mock.expects(:method1).yields(*parameters_for_yield)
|
151
164
|
yielded_parameters = nil
|
152
|
-
mock.method1
|
165
|
+
mock.method1 { |*parameters| yielded_parameters = parameters }
|
153
166
|
assert_equal parameters_for_yield, yielded_parameters
|
154
167
|
end
|
155
168
|
|
@@ -228,7 +241,7 @@ class MockTest < Mocha::TestCase
|
|
228
241
|
|
229
242
|
def test_should_respond_to_methods_which_the_responder_does_responds_to
|
230
243
|
instance = Class.new do
|
231
|
-
define_method(:respond_to?) { |
|
244
|
+
define_method(:respond_to?) { |_symbol| true }
|
232
245
|
end.new
|
233
246
|
mock = build_mock
|
234
247
|
mock.responds_like(instance)
|
@@ -237,7 +250,7 @@ class MockTest < Mocha::TestCase
|
|
237
250
|
|
238
251
|
def test_should_not_respond_to_methods_which_the_responder_does_not_responds_to
|
239
252
|
instance = Class.new do
|
240
|
-
define_method(:respond_to?) { |
|
253
|
+
define_method(:respond_to?) { |_symbol| false }
|
241
254
|
end.new
|
242
255
|
mock = build_mock
|
243
256
|
mock.responds_like(instance)
|
@@ -246,7 +259,7 @@ class MockTest < Mocha::TestCase
|
|
246
259
|
|
247
260
|
def test_should_respond_to_methods_which_the_responder_instance_does_responds_to
|
248
261
|
klass = Class.new do
|
249
|
-
define_method(:respond_to?) { |
|
262
|
+
define_method(:respond_to?) { |_symbol| true }
|
250
263
|
end
|
251
264
|
mock = build_mock
|
252
265
|
mock.responds_like_instance_of(klass)
|
@@ -255,7 +268,7 @@ class MockTest < Mocha::TestCase
|
|
255
268
|
|
256
269
|
def test_should_not_respond_to_methods_which_the_responder_instance_does_not_responds_to
|
257
270
|
klass = Class.new do
|
258
|
-
define_method(:respond_to?) { |
|
271
|
+
define_method(:respond_to?) { |_symbol| false }
|
259
272
|
end
|
260
273
|
mock = build_mock
|
261
274
|
mock.responds_like_instance_of(klass)
|
@@ -280,7 +293,7 @@ class MockTest < Mocha::TestCase
|
|
280
293
|
|
281
294
|
def test_should_not_raise_no_method_error_if_responder_does_respond_to_invoked_method
|
282
295
|
instance = Class.new do
|
283
|
-
define_method(:respond_to?) { |
|
296
|
+
define_method(:respond_to?) { |_symbol| true }
|
284
297
|
end.new
|
285
298
|
mock = build_mock
|
286
299
|
mock.responds_like(instance)
|
@@ -290,7 +303,7 @@ class MockTest < Mocha::TestCase
|
|
290
303
|
|
291
304
|
def test_should_raise_no_method_error_if_responder_does_not_respond_to_invoked_method
|
292
305
|
instance = Class.new do
|
293
|
-
define_method(:respond_to?) { |
|
306
|
+
define_method(:respond_to?) { |_symbol| false }
|
294
307
|
define_method(:mocha_inspect) { 'mocha_inspect' }
|
295
308
|
end.new
|
296
309
|
mock = build_mock
|
@@ -301,7 +314,7 @@ class MockTest < Mocha::TestCase
|
|
301
314
|
|
302
315
|
def test_should_raise_no_method_error_with_message_indicating_that_mock_is_constrained_to_respond_like_responder
|
303
316
|
instance = Class.new do
|
304
|
-
define_method(:respond_to?) { |
|
317
|
+
define_method(:respond_to?) { |_symbol| false }
|
305
318
|
define_method(:mocha_inspect) { 'mocha_inspect' }
|
306
319
|
end.new
|
307
320
|
mock = build_mock
|