mocha 0.10.5 → 1.13.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/FUNDING.yml +1 -0
- data/.rubocop.yml +61 -0
- data/.rubocop_todo.yml +27 -0
- data/.yardopts +24 -0
- data/CONTRIBUTING.md +7 -0
- data/COPYING.md +3 -0
- data/Gemfile +2 -2
- data/{MIT-LICENSE.rdoc → MIT-LICENSE.md} +1 -1
- data/README.md +363 -0
- data/{RELEASE.rdoc → RELEASE.md} +436 -35
- data/Rakefile +87 -87
- data/gemfiles/Gemfile.minitest.latest +2 -2
- data/gemfiles/Gemfile.test-unit.latest +6 -2
- data/init.rb +1 -3
- data/lib/mocha/any_instance_method.rb +12 -45
- data/lib/mocha/api.rb +199 -131
- data/lib/mocha/argument_iterator.rb +6 -10
- data/lib/mocha/backtrace_filter.rb +1 -5
- data/lib/mocha/block_matcher.rb +31 -0
- data/lib/mocha/cardinality.rb +77 -66
- data/lib/mocha/central.rb +27 -18
- data/lib/mocha/change_state_side_effect.rb +3 -7
- data/lib/mocha/class_methods.rb +62 -0
- data/lib/mocha/configuration.rb +399 -46
- data/lib/mocha/debug.rb +12 -0
- data/lib/mocha/deprecation.rb +11 -12
- data/lib/mocha/detection/mini_test.rb +23 -0
- data/lib/mocha/detection/test_unit.rb +27 -0
- data/lib/mocha/error_with_filtered_backtrace.rb +13 -0
- data/lib/mocha/exception_raiser.rb +8 -10
- data/lib/mocha/expectation.rb +290 -151
- data/lib/mocha/expectation_error.rb +6 -13
- data/lib/mocha/expectation_error_factory.rb +35 -0
- data/lib/mocha/expectation_list.rb +22 -22
- data/lib/mocha/hooks.rb +42 -0
- data/lib/mocha/in_state_ordering_constraint.rb +3 -7
- data/lib/mocha/inspect.rb +35 -43
- data/lib/mocha/instance_method.rb +12 -21
- data/lib/mocha/integration/assertion_counter.rb +13 -0
- data/lib/mocha/integration/mini_test/adapter.rb +52 -0
- data/lib/mocha/integration/mini_test/exception_translation.rb +1 -7
- data/lib/mocha/integration/mini_test/nothing.rb +19 -0
- data/lib/mocha/integration/mini_test/version_13.rb +35 -25
- data/lib/mocha/integration/mini_test/version_140.rb +35 -26
- data/lib/mocha/integration/mini_test/version_141.rb +43 -34
- data/lib/mocha/integration/mini_test/version_142_to_172.rb +44 -35
- data/lib/mocha/integration/mini_test/version_200.rb +45 -36
- data/lib/mocha/integration/mini_test/version_201_to_222.rb +44 -35
- data/lib/mocha/integration/mini_test/version_2110_to_2111.rb +70 -0
- data/lib/mocha/integration/mini_test/version_2112_to_320.rb +73 -0
- data/lib/mocha/integration/mini_test/version_230_to_2101.rb +68 -0
- data/lib/mocha/integration/mini_test.rb +51 -49
- data/lib/mocha/integration/monkey_patcher.rb +24 -0
- data/lib/mocha/integration/test_unit/adapter.rb +50 -0
- data/lib/mocha/integration/test_unit/gem_version_200.rb +39 -29
- data/lib/mocha/integration/test_unit/gem_version_201_to_202.rb +39 -29
- data/lib/mocha/integration/test_unit/gem_version_203_to_220.rb +39 -29
- data/lib/mocha/integration/test_unit/gem_version_230_to_250.rb +68 -0
- data/lib/mocha/integration/test_unit/nothing.rb +19 -0
- data/lib/mocha/integration/test_unit/ruby_version_185_and_below.rb +39 -29
- data/lib/mocha/integration/test_unit/ruby_version_186_and_above.rb +40 -30
- data/lib/mocha/integration/test_unit.rb +45 -51
- data/lib/mocha/integration.rb +6 -33
- data/lib/mocha/invocation.rb +77 -0
- data/lib/mocha/is_a.rb +0 -2
- data/lib/mocha/logger.rb +2 -6
- data/lib/mocha/macos_version.rb +5 -0
- data/lib/mocha/method_matcher.rb +6 -10
- data/lib/mocha/minitest.rb +8 -0
- data/lib/mocha/mock.rb +266 -79
- data/lib/mocha/mockery.rb +104 -106
- data/lib/mocha/names.rb +10 -20
- data/lib/mocha/not_initialized_error.rb +7 -0
- data/lib/mocha/object_methods.rb +169 -0
- data/lib/mocha/parameter_matchers/all_of.rb +18 -14
- data/lib/mocha/parameter_matchers/any_of.rb +19 -14
- data/lib/mocha/parameter_matchers/any_parameters.rb +14 -13
- data/lib/mocha/parameter_matchers/anything.rb +17 -14
- data/lib/mocha/parameter_matchers/base.rb +33 -31
- data/lib/mocha/parameter_matchers/equals.rb +18 -13
- data/lib/mocha/parameter_matchers/equivalent_uri.rb +58 -0
- data/lib/mocha/parameter_matchers/has_entries.rb +19 -14
- data/lib/mocha/parameter_matchers/has_entry.rb +58 -26
- data/lib/mocha/parameter_matchers/has_key.rb +18 -13
- data/lib/mocha/parameter_matchers/has_keys.rb +53 -0
- data/lib/mocha/parameter_matchers/has_value.rb +18 -13
- data/lib/mocha/parameter_matchers/includes.rb +80 -19
- data/lib/mocha/parameter_matchers/instance_methods.rb +18 -0
- data/lib/mocha/parameter_matchers/instance_of.rb +18 -13
- data/lib/mocha/parameter_matchers/is_a.rb +20 -14
- data/lib/mocha/parameter_matchers/kind_of.rb +20 -13
- data/lib/mocha/parameter_matchers/not.rb +19 -14
- data/lib/mocha/parameter_matchers/optionally.rb +23 -17
- data/lib/mocha/parameter_matchers/regexp_matches.rb +16 -12
- data/lib/mocha/parameter_matchers/responds_with.rb +17 -11
- data/lib/mocha/parameter_matchers/yaml_equivalent.rb +15 -9
- data/lib/mocha/parameter_matchers.rb +4 -5
- data/lib/mocha/parameters_matcher.rb +11 -14
- data/lib/mocha/raised_exception.rb +11 -0
- data/lib/mocha/receivers.rb +45 -0
- data/lib/mocha/return_values.rb +11 -15
- data/lib/mocha/ruby_version.rb +4 -0
- data/lib/mocha/sequence.rb +21 -17
- data/lib/mocha/setup.rb +14 -0
- data/lib/mocha/single_return_value.rb +5 -8
- data/lib/mocha/singleton_class.rb +9 -0
- data/lib/mocha/state_machine.rb +69 -67
- data/lib/mocha/stubbed_method.rb +125 -0
- data/lib/mocha/stubbing_error.rb +6 -14
- data/lib/mocha/test_unit.rb +8 -0
- data/lib/mocha/thrower.rb +6 -8
- data/lib/mocha/thrown_object.rb +12 -0
- data/lib/mocha/version.rb +1 -1
- data/lib/mocha/yield_parameters.rb +12 -22
- data/lib/mocha.rb +8 -3
- data/mocha.gemspec +43 -34
- data/yard-templates/default/layout/html/google_analytics.erb +8 -0
- data/yard-templates/default/layout/html/setup.rb +5 -0
- metadata +123 -268
- data/COPYING.rdoc +0 -3
- data/README.rdoc +0 -54
- data/examples/misc.rb +0 -43
- data/examples/mocha.rb +0 -25
- data/examples/stubba.rb +0 -64
- data/gemfiles/Gemfile.minitest.1.3.0 +0 -7
- data/gemfiles/Gemfile.minitest.1.4.0 +0 -7
- data/gemfiles/Gemfile.minitest.1.4.1 +0 -7
- data/gemfiles/Gemfile.minitest.1.4.2 +0 -7
- data/gemfiles/Gemfile.minitest.2.0.0 +0 -7
- data/gemfiles/Gemfile.minitest.2.0.1 +0 -7
- data/gemfiles/Gemfile.minitest.2.3.0 +0 -7
- data/gemfiles/Gemfile.test-unit.2.0.0 +0 -8
- data/gemfiles/Gemfile.test-unit.2.0.1 +0 -7
- data/gemfiles/Gemfile.test-unit.2.0.3 +0 -7
- data/lib/mocha/class_method.rb +0 -98
- data/lib/mocha/integration/mini_test/assertion_counter.rb +0 -23
- data/lib/mocha/integration/mini_test/version_230_to_262.rb +0 -59
- data/lib/mocha/integration/test_unit/assertion_counter.rb +0 -23
- data/lib/mocha/integration/test_unit/gem_version_230_to_240.rb +0 -58
- data/lib/mocha/module_method.rb +0 -16
- data/lib/mocha/multiple_yields.rb +0 -20
- data/lib/mocha/no_yields.rb +0 -11
- data/lib/mocha/object.rb +0 -223
- data/lib/mocha/options.rb +0 -1
- data/lib/mocha/parameter_matchers/object.rb +0 -15
- data/lib/mocha/parameter_matchers/query_string.rb +0 -47
- data/lib/mocha/pretty_parameters.rb +0 -28
- data/lib/mocha/single_yield.rb +0 -18
- data/lib/mocha/standalone.rb +0 -1
- data/lib/mocha/unexpected_invocation.rb +0 -18
- data/lib/mocha_standalone.rb +0 -2
- data/lib/stubba.rb +0 -4
- data/test/acceptance/acceptance_test_helper.rb +0 -41
- data/test/acceptance/api_test.rb +0 -139
- data/test/acceptance/bug_18914_test.rb +0 -43
- data/test/acceptance/bug_21465_test.rb +0 -34
- data/test/acceptance/bug_21563_test.rb +0 -25
- data/test/acceptance/exception_rescue_test.rb +0 -55
- data/test/acceptance/expectations_on_multiple_methods_test.rb +0 -55
- data/test/acceptance/expected_invocation_count_test.rb +0 -232
- data/test/acceptance/failure_messages_test.rb +0 -64
- data/test/acceptance/issue_65_test.rb +0 -63
- data/test/acceptance/issue_70_test.rb +0 -55
- data/test/acceptance/minitest_test.rb +0 -162
- data/test/acceptance/mocha_example_test.rb +0 -98
- data/test/acceptance/mocha_test_result_test.rb +0 -84
- data/test/acceptance/mock_test.rb +0 -100
- data/test/acceptance/mock_with_initializer_block_test.rb +0 -51
- data/test/acceptance/mocked_methods_dispatch_test.rb +0 -78
- data/test/acceptance/multiple_expectations_failure_message_test.rb +0 -68
- data/test/acceptance/optional_parameters_test.rb +0 -70
- data/test/acceptance/parameter_matcher_test.rb +0 -300
- data/test/acceptance/partial_mocks_test.rb +0 -47
- data/test/acceptance/raise_exception_test.rb +0 -39
- data/test/acceptance/return_value_test.rb +0 -52
- data/test/acceptance/sequence_test.rb +0 -192
- data/test/acceptance/states_test.rb +0 -70
- data/test/acceptance/stub_any_instance_method_test.rb +0 -198
- data/test/acceptance/stub_class_method_defined_on_active_record_association_proxy_test.rb +0 -106
- data/test/acceptance/stub_class_method_defined_on_class_test.rb +0 -72
- data/test/acceptance/stub_class_method_defined_on_module_test.rb +0 -75
- data/test/acceptance/stub_class_method_defined_on_superclass_test.rb +0 -75
- data/test/acceptance/stub_everything_test.rb +0 -56
- data/test/acceptance/stub_instance_method_defined_on_active_record_association_proxy_test.rb +0 -93
- data/test/acceptance/stub_instance_method_defined_on_class_and_aliased_test.rb +0 -69
- data/test/acceptance/stub_instance_method_defined_on_class_test.rb +0 -66
- data/test/acceptance/stub_instance_method_defined_on_kernel_module_test.rb +0 -75
- data/test/acceptance/stub_instance_method_defined_on_module_test.rb +0 -75
- data/test/acceptance/stub_instance_method_defined_on_object_class_test.rb +0 -75
- data/test/acceptance/stub_instance_method_defined_on_singleton_class_test.rb +0 -70
- data/test/acceptance/stub_instance_method_defined_on_superclass_test.rb +0 -72
- data/test/acceptance/stub_module_method_test.rb +0 -163
- data/test/acceptance/stub_test.rb +0 -52
- data/test/acceptance/stubba_example_test.rb +0 -102
- data/test/acceptance/stubba_test.rb +0 -15
- data/test/acceptance/stubba_test_result_test.rb +0 -66
- data/test/acceptance/stubbing_error_backtrace_test.rb +0 -64
- data/test/acceptance/stubbing_method_unnecessarily_test.rb +0 -65
- data/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +0 -130
- data/test/acceptance/stubbing_non_existent_class_method_test.rb +0 -157
- data/test/acceptance/stubbing_non_existent_instance_method_test.rb +0 -147
- data/test/acceptance/stubbing_non_public_any_instance_method_test.rb +0 -130
- data/test/acceptance/stubbing_non_public_class_method_test.rb +0 -163
- data/test/acceptance/stubbing_non_public_instance_method_test.rb +0 -143
- data/test/acceptance/stubbing_on_non_mock_object_test.rb +0 -64
- data/test/acceptance/throw_test.rb +0 -45
- data/test/acceptance/unstubbing_test.rb +0 -151
- data/test/deprecation_disabler.rb +0 -15
- data/test/execution_point.rb +0 -36
- data/test/method_definer.rb +0 -24
- data/test/mini_test_result.rb +0 -83
- data/test/simple_counter.rb +0 -13
- data/test/test_helper.rb +0 -11
- data/test/test_runner.rb +0 -50
- data/test/unit/any_instance_method_test.rb +0 -136
- data/test/unit/array_inspect_test.rb +0 -16
- data/test/unit/backtrace_filter_test.rb +0 -19
- data/test/unit/cardinality_test.rb +0 -56
- data/test/unit/central_test.rb +0 -100
- data/test/unit/change_state_side_effect_test.rb +0 -41
- data/test/unit/class_method_test.rb +0 -260
- data/test/unit/configuration_test.rb +0 -38
- data/test/unit/date_time_inspect_test.rb +0 -21
- data/test/unit/exception_raiser_test.rb +0 -42
- data/test/unit/expectation_list_test.rb +0 -71
- data/test/unit/expectation_test.rb +0 -480
- data/test/unit/hash_inspect_test.rb +0 -16
- data/test/unit/in_state_ordering_constraint_test.rb +0 -43
- data/test/unit/method_matcher_test.rb +0 -23
- data/test/unit/mock_test.rb +0 -312
- data/test/unit/mockery_test.rb +0 -150
- data/test/unit/multiple_yields_test.rb +0 -18
- data/test/unit/no_yields_test.rb +0 -18
- data/test/unit/object_inspect_test.rb +0 -38
- data/test/unit/object_test.rb +0 -87
- data/test/unit/parameter_matchers/all_of_test.rb +0 -26
- data/test/unit/parameter_matchers/any_of_test.rb +0 -26
- data/test/unit/parameter_matchers/anything_test.rb +0 -21
- data/test/unit/parameter_matchers/equals_test.rb +0 -25
- data/test/unit/parameter_matchers/has_entries_test.rb +0 -51
- data/test/unit/parameter_matchers/has_entry_test.rb +0 -96
- data/test/unit/parameter_matchers/has_key_test.rb +0 -55
- data/test/unit/parameter_matchers/has_value_test.rb +0 -57
- data/test/unit/parameter_matchers/includes_test.rb +0 -44
- data/test/unit/parameter_matchers/instance_of_test.rb +0 -25
- data/test/unit/parameter_matchers/is_a_test.rb +0 -25
- data/test/unit/parameter_matchers/kind_of_test.rb +0 -25
- data/test/unit/parameter_matchers/not_test.rb +0 -26
- data/test/unit/parameter_matchers/regexp_matches_test.rb +0 -46
- data/test/unit/parameter_matchers/responds_with_test.rb +0 -25
- data/test/unit/parameter_matchers/stub_matcher.rb +0 -27
- data/test/unit/parameter_matchers/yaml_equivalent_test.rb +0 -25
- data/test/unit/parameters_matcher_test.rb +0 -121
- data/test/unit/return_values_test.rb +0 -63
- data/test/unit/sequence_test.rb +0 -104
- data/test/unit/single_return_value_test.rb +0 -14
- data/test/unit/single_yield_test.rb +0 -18
- data/test/unit/state_machine_test.rb +0 -98
- data/test/unit/string_inspect_test.rb +0 -11
- data/test/unit/thrower_test.rb +0 -20
- data/test/unit/yield_parameters_test.rb +0 -93
data/lib/mocha/expectation.rb
CHANGED
@@ -9,17 +9,18 @@ require 'mocha/is_a'
|
|
9
9
|
require 'mocha/in_state_ordering_constraint'
|
10
10
|
require 'mocha/change_state_side_effect'
|
11
11
|
require 'mocha/cardinality'
|
12
|
+
require 'mocha/configuration'
|
13
|
+
require 'mocha/block_matcher'
|
12
14
|
|
13
|
-
module Mocha
|
14
|
-
|
15
|
-
# Methods on expectations returned from Mock#expects, Mock#stubs, Object#expects and Object#stubs.
|
15
|
+
module Mocha
|
16
|
+
# Methods on expectations returned from {Mock#expects}, {Mock#stubs}, {ObjectMethods#expects} and {ObjectMethods#stubs}.
|
16
17
|
class Expectation
|
17
|
-
|
18
|
-
# :call-seq: times(range) -> expectation
|
19
|
-
#
|
20
18
|
# Modifies expectation so that the number of calls to the expected method must be within a specific +range+.
|
21
19
|
#
|
22
|
-
#
|
20
|
+
# @param [Range,Integer] range specifies the allowable range in the number of expected invocations.
|
21
|
+
# @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
|
22
|
+
#
|
23
|
+
# @example Specifying a specific number of expected invocations.
|
23
24
|
# object = mock()
|
24
25
|
# object.expects(:expected_method).times(3)
|
25
26
|
# 3.times { object.expected_method }
|
@@ -30,6 +31,7 @@ module Mocha # :nodoc:
|
|
30
31
|
# 2.times { object.expected_method }
|
31
32
|
# # => verify fails
|
32
33
|
#
|
34
|
+
# @example Specifying a range in the number of expected invocations.
|
33
35
|
# object = mock()
|
34
36
|
# object.expects(:expected_method).times(2..4)
|
35
37
|
# 3.times { object.expected_method }
|
@@ -40,13 +42,15 @@ module Mocha # :nodoc:
|
|
40
42
|
# object.expected_method
|
41
43
|
# # => verify fails
|
42
44
|
def times(range)
|
43
|
-
@cardinality
|
45
|
+
@cardinality.times(range)
|
44
46
|
self
|
45
47
|
end
|
46
48
|
|
47
|
-
# :call-seq: twice() -> expectation
|
48
|
-
#
|
49
49
|
# Modifies expectation so that the expected method must be called exactly twice.
|
50
|
+
#
|
51
|
+
# @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
|
52
|
+
#
|
53
|
+
# @example Expected method must be invoked exactly twice.
|
50
54
|
# object = mock()
|
51
55
|
# object.expects(:expected_method).twice
|
52
56
|
# object.expected_method
|
@@ -57,22 +61,24 @@ module Mocha # :nodoc:
|
|
57
61
|
# object.expects(:expected_method).twice
|
58
62
|
# object.expected_method
|
59
63
|
# object.expected_method
|
60
|
-
# object.expected_method
|
61
|
-
# # => verify fails
|
64
|
+
# object.expected_method # => unexpected invocation
|
62
65
|
#
|
63
66
|
# object = mock()
|
64
67
|
# object.expects(:expected_method).twice
|
65
68
|
# object.expected_method
|
66
69
|
# # => verify fails
|
67
70
|
def twice
|
68
|
-
@cardinality
|
71
|
+
@cardinality.exactly(2)
|
69
72
|
self
|
70
73
|
end
|
71
74
|
|
72
|
-
# :call-seq: once() -> expectation
|
73
|
-
#
|
74
75
|
# Modifies expectation so that the expected method must be called exactly once.
|
76
|
+
#
|
75
77
|
# Note that this is the default behaviour for an expectation, but you may wish to use it for clarity/emphasis.
|
78
|
+
#
|
79
|
+
# @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
|
80
|
+
#
|
81
|
+
# @example Expected method must be invoked exactly once.
|
76
82
|
# object = mock()
|
77
83
|
# object.expects(:expected_method).once
|
78
84
|
# object.expected_method
|
@@ -81,36 +87,39 @@ module Mocha # :nodoc:
|
|
81
87
|
# object = mock()
|
82
88
|
# object.expects(:expected_method).once
|
83
89
|
# object.expected_method
|
84
|
-
# object.expected_method
|
85
|
-
# # => verify fails
|
90
|
+
# object.expected_method # => unexpected invocation
|
86
91
|
#
|
87
92
|
# object = mock()
|
88
93
|
# object.expects(:expected_method).once
|
89
94
|
# # => verify fails
|
90
95
|
def once
|
91
|
-
@cardinality
|
96
|
+
@cardinality.exactly(1)
|
92
97
|
self
|
93
98
|
end
|
94
99
|
|
95
|
-
# :call-seq: never() -> expectation
|
96
|
-
#
|
97
100
|
# Modifies expectation so that the expected method must never be called.
|
101
|
+
#
|
102
|
+
# @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
|
103
|
+
#
|
104
|
+
# @example Expected method must never be called.
|
98
105
|
# object = mock()
|
99
106
|
# object.expects(:expected_method).never
|
100
|
-
# object.expected_method
|
101
|
-
# # => verify fails
|
107
|
+
# object.expected_method # => unexpected invocation
|
102
108
|
#
|
103
109
|
# object = mock()
|
104
110
|
# object.expects(:expected_method).never
|
105
111
|
# # => verify succeeds
|
106
112
|
def never
|
107
|
-
@cardinality
|
113
|
+
@cardinality.exactly(0)
|
108
114
|
self
|
109
115
|
end
|
110
116
|
|
111
|
-
# :call-seq: at_least(minimum_number_of_times) -> expectation
|
112
|
-
#
|
113
117
|
# Modifies expectation so that the expected method must be called at least a +minimum_number_of_times+.
|
118
|
+
#
|
119
|
+
# @param [Integer] minimum_number_of_times minimum number of expected invocations.
|
120
|
+
# @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
|
121
|
+
#
|
122
|
+
# @example Expected method must be called at least twice.
|
114
123
|
# object = mock()
|
115
124
|
# object.expects(:expected_method).at_least(2)
|
116
125
|
# 3.times { object.expected_method }
|
@@ -121,13 +130,15 @@ module Mocha # :nodoc:
|
|
121
130
|
# object.expected_method
|
122
131
|
# # => verify fails
|
123
132
|
def at_least(minimum_number_of_times)
|
124
|
-
@cardinality
|
133
|
+
@cardinality.at_least(minimum_number_of_times)
|
125
134
|
self
|
126
135
|
end
|
127
136
|
|
128
|
-
# :call-seq: at_least_once() -> expectation
|
129
|
-
#
|
130
137
|
# Modifies expectation so that the expected method must be called at least once.
|
138
|
+
#
|
139
|
+
# @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
|
140
|
+
#
|
141
|
+
# @example Expected method must be called at least once.
|
131
142
|
# object = mock()
|
132
143
|
# object.expects(:expected_method).at_least_once
|
133
144
|
# object.expected_method
|
@@ -138,12 +149,14 @@ module Mocha # :nodoc:
|
|
138
149
|
# # => verify fails
|
139
150
|
def at_least_once
|
140
151
|
at_least(1)
|
141
|
-
self
|
142
152
|
end
|
143
153
|
|
144
|
-
# :call-seq: at_most(maximum_number_of_times) -> expectation
|
145
|
-
#
|
146
154
|
# Modifies expectation so that the expected method must be called at most a +maximum_number_of_times+.
|
155
|
+
#
|
156
|
+
# @param [Integer] maximum_number_of_times maximum number of expected invocations.
|
157
|
+
# @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
|
158
|
+
#
|
159
|
+
# @example Expected method must be called at most twice.
|
147
160
|
# object = mock()
|
148
161
|
# object.expects(:expected_method).at_most(2)
|
149
162
|
# 2.times { object.expected_method }
|
@@ -151,16 +164,17 @@ module Mocha # :nodoc:
|
|
151
164
|
#
|
152
165
|
# object = mock()
|
153
166
|
# object.expects(:expected_method).at_most(2)
|
154
|
-
# 3.times { object.expected_method }
|
155
|
-
# # => verify fails
|
167
|
+
# 3.times { object.expected_method } # => unexpected invocation
|
156
168
|
def at_most(maximum_number_of_times)
|
157
|
-
@cardinality
|
169
|
+
@cardinality.at_most(maximum_number_of_times)
|
158
170
|
self
|
159
171
|
end
|
160
172
|
|
161
|
-
# :call-seq: at_most_once() -> expectation
|
162
|
-
#
|
163
173
|
# Modifies expectation so that the expected method must be called at most once.
|
174
|
+
#
|
175
|
+
# @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
|
176
|
+
#
|
177
|
+
# @example Expected method must be called at most once.
|
164
178
|
# object = mock()
|
165
179
|
# object.expects(:expected_method).at_most_once
|
166
180
|
# object.expected_method
|
@@ -168,16 +182,22 @@ module Mocha # :nodoc:
|
|
168
182
|
#
|
169
183
|
# object = mock()
|
170
184
|
# object.expects(:expected_method).at_most_once
|
171
|
-
# 2.times { object.expected_method }
|
172
|
-
|
173
|
-
def at_most_once()
|
185
|
+
# 2.times { object.expected_method } # => unexpected invocation
|
186
|
+
def at_most_once
|
174
187
|
at_most(1)
|
175
|
-
self
|
176
188
|
end
|
177
189
|
|
178
|
-
# :call-seq: with(*expected_parameters, &matching_block) -> expectation
|
179
|
-
#
|
180
190
|
# Modifies expectation so that the expected method must be called with +expected_parameters+.
|
191
|
+
#
|
192
|
+
# May be used with parameter matchers in {ParameterMatchers}.
|
193
|
+
#
|
194
|
+
# @param [*Array] expected_parameters parameters expected.
|
195
|
+
# @yield optional block specifying custom matching.
|
196
|
+
# @yieldparam [*Array] actual_parameters parameters with which expected method was invoked.
|
197
|
+
# @yieldreturn [Boolean] +true+ if +actual_parameters+ are acceptable.
|
198
|
+
# @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
|
199
|
+
#
|
200
|
+
# @example Expected method must be called with expected parameters.
|
181
201
|
# object = mock()
|
182
202
|
# object.expects(:expected_method).with(:param1, :param2)
|
183
203
|
# object.expected_method(:param1, :param2)
|
@@ -187,10 +207,8 @@ module Mocha # :nodoc:
|
|
187
207
|
# object.expects(:expected_method).with(:param1, :param2)
|
188
208
|
# object.expected_method(:param3)
|
189
209
|
# # => verify fails
|
190
|
-
# May be used with parameter matchers in Mocha::ParameterMatchers.
|
191
210
|
#
|
192
|
-
#
|
193
|
-
# The expectation is matched if the block evaluates to +true+.
|
211
|
+
# @example Expected method must be called with a value divisible by 4.
|
194
212
|
# object = mock()
|
195
213
|
# object.expects(:expected_method).with() { |value| value % 4 == 0 }
|
196
214
|
# object.expected_method(16)
|
@@ -205,79 +223,148 @@ module Mocha # :nodoc:
|
|
205
223
|
self
|
206
224
|
end
|
207
225
|
|
208
|
-
#
|
226
|
+
# Modifies expectation so that the expected method must be called with a block.
|
227
|
+
#
|
228
|
+
# @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
|
209
229
|
#
|
210
|
-
#
|
230
|
+
# @example Expected method must be called with a block.
|
211
231
|
# object = mock()
|
212
|
-
# object.expects(:expected_method).
|
213
|
-
#
|
214
|
-
#
|
215
|
-
#
|
216
|
-
# May be called multiple times on the same expectation for consecutive invocations. Also see Expectation#then.
|
232
|
+
# object.expects(:expected_method).with_block_given
|
233
|
+
# object.expected_method { 1 + 1 }
|
234
|
+
# # => verify succeeds
|
235
|
+
#
|
217
236
|
# object = mock()
|
218
|
-
# object.
|
219
|
-
#
|
220
|
-
#
|
221
|
-
|
222
|
-
|
223
|
-
# yielded_values_from_first_invocation # => [1]
|
224
|
-
# yielded_values_from_second_invocation # => [2]
|
225
|
-
def yields(*parameters)
|
226
|
-
@yield_parameters.add(*parameters)
|
237
|
+
# object.expects(:expected_method).with_block_given
|
238
|
+
# object.expected_method
|
239
|
+
# # => verify fails
|
240
|
+
def with_block_given
|
241
|
+
@block_matcher = BlockMatchers::BlockGiven.new
|
227
242
|
self
|
228
243
|
end
|
229
244
|
|
230
|
-
#
|
231
|
-
#
|
232
|
-
# Modifies expectation so that when the expected method is called, it yields multiple times per invocation with the specified +parameter_groups+.
|
245
|
+
# Modifies expectation so that the expected method must be called without a block.
|
233
246
|
#
|
234
|
-
#
|
247
|
+
# @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
|
235
248
|
#
|
249
|
+
# @example Expected method must be called without a block.
|
236
250
|
# object = mock()
|
237
|
-
# object.expects(:expected_method).
|
238
|
-
#
|
239
|
-
#
|
240
|
-
#
|
241
|
-
# May be called multiple times on the same expectation for consecutive invocations. Also see Expectation#then.
|
251
|
+
# object.expects(:expected_method).with_no_block_given
|
252
|
+
# object.expected_method
|
253
|
+
# # => verify succeeds
|
254
|
+
#
|
242
255
|
# object = mock()
|
243
|
-
# object.
|
244
|
-
#
|
245
|
-
#
|
246
|
-
|
247
|
-
|
248
|
-
# yielded_values_from_first_invocation # => [[1, 2], [3]]
|
249
|
-
# yielded_values_from_second_invocation # => [[4], [5, 6]]
|
250
|
-
def multiple_yields(*parameter_groups)
|
251
|
-
@yield_parameters.multiple_add(*parameter_groups)
|
256
|
+
# object.expects(:expected_method).with_block_given
|
257
|
+
# object.expected_method { 1 + 1 }
|
258
|
+
# # => verify fails
|
259
|
+
def with_no_block_given
|
260
|
+
@block_matcher = BlockMatchers::NoBlockGiven.new
|
252
261
|
self
|
253
262
|
end
|
254
263
|
|
255
|
-
#
|
256
|
-
#
|
264
|
+
# Modifies expectation so that when the expected method is called, it yields to the block with the specified +parameters+.
|
265
|
+
#
|
266
|
+
# If no +parameters+ are specified, it yields to the block without any parameters.
|
267
|
+
#
|
268
|
+
# If no block is provided, the method will still attempt to yield resulting in a +LocalJumpError+. Note that this is what would happen if a "real" (non-mock) method implementation tried to yield to a non-existent block.
|
269
|
+
#
|
270
|
+
# May be called multiple times on the same expectation for consecutive invocations.
|
271
|
+
#
|
272
|
+
# @param [*Array] parameters parameters to be yielded.
|
273
|
+
# @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
|
274
|
+
# @see #then
|
275
|
+
#
|
276
|
+
# @example Yield when expected method is invoked.
|
277
|
+
# benchmark = mock()
|
278
|
+
# benchmark.expects(:measure).yields
|
279
|
+
# yielded = false
|
280
|
+
# benchmark.measure { yielded = true }
|
281
|
+
# yielded # => true
|
282
|
+
#
|
283
|
+
# @example Yield parameters when expected method is invoked.
|
284
|
+
# fibonacci = mock()
|
285
|
+
# fibonacci.expects(:next_pair).yields(0, 1)
|
286
|
+
# sum = 0
|
287
|
+
# fibonacci.next_pair { |first, second| sum = first + second }
|
288
|
+
# sum # => 1
|
289
|
+
#
|
290
|
+
# @example Yield different parameters on different invocations of the expected method.
|
291
|
+
# fibonacci = mock()
|
292
|
+
# fibonacci.expects(:next_pair).yields(0, 1).then.yields(1, 1)
|
293
|
+
# sum = 0
|
294
|
+
# fibonacci.next_pair { |first, second| sum = first + second }
|
295
|
+
# sum # => 1
|
296
|
+
# fibonacci.next_pair { |first, second| sum = first + second }
|
297
|
+
# sum # => 2
|
298
|
+
def yields(*parameters)
|
299
|
+
multiple_yields(parameters)
|
300
|
+
end
|
301
|
+
|
302
|
+
# Modifies expectation so that when the expected method is called, it yields multiple times per invocation with the specified +parameter_groups+.
|
257
303
|
#
|
304
|
+
# If no block is provided, the method will still attempt to yield resulting in a +LocalJumpError+. Note that this is what would happen if a "real" (non-mock) method implementation tried to yield to a non-existent block.
|
305
|
+
#
|
306
|
+
# @param [*Array<Array>] parameter_groups each element of +parameter_groups+ should iself be an +Array+ representing the parameters to be passed to the block for a single yield. Any element of +parameter_groups+ that is not an +Array+ is wrapped in an +Array+.
|
307
|
+
# @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
|
308
|
+
# @see #then
|
309
|
+
#
|
310
|
+
# @example When +foreach+ is called, the stub will invoke the block twice, the first time it passes ['row1_col1', 'row1_col2'] as the parameters, and the second time it passes ['row2_col1', ''] as the parameters.
|
311
|
+
# csv = mock()
|
312
|
+
# csv.expects(:foreach).with("path/to/file.csv").multiple_yields(['row1_col1', 'row1_col2'], ['row2_col1', ''])
|
313
|
+
# rows = []
|
314
|
+
# csv.foreach { |row| rows << row }
|
315
|
+
# rows # => [['row1_col1', 'row1_col2'], ['row2_col1', '']]
|
316
|
+
#
|
317
|
+
# @example Yield different groups of parameters on different invocations of the expected method. Simulating a situation where the CSV file at 'path/to/file.csv' has been modified between the two calls to +foreach+.
|
318
|
+
# csv = mock()
|
319
|
+
# csv.stubs(:foreach).with("path/to/file.csv").multiple_yields(['old_row1_col1', 'old_row1_col2'], ['old_row2_col1', '']).then.multiple_yields(['new_row1_col1', ''], ['new_row2_col1', 'new_row2_col2'])
|
320
|
+
# rows_from_first_invocation = []
|
321
|
+
# rows_from_second_invocation = []
|
322
|
+
# csv.foreach { |row| rows_from_first_invocation << row } # first invocation
|
323
|
+
# csv.foreach { |row| rows_from_second_invocation << row } # second invocation
|
324
|
+
# rows_from_first_invocation # => [['old_row1_col1', 'old_row1_col2'], ['old_row2_col1', '']]
|
325
|
+
# rows_from_second_invocation # => [['new_row1_col1', ''], ['new_row2_col1', 'new_row2_col2']]
|
326
|
+
def multiple_yields(*parameter_groups)
|
327
|
+
@yield_parameters.add(*parameter_groups)
|
328
|
+
self
|
329
|
+
end
|
330
|
+
|
258
331
|
# Modifies expectation so that when the expected method is called, it returns the specified +value+.
|
332
|
+
#
|
333
|
+
# @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
|
334
|
+
# @see #then
|
335
|
+
#
|
336
|
+
# @overload def returns(value)
|
337
|
+
# @param [Object] value value to return on invocation of expected method.
|
338
|
+
# @overload def returns(*values)
|
339
|
+
# @param [*Array] values values to return on consecutive invocations of expected method.
|
340
|
+
#
|
341
|
+
# @example Return the same value on every invocation.
|
259
342
|
# object = mock()
|
260
343
|
# object.stubs(:stubbed_method).returns('result')
|
261
344
|
# object.stubbed_method # => 'result'
|
262
345
|
# object.stubbed_method # => 'result'
|
263
|
-
#
|
346
|
+
#
|
347
|
+
# @example Return a different value on consecutive invocations.
|
264
348
|
# object = mock()
|
265
349
|
# object.stubs(:stubbed_method).returns(1, 2)
|
266
350
|
# object.stubbed_method # => 1
|
267
351
|
# object.stubbed_method # => 2
|
268
|
-
#
|
352
|
+
#
|
353
|
+
# @example Alternative way to return a different value on consecutive invocations.
|
269
354
|
# object = mock()
|
270
355
|
# object.stubs(:expected_method).returns(1, 2).then.returns(3)
|
271
356
|
# object.expected_method # => 1
|
272
357
|
# object.expected_method # => 2
|
273
358
|
# object.expected_method # => 3
|
274
|
-
#
|
359
|
+
#
|
360
|
+
# @example May be called in conjunction with {#raises} on the same expectation.
|
275
361
|
# object = mock()
|
276
362
|
# object.stubs(:expected_method).returns(1, 2).then.raises(Exception)
|
277
363
|
# object.expected_method # => 1
|
278
364
|
# object.expected_method # => 2
|
279
365
|
# object.expected_method # => raises exception of class Exception1
|
280
|
-
#
|
366
|
+
#
|
367
|
+
# @example Note that in Ruby a method returning multiple values is exactly equivalent to a method returning an +Array+ of those values.
|
281
368
|
# object = mock()
|
282
369
|
# object.stubs(:expected_method).returns([1, 2])
|
283
370
|
# x, y = object.expected_method
|
@@ -288,22 +375,36 @@ module Mocha # :nodoc:
|
|
288
375
|
self
|
289
376
|
end
|
290
377
|
|
291
|
-
#
|
378
|
+
# Modifies expectation so that when the expected method is called, it raises the specified +exception+ with the specified +message+ i.e. calls +Kernel#raise(exception, message)+.
|
379
|
+
#
|
380
|
+
# @param [Class,Exception,String,#exception] exception exception to be raised or message to be passed to RuntimeError.
|
381
|
+
# @param [String] message exception message.
|
382
|
+
# @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
|
292
383
|
#
|
293
|
-
#
|
384
|
+
# @see Kernel#raise
|
385
|
+
# @see #then
|
386
|
+
#
|
387
|
+
# @overload def raises
|
388
|
+
# @overload def raises(exception)
|
389
|
+
# @overload def raises(exception, message)
|
390
|
+
#
|
391
|
+
# @example Raise specified exception if expected method is invoked.
|
294
392
|
# object = stub()
|
295
393
|
# object.stubs(:expected_method).raises(Exception, 'message')
|
296
394
|
# object.expected_method # => raises exception of class Exception and with message 'message'
|
297
|
-
#
|
395
|
+
#
|
396
|
+
# @example Raise custom exception with extra constructor parameters by passing in an instance of the exception.
|
298
397
|
# object = stub()
|
299
398
|
# object.stubs(:expected_method).raises(MyException.new('message', 1, 2, 3))
|
300
399
|
# object.expected_method # => raises the specified instance of MyException
|
301
|
-
#
|
400
|
+
#
|
401
|
+
# @example Raise different exceptions on consecutive invocations of the expected method.
|
302
402
|
# object = stub()
|
303
403
|
# object.stubs(:expected_method).raises(Exception1).then.raises(Exception2)
|
304
404
|
# object.expected_method # => raises exception of class Exception1
|
305
405
|
# object.expected_method # => raises exception of class Exception2
|
306
|
-
#
|
406
|
+
#
|
407
|
+
# @example Raise an exception on first invocation of expected method and then return values on subsequent invocations.
|
307
408
|
# object = stub()
|
308
409
|
# object.stubs(:expected_method).raises(Exception).then.returns(2, 3)
|
309
410
|
# object.expected_method # => raises exception of class Exception1
|
@@ -314,22 +415,35 @@ module Mocha # :nodoc:
|
|
314
415
|
self
|
315
416
|
end
|
316
417
|
|
317
|
-
#
|
418
|
+
# Modifies expectation so that when the expected method is called, it throws the specified +tag+ with the specific return value +object+ i.e. calls +Kernel#throw(tag, object)+.
|
419
|
+
#
|
420
|
+
# @param [Symbol,String] tag tag to throw to transfer control to the active catch block.
|
421
|
+
# @param [Object] object return value for the catch block.
|
422
|
+
# @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
|
423
|
+
#
|
424
|
+
# @see Kernel#throw
|
425
|
+
# @see #then
|
318
426
|
#
|
319
|
-
#
|
427
|
+
# @overload def throw(tag)
|
428
|
+
# @overload def throw(tag, object)
|
429
|
+
#
|
430
|
+
# @example Throw tag when expected method is invoked.
|
320
431
|
# object = stub()
|
321
432
|
# object.stubs(:expected_method).throws(:done)
|
322
433
|
# object.expected_method # => throws tag :done
|
323
|
-
#
|
434
|
+
#
|
435
|
+
# @example Throw tag with return value +object+ c.f. +Kernel#throw+.
|
324
436
|
# object = stub()
|
325
437
|
# object.stubs(:expected_method).throws(:done, 'result')
|
326
438
|
# object.expected_method # => throws tag :done and causes catch block to return 'result'
|
327
|
-
#
|
439
|
+
#
|
440
|
+
# @example Throw different tags on consecutive invocations of the expected method.
|
328
441
|
# object = stub()
|
329
442
|
# object.stubs(:expected_method).throws(:done).then.throws(:continue)
|
330
443
|
# object.expected_method # => throws :done
|
331
444
|
# object.expected_method # => throws :continue
|
332
|
-
#
|
445
|
+
#
|
446
|
+
# @example Throw tag on first invocation of expected method and then return values for subsequent invocations.
|
333
447
|
# object = stub()
|
334
448
|
# object.stubs(:expected_method).throws(:done).then.returns(2, 3)
|
335
449
|
# object.expected_method # => throws :done
|
@@ -340,10 +454,19 @@ module Mocha # :nodoc:
|
|
340
454
|
self
|
341
455
|
end
|
342
456
|
|
343
|
-
#
|
344
|
-
#
|
457
|
+
# @overload def then
|
458
|
+
# Used as syntactic sugar to improve readability. It has no effect on state of the expectation.
|
459
|
+
# @overload def then(state)
|
460
|
+
# Used to change the +state_machine+ to the specified state when the expected invocation occurs.
|
461
|
+
# @param [StateMachine::State] state state_machine.is(state_name) provides a mechanism to change the +state_machine+ into the state specified by +state_name+ when the expected method is invoked.
|
462
|
+
#
|
463
|
+
# @see API#states
|
464
|
+
# @see StateMachine
|
465
|
+
# @see #when
|
466
|
+
#
|
467
|
+
# @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
|
345
468
|
#
|
346
|
-
#
|
469
|
+
# @example Using {#then} as syntactic sugar when specifying values to be returned and exceptions to be raised on consecutive invocations of the expected method.
|
347
470
|
# object = mock()
|
348
471
|
# object.stubs(:expected_method).returns(1, 2).then.raises(Exception).then.returns(4)
|
349
472
|
# object.expected_method # => 1
|
@@ -351,9 +474,7 @@ module Mocha # :nodoc:
|
|
351
474
|
# object.expected_method # => raises exception of class Exception
|
352
475
|
# object.expected_method # => 4
|
353
476
|
#
|
354
|
-
#
|
355
|
-
#
|
356
|
-
# See also API#states, StateMachine and Expectation#when.
|
477
|
+
# @example Using {#then} to change the +state+ of a +state_machine+ on the invocation of an expected method.
|
357
478
|
# power = states('power').starts_as('off')
|
358
479
|
#
|
359
480
|
# radio = mock('radio')
|
@@ -363,19 +484,21 @@ module Mocha # :nodoc:
|
|
363
484
|
# radio.expects(:select_channel).with('BBC World Service').when(power.is('on'))
|
364
485
|
# radio.expects(:adjust_volume).with(-5).when(power.is('on'))
|
365
486
|
# radio.expects(:switch_off).then(power.is('off'))
|
366
|
-
def then(
|
367
|
-
|
368
|
-
state = parameters.first
|
369
|
-
add_side_effect(ChangeStateSideEffect.new(state))
|
370
|
-
end
|
487
|
+
def then(state = nil)
|
488
|
+
add_side_effect(ChangeStateSideEffect.new(state)) if state
|
371
489
|
self
|
372
490
|
end
|
373
491
|
|
374
|
-
#
|
492
|
+
# Constrains the expectation to occur only when the +state_machine+ is in the state specified by +state_predicate+.
|
493
|
+
#
|
494
|
+
# @param [StateMachine::StatePredicate] state_predicate +state_machine.is(state_name)+ provides a mechanism to determine whether the +state_machine+ is in the state specified by +state_predicate+ when the expected method is invoked.
|
495
|
+
# @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
|
375
496
|
#
|
376
|
-
#
|
497
|
+
# @see API#states
|
498
|
+
# @see StateMachine
|
499
|
+
# @see #then
|
377
500
|
#
|
378
|
-
#
|
501
|
+
# @example Using {#when} to only allow invocation of methods when "power" state machine is in the "on" state.
|
379
502
|
# power = states('power').starts_as('off')
|
380
503
|
#
|
381
504
|
# radio = mock('radio')
|
@@ -390,120 +513,136 @@ module Mocha # :nodoc:
|
|
390
513
|
self
|
391
514
|
end
|
392
515
|
|
393
|
-
#
|
516
|
+
# Constrains the expectation so that it must be invoked at the current point in the +sequence+.
|
394
517
|
#
|
395
|
-
#
|
518
|
+
# To expect a sequence of invocations, write the expectations in order and add the +in_sequence(sequence)+ clause to each one.
|
396
519
|
#
|
397
|
-
#
|
520
|
+
# Expectations in a +sequence+ can have any invocation count.
|
398
521
|
#
|
399
|
-
#
|
522
|
+
# If an expectation in a sequence is stubbed, rather than expected, it can be skipped in the +sequence+.
|
400
523
|
#
|
401
|
-
#
|
524
|
+
# An expected method can appear in multiple sequences.
|
402
525
|
#
|
403
|
-
#
|
526
|
+
# @param [Sequence] sequence sequence in which expected method should appear.
|
527
|
+
# @param [*Array<Sequence>] sequences more sequences in which expected method should appear.
|
528
|
+
# @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
|
529
|
+
#
|
530
|
+
# @see API#sequence
|
531
|
+
#
|
532
|
+
# @example Ensure methods are invoked in a specified order.
|
404
533
|
# breakfast = sequence('breakfast')
|
405
534
|
#
|
406
535
|
# egg = mock('egg')
|
407
536
|
# egg.expects(:crack).in_sequence(breakfast)
|
408
537
|
# egg.expects(:fry).in_sequence(breakfast)
|
409
538
|
# egg.expects(:eat).in_sequence(breakfast)
|
410
|
-
def in_sequence(*sequences)
|
411
|
-
sequences.each { |
|
539
|
+
def in_sequence(sequence, *sequences)
|
540
|
+
sequences.unshift(sequence).each { |seq| add_in_sequence_ordering_constraint(seq) }
|
412
541
|
self
|
413
542
|
end
|
414
543
|
|
415
|
-
#
|
416
|
-
|
544
|
+
# @private
|
417
545
|
attr_reader :backtrace
|
418
546
|
|
547
|
+
# @private
|
419
548
|
def initialize(mock, expected_method_name, backtrace = nil)
|
420
549
|
@mock = mock
|
421
550
|
@method_matcher = MethodMatcher.new(expected_method_name.to_sym)
|
422
551
|
@parameters_matcher = ParametersMatcher.new
|
552
|
+
@block_matcher = BlockMatchers::OptionalBlock.new
|
423
553
|
@ordering_constraints = []
|
424
554
|
@side_effects = []
|
425
|
-
@cardinality
|
555
|
+
@cardinality = Cardinality.new.exactly(1)
|
426
556
|
@return_values = ReturnValues.new
|
427
557
|
@yield_parameters = YieldParameters.new
|
428
558
|
@backtrace = backtrace || caller
|
429
559
|
end
|
430
560
|
|
561
|
+
# @private
|
431
562
|
def add_ordering_constraint(ordering_constraint)
|
432
563
|
@ordering_constraints << ordering_constraint
|
433
564
|
end
|
434
565
|
|
566
|
+
# @private
|
435
567
|
def add_in_sequence_ordering_constraint(sequence)
|
436
568
|
sequence.constrain_as_next_in_sequence(self)
|
437
569
|
end
|
438
570
|
|
571
|
+
# @private
|
439
572
|
def add_side_effect(side_effect)
|
440
573
|
@side_effects << side_effect
|
441
574
|
end
|
442
575
|
|
576
|
+
# @private
|
443
577
|
def perform_side_effects
|
444
|
-
@side_effects.each
|
578
|
+
@side_effects.each(&:perform)
|
445
579
|
end
|
446
580
|
|
581
|
+
# @private
|
447
582
|
def in_correct_order?
|
448
|
-
@ordering_constraints.all?
|
583
|
+
@ordering_constraints.all?(&:allows_invocation_now?)
|
449
584
|
end
|
450
585
|
|
586
|
+
# @private
|
451
587
|
def matches_method?(method_name)
|
452
588
|
@method_matcher.match?(method_name)
|
453
589
|
end
|
454
590
|
|
455
|
-
|
456
|
-
|
591
|
+
# @private
|
592
|
+
def match?(invocation)
|
593
|
+
@method_matcher.match?(invocation.method_name) && @parameters_matcher.match?(invocation.arguments) && @block_matcher.match?(invocation.block) && in_correct_order?
|
457
594
|
end
|
458
595
|
|
596
|
+
# @private
|
459
597
|
def invocations_allowed?
|
460
|
-
@cardinality.invocations_allowed?
|
598
|
+
@cardinality.invocations_allowed?
|
461
599
|
end
|
462
600
|
|
601
|
+
# @private
|
463
602
|
def satisfied?
|
464
|
-
@cardinality.satisfied?
|
603
|
+
@cardinality.satisfied?
|
465
604
|
end
|
466
605
|
|
467
|
-
|
468
|
-
|
469
|
-
perform_side_effects
|
470
|
-
|
471
|
-
|
472
|
-
yield(*yield_parameters)
|
473
|
-
end
|
474
|
-
end
|
475
|
-
@return_values.next
|
606
|
+
# @private
|
607
|
+
def invoke(invocation)
|
608
|
+
perform_side_effects
|
609
|
+
@cardinality << invocation
|
610
|
+
invocation.call(@yield_parameters, @return_values)
|
476
611
|
end
|
477
612
|
|
613
|
+
# @private
|
478
614
|
def verified?(assertion_counter = nil)
|
479
615
|
assertion_counter.increment if assertion_counter && @cardinality.needs_verifying?
|
480
|
-
@cardinality.verified?
|
616
|
+
@cardinality.verified?
|
481
617
|
end
|
482
618
|
|
619
|
+
# @private
|
483
620
|
def used?
|
484
|
-
@cardinality.used?
|
621
|
+
@cardinality.used?
|
622
|
+
end
|
623
|
+
|
624
|
+
# @private
|
625
|
+
def inspect
|
626
|
+
address = __id__ * 2
|
627
|
+
address += 0x100000000 if address < 0
|
628
|
+
"#<Expectation:0x#{format('%x', address)} #{mocha_inspect} >"
|
485
629
|
end
|
486
630
|
|
631
|
+
# @private
|
487
632
|
def mocha_inspect
|
488
|
-
message = "#{@cardinality.
|
489
|
-
message <<
|
490
|
-
|
491
|
-
|
492
|
-
when 2 then "invoked twice"
|
493
|
-
else "invoked #{@invocation_count} times"
|
633
|
+
message = "#{@cardinality.anticipated_times}, #{@cardinality.invoked_times}: #{method_signature}"
|
634
|
+
message << "; #{@ordering_constraints.map(&:mocha_inspect).join('; ')}" unless @ordering_constraints.empty?
|
635
|
+
if Mocha.configuration.display_matching_invocations_on_failure?
|
636
|
+
message << @cardinality.actual_invocations
|
494
637
|
end
|
495
|
-
message << ": "
|
496
|
-
message << method_signature
|
497
|
-
message << "; #{@ordering_constraints.map { |oc| oc.mocha_inspect }.join("; ")}" unless @ordering_constraints.empty?
|
498
638
|
message
|
499
639
|
end
|
500
640
|
|
641
|
+
# @private
|
501
642
|
def method_signature
|
502
|
-
"#{@mock.mocha_inspect}.#{@method_matcher.mocha_inspect}#{@parameters_matcher.mocha_inspect}"
|
643
|
+
signature = "#{@mock.mocha_inspect}.#{@method_matcher.mocha_inspect}#{@parameters_matcher.mocha_inspect}"
|
644
|
+
signature << " #{@block_matcher.mocha_inspect}" if @block_matcher.mocha_inspect
|
645
|
+
signature
|
503
646
|
end
|
504
|
-
|
505
|
-
# :startdoc:
|
506
|
-
|
507
647
|
end
|
508
|
-
|
509
648
|
end
|