mocha 0.10.5 → 0.11.0
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING.rdoc +1 -1
- data/README.rdoc +6 -4
- data/RELEASE.rdoc +12 -0
- data/Rakefile +29 -60
- data/doc/Mocha.html +112 -0
- data/doc/Mocha/API.html +898 -0
- data/doc/Mocha/ClassMethods.html +246 -0
- data/doc/Mocha/Configuration.html +471 -0
- data/doc/Mocha/Expectation.html +2570 -0
- data/doc/Mocha/Mock.html +830 -0
- data/doc/Mocha/ObjectMethods.html +668 -0
- data/doc/Mocha/ParameterMatchers.html +2715 -0
- data/doc/Mocha/ParameterMatchers/AllOf.html +137 -0
- data/doc/Mocha/ParameterMatchers/AnyOf.html +137 -0
- data/doc/Mocha/ParameterMatchers/AnyParameters.html +136 -0
- data/doc/Mocha/ParameterMatchers/Anything.html +136 -0
- data/doc/Mocha/ParameterMatchers/Base.html +419 -0
- data/doc/Mocha/ParameterMatchers/Equals.html +137 -0
- data/doc/Mocha/ParameterMatchers/HasEntries.html +137 -0
- data/doc/Mocha/ParameterMatchers/HasEntry.html +137 -0
- data/doc/Mocha/ParameterMatchers/HasKey.html +137 -0
- data/doc/Mocha/ParameterMatchers/HasValue.html +137 -0
- data/doc/Mocha/ParameterMatchers/Includes.html +137 -0
- data/doc/Mocha/ParameterMatchers/InstanceOf.html +137 -0
- data/doc/Mocha/ParameterMatchers/IsA.html +136 -0
- data/doc/Mocha/ParameterMatchers/KindOf.html +137 -0
- data/doc/Mocha/ParameterMatchers/Not.html +137 -0
- data/doc/Mocha/ParameterMatchers/Optionally.html +136 -0
- data/doc/Mocha/ParameterMatchers/QueryStringMatches.html +136 -0
- data/doc/Mocha/ParameterMatchers/RegexpMatches.html +137 -0
- data/doc/Mocha/ParameterMatchers/RespondsWith.html +137 -0
- data/doc/Mocha/ParameterMatchers/YamlEquivalent.html +137 -0
- data/doc/Mocha/Sequence.html +133 -0
- data/doc/Mocha/StateMachine.html +510 -0
- data/doc/Mocha/StateMachine/State.html +125 -0
- data/doc/Mocha/StateMachine/StatePredicate.html +125 -0
- data/doc/Mocha/StubbingError.html +134 -0
- data/doc/Mocha/UnexpectedInvocation.html +124 -0
- data/doc/_index.html +481 -0
- data/doc/class_list.html +47 -0
- data/doc/css/common.css +1 -0
- data/doc/css/full_list.css +55 -0
- data/doc/css/style.css +322 -0
- data/doc/file.COPYING.html +72 -0
- data/doc/file.MIT-LICENSE.html +86 -0
- data/doc/file.README.html +153 -0
- data/doc/file.RELEASE.html +933 -0
- data/doc/file.misc.html +108 -0
- data/doc/file.mocha.html +90 -0
- data/doc/file.stubba.html +129 -0
- data/doc/file_list.html +67 -0
- data/doc/frames.html +13 -0
- data/doc/index.html +153 -0
- data/doc/js/app.js +205 -0
- data/doc/js/full_list.js +173 -0
- data/doc/js/jquery.js +16 -0
- data/doc/method_list.html +510 -0
- data/doc/top-level-namespace.html +105 -0
- data/lib/mocha/any_instance_method.rb +16 -11
- data/lib/mocha/api.rb +120 -106
- data/lib/mocha/argument_iterator.rb +6 -6
- data/lib/mocha/backtrace_filter.rb +1 -1
- data/lib/mocha/cardinality.rb +21 -21
- data/lib/mocha/central.rb +8 -8
- data/lib/mocha/change_state_side_effect.rb +5 -5
- data/lib/mocha/class_method.rb +14 -19
- data/lib/mocha/configuration.rb +47 -37
- data/lib/mocha/deprecation.rb +8 -8
- data/lib/mocha/exception_raiser.rb +7 -7
- data/lib/mocha/expectation.rb +173 -77
- data/lib/mocha/expectation_error.rb +2 -2
- data/lib/mocha/expectation_list.rb +11 -11
- data/lib/mocha/in_state_ordering_constraint.rb +5 -5
- data/lib/mocha/inspect.rb +8 -8
- data/lib/mocha/instance_method.rb +1 -17
- data/lib/mocha/integration.rb +9 -9
- data/lib/mocha/integration/mini_test/assertion_counter.rb +9 -9
- data/lib/mocha/integration/mini_test/exception_translation.rb +6 -6
- data/lib/mocha/integration/mini_test/version_13.rb +6 -6
- data/lib/mocha/integration/mini_test/version_140.rb +6 -6
- data/lib/mocha/integration/mini_test/version_141.rb +6 -6
- data/lib/mocha/integration/mini_test/version_142_to_172.rb +7 -7
- data/lib/mocha/integration/mini_test/version_200.rb +7 -7
- data/lib/mocha/integration/test_unit/assertion_counter.rb +10 -10
- data/lib/mocha/integration/test_unit/gem_version_200.rb +6 -6
- data/lib/mocha/integration/test_unit/gem_version_201_to_202.rb +6 -6
- data/lib/mocha/integration/test_unit/ruby_version_185_and_below.rb +6 -6
- data/lib/mocha/integration/test_unit/ruby_version_186_and_above.rb +6 -6
- data/lib/mocha/is_a.rb +1 -1
- data/lib/mocha/logger.rb +6 -6
- data/lib/mocha/method_matcher.rb +7 -7
- data/lib/mocha/mock.rb +85 -48
- data/lib/mocha/mockery.rb +47 -33
- data/lib/mocha/module_method.rb +2 -2
- data/lib/mocha/multiple_yields.rb +9 -9
- data/lib/mocha/names.rb +18 -18
- data/lib/mocha/no_yields.rb +7 -7
- data/lib/mocha/object.rb +104 -75
- data/lib/mocha/parameter_matchers.rb +3 -3
- data/lib/mocha/parameter_matchers/all_of.rb +22 -12
- data/lib/mocha/parameter_matchers/any_of.rb +23 -12
- data/lib/mocha/parameter_matchers/any_parameters.rb +15 -8
- data/lib/mocha/parameter_matchers/anything.rb +20 -11
- data/lib/mocha/parameter_matchers/base.rb +35 -27
- data/lib/mocha/parameter_matchers/equals.rb +23 -12
- data/lib/mocha/parameter_matchers/has_entries.rb +22 -12
- data/lib/mocha/parameter_matchers/has_entry.rb +31 -13
- data/lib/mocha/parameter_matchers/has_key.rb +21 -11
- data/lib/mocha/parameter_matchers/has_value.rb +21 -11
- data/lib/mocha/parameter_matchers/includes.rb +15 -5
- data/lib/mocha/parameter_matchers/instance_of.rb +23 -12
- data/lib/mocha/parameter_matchers/is_a.rb +23 -12
- data/lib/mocha/parameter_matchers/kind_of.rb +23 -12
- data/lib/mocha/parameter_matchers/not.rb +22 -12
- data/lib/mocha/parameter_matchers/object.rb +5 -3
- data/lib/mocha/parameter_matchers/optionally.rb +24 -12
- data/lib/mocha/parameter_matchers/query_string.rb +14 -3
- data/lib/mocha/parameter_matchers/regexp_matches.rb +21 -11
- data/lib/mocha/parameter_matchers/responds_with.rb +17 -6
- data/lib/mocha/parameter_matchers/yaml_equivalent.rb +16 -6
- data/lib/mocha/parameters_matcher.rb +8 -8
- data/lib/mocha/pretty_parameters.rb +7 -7
- data/lib/mocha/return_values.rb +11 -11
- data/lib/mocha/sequence.rb +23 -14
- data/lib/mocha/single_return_value.rb +7 -7
- data/lib/mocha/single_yield.rb +9 -9
- data/lib/mocha/standalone.rb +1 -1
- data/lib/mocha/state_machine.rb +61 -46
- data/lib/mocha/stubbing_error.rb +8 -5
- data/lib/mocha/thrower.rb +2 -2
- data/lib/mocha/unexpected_invocation.rb +9 -6
- data/lib/mocha/version.rb +1 -1
- data/lib/mocha/yield_parameters.rb +10 -10
- data/lib/mocha_standalone.rb +1 -1
- data/mocha.gemspec +4 -7
- data/test/acceptance/acceptance_test_helper.rb +10 -10
- data/test/acceptance/api_test.rb +20 -20
- data/test/acceptance/bug_18914_test.rb +12 -12
- data/test/acceptance/bug_21465_test.rb +6 -6
- data/test/acceptance/bug_21563_test.rb +5 -5
- data/test/acceptance/exception_rescue_test.rb +1 -1
- data/test/acceptance/expected_invocation_count_test.rb +17 -17
- data/test/acceptance/failure_messages_test.rb +13 -13
- data/test/acceptance/minitest_test.rb +39 -39
- data/test/acceptance/mocha_example_test.rb +26 -26
- data/test/acceptance/mocha_test_result_test.rb +13 -13
- data/test/acceptance/mock_test.rb +5 -5
- data/test/acceptance/mock_with_initializer_block_test.rb +5 -5
- data/test/acceptance/mocked_methods_dispatch_test.rb +4 -4
- data/test/acceptance/multiple_expectations_failure_message_test.rb +2 -2
- data/test/acceptance/optional_parameters_test.rb +5 -5
- data/test/acceptance/parameter_matcher_test.rb +18 -18
- data/test/acceptance/partial_mocks_test.rb +9 -9
- data/test/acceptance/raise_exception_test.rb +1 -1
- data/test/acceptance/return_value_test.rb +5 -5
- data/test/acceptance/sequence_test.rb +29 -29
- data/test/acceptance/states_test.rb +17 -17
- data/test/acceptance/stub_any_instance_method_test.rb +13 -13
- data/test/acceptance/stub_class_method_defined_on_active_record_association_proxy_test.rb +7 -7
- data/test/acceptance/stub_everything_test.rb +5 -5
- data/test/acceptance/stub_instance_method_defined_on_singleton_class_test.rb +1 -1
- data/test/acceptance/stub_module_method_test.rb +17 -17
- data/test/acceptance/stub_test.rb +5 -5
- data/test/acceptance/stubba_example_test.rb +24 -24
- data/test/acceptance/stubba_test_result_test.rb +11 -11
- data/test/acceptance/stubbing_error_backtrace_test.rb +6 -6
- data/test/acceptance/stubbing_frozen_object_test.rb +88 -0
- data/test/acceptance/stubbing_method_unnecessarily_test.rb +9 -9
- data/test/acceptance/stubbing_nil_test.rb +59 -0
- data/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +13 -13
- data/test/acceptance/stubbing_non_existent_class_method_test.rb +15 -15
- data/test/acceptance/stubbing_non_existent_instance_method_test.rb +14 -14
- data/test/acceptance/stubbing_non_public_any_instance_method_test.rb +13 -13
- data/test/acceptance/stubbing_non_public_class_method_test.rb +12 -12
- data/test/acceptance/stubbing_non_public_instance_method_test.rb +12 -12
- data/test/acceptance/stubbing_on_non_mock_object_test.rb +9 -9
- data/test/acceptance/stubbing_same_class_method_on_parent_and_child_classes_test.rb +35 -0
- data/test/acceptance/throw_test.rb +1 -1
- data/test/deprecation_disabler.rb +1 -1
- data/test/execution_point.rb +7 -7
- data/test/method_definer.rb +1 -1
- data/test/mini_test_result.rb +21 -21
- data/test/simple_counter.rb +5 -5
- data/test/test_helper.rb +1 -1
- data/test/test_runner.rb +22 -24
- data/test/test_unit_result.rb +20 -0
- data/test/unit/any_instance_method_test.rb +28 -32
- data/test/unit/array_inspect_test.rb +3 -3
- data/test/unit/backtrace_filter_test.rb +3 -3
- data/test/unit/cardinality_test.rb +12 -12
- data/test/unit/central_test.rb +18 -18
- data/test/unit/change_state_side_effect_test.rb +12 -12
- data/test/unit/class_method_test.rb +33 -91
- data/test/unit/date_time_inspect_test.rb +4 -4
- data/test/unit/exception_raiser_test.rb +5 -5
- data/test/unit/expectation_list_test.rb +7 -7
- data/test/unit/expectation_test.rb +66 -66
- data/test/unit/hash_inspect_test.rb +4 -4
- data/test/unit/in_state_ordering_constraint_test.rb +13 -13
- data/test/unit/method_matcher_test.rb +3 -3
- data/test/unit/mock_test.rb +40 -40
- data/test/unit/mockery_test.rb +25 -25
- data/test/unit/multiple_yields_test.rb +2 -2
- data/test/unit/no_yields_test.rb +2 -2
- data/test/unit/object_inspect_test.rb +4 -4
- data/test/unit/object_test.rb +15 -15
- data/test/unit/parameter_matchers/all_of_test.rb +6 -6
- data/test/unit/parameter_matchers/any_of_test.rb +6 -6
- data/test/unit/parameter_matchers/anything_test.rb +5 -5
- data/test/unit/parameter_matchers/has_entries_test.rb +10 -10
- data/test/unit/parameter_matchers/has_entry_test.rb +13 -13
- data/test/unit/parameter_matchers/has_key_test.rb +11 -11
- data/test/unit/parameter_matchers/has_value_test.rb +12 -12
- data/test/unit/parameter_matchers/includes_test.rb +4 -4
- data/test/unit/parameter_matchers/not_test.rb +6 -6
- data/test/unit/parameter_matchers/regexp_matches_test.rb +9 -9
- data/test/unit/parameter_matchers/responds_with_test.rb +6 -6
- data/test/unit/parameter_matchers/stub_matcher.rb +4 -4
- data/test/unit/parameter_matchers/yaml_equivalent_test.rb +6 -6
- data/test/unit/parameters_matcher_test.rb +16 -16
- data/test/unit/return_values_test.rb +5 -5
- data/test/unit/sequence_test.rb +10 -10
- data/test/unit/single_return_value_test.rb +3 -3
- data/test/unit/single_yield_test.rb +2 -2
- data/test/unit/state_machine_test.rb +19 -19
- data/test/unit/string_inspect_test.rb +2 -2
- data/test/unit/thrower_test.rb +1 -1
- data/test/unit/yield_parameters_test.rb +11 -11
- metadata +113 -101
data/lib/mocha/cardinality.rb
CHANGED
@@ -1,60 +1,60 @@
|
|
1
1
|
module Mocha
|
2
2
|
|
3
3
|
class Cardinality
|
4
|
-
|
4
|
+
|
5
5
|
INFINITY = 1 / 0.0
|
6
|
-
|
6
|
+
|
7
7
|
class << self
|
8
|
-
|
8
|
+
|
9
9
|
def exactly(count)
|
10
10
|
new(count, count)
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
def at_least(count)
|
14
14
|
new(count, INFINITY)
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
def at_most(count)
|
18
18
|
new(0, count)
|
19
19
|
end
|
20
|
-
|
20
|
+
|
21
21
|
def times(range_or_count)
|
22
22
|
case range_or_count
|
23
23
|
when Range then new(range_or_count.first, range_or_count.last)
|
24
24
|
else new(range_or_count, range_or_count)
|
25
25
|
end
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
30
|
def initialize(required, maximum)
|
31
31
|
@required, @maximum = required, maximum
|
32
32
|
end
|
33
|
-
|
33
|
+
|
34
34
|
def invocations_allowed?(invocation_count)
|
35
35
|
invocation_count < maximum
|
36
36
|
end
|
37
|
-
|
37
|
+
|
38
38
|
def satisfied?(invocations_so_far)
|
39
39
|
invocations_so_far >= required
|
40
40
|
end
|
41
|
-
|
41
|
+
|
42
42
|
def needs_verifying?
|
43
43
|
!allowed_any_number_of_times?
|
44
44
|
end
|
45
|
-
|
45
|
+
|
46
46
|
def verified?(invocation_count)
|
47
47
|
(invocation_count >= required) && (invocation_count <= maximum)
|
48
48
|
end
|
49
|
-
|
49
|
+
|
50
50
|
def allowed_any_number_of_times?
|
51
51
|
required == 0 && infinite?(maximum)
|
52
52
|
end
|
53
|
-
|
53
|
+
|
54
54
|
def used?(invocation_count)
|
55
55
|
(invocation_count > 0) || (maximum == 0)
|
56
56
|
end
|
57
|
-
|
57
|
+
|
58
58
|
def mocha_inspect
|
59
59
|
if allowed_any_number_of_times?
|
60
60
|
"allowed any number of times"
|
@@ -72,11 +72,11 @@ module Mocha
|
|
72
72
|
end
|
73
73
|
end
|
74
74
|
end
|
75
|
-
|
75
|
+
|
76
76
|
protected
|
77
|
-
|
77
|
+
|
78
78
|
attr_reader :required, :maximum
|
79
|
-
|
79
|
+
|
80
80
|
def times(number)
|
81
81
|
case number
|
82
82
|
when 0 then "no times"
|
@@ -85,11 +85,11 @@ module Mocha
|
|
85
85
|
else "#{number} times"
|
86
86
|
end
|
87
87
|
end
|
88
|
-
|
88
|
+
|
89
89
|
def infinite?(number)
|
90
90
|
number.respond_to?(:infinite?) && number.infinite?
|
91
91
|
end
|
92
|
-
|
92
|
+
|
93
93
|
end
|
94
|
-
|
94
|
+
|
95
95
|
end
|
data/lib/mocha/central.rb
CHANGED
@@ -1,27 +1,27 @@
|
|
1
1
|
module Mocha
|
2
|
-
|
2
|
+
|
3
3
|
class Central
|
4
|
-
|
4
|
+
|
5
5
|
attr_accessor :stubba_methods
|
6
|
-
|
6
|
+
|
7
7
|
def initialize
|
8
8
|
self.stubba_methods = []
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
def stub(method)
|
12
12
|
unless stubba_methods.detect { |m| m.matches?(method) }
|
13
|
-
method.stub
|
13
|
+
method.stub
|
14
14
|
stubba_methods.push(method)
|
15
15
|
end
|
16
16
|
end
|
17
|
-
|
17
|
+
|
18
18
|
def unstub(method)
|
19
19
|
if existing = stubba_methods.detect { |m| m.matches?(method) }
|
20
20
|
existing.unstub
|
21
21
|
stubba_methods.delete(existing)
|
22
22
|
end
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
def unstub_all
|
26
26
|
while stubba_methods.any? do
|
27
27
|
unstub(stubba_methods.first)
|
@@ -30,4 +30,4 @@ module Mocha
|
|
30
30
|
|
31
31
|
end
|
32
32
|
|
33
|
-
end
|
33
|
+
end
|
@@ -1,19 +1,19 @@
|
|
1
1
|
module Mocha
|
2
2
|
|
3
3
|
class ChangeStateSideEffect
|
4
|
-
|
4
|
+
|
5
5
|
def initialize(state)
|
6
6
|
@state = state
|
7
7
|
end
|
8
|
-
|
8
|
+
|
9
9
|
def perform
|
10
10
|
@state.activate
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
def mocha_inspect
|
14
14
|
"then #{@state.mocha_inspect}"
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
end
|
18
18
|
|
19
|
-
end
|
19
|
+
end
|
data/lib/mocha/class_method.rb
CHANGED
@@ -7,7 +7,7 @@ module Mocha
|
|
7
7
|
attr_reader :stubbee, :method
|
8
8
|
|
9
9
|
def initialize(stubbee, method)
|
10
|
-
@stubbee = stubbee
|
10
|
+
@stubbee, @original_method = stubbee, nil
|
11
11
|
@method = RUBY_VERSION < '1.9' ? method.to_s : method.to_sym
|
12
12
|
end
|
13
13
|
|
@@ -36,7 +36,16 @@ module Mocha
|
|
36
36
|
def hide_original_method
|
37
37
|
if method_exists?(method)
|
38
38
|
begin
|
39
|
-
stubbee.
|
39
|
+
@original_method = stubbee.method(method)
|
40
|
+
if @original_method && @original_method.owner == stubbee.__metaclass__
|
41
|
+
@original_visibility = :public
|
42
|
+
if stubbee.__metaclass__.protected_instance_methods.include?(method)
|
43
|
+
@original_visibility = :protected
|
44
|
+
elsif stubbee.__metaclass__.private_instance_methods.include?(method)
|
45
|
+
@original_visibility = :private
|
46
|
+
end
|
47
|
+
stubbee.__metaclass__.send(:remove_method, method)
|
48
|
+
end
|
40
49
|
rescue NameError
|
41
50
|
# deal with nasties like ActiveRecord::Associations::AssociationProxy
|
42
51
|
end
|
@@ -56,24 +65,10 @@ module Mocha
|
|
56
65
|
end
|
57
66
|
|
58
67
|
def restore_original_method
|
59
|
-
if
|
60
|
-
|
61
|
-
|
62
|
-
stubbee.__metaclass__.send(:remove_method, hidden_method)
|
63
|
-
rescue NameError
|
64
|
-
# deal with nasties like ActiveRecord::Associations::AssociationProxy
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
def hidden_method
|
70
|
-
if RUBY_VERSION < '1.9'
|
71
|
-
method_name = method.to_s.gsub(/\W/) { |s| "_substituted_character_#{s[0]}_" }
|
72
|
-
else
|
73
|
-
method_name = method.to_s.gsub(/\W/) { |s| "_substituted_character_#{s.ord}_" }
|
68
|
+
if @original_method && @original_method.owner == stubbee.__metaclass__
|
69
|
+
stubbee.__metaclass__.send(:define_method, method, @original_method.to_proc)
|
70
|
+
stubbee.__metaclass__.send(@original_visibility, method)
|
74
71
|
end
|
75
|
-
hidden_method = "__stubba__#{method_name}__stubba__"
|
76
|
-
RUBY_VERSION < '1.9' ? hidden_method.to_s : hidden_method.to_sym
|
77
72
|
end
|
78
73
|
|
79
74
|
def matches?(other)
|
data/lib/mocha/configuration.rb
CHANGED
@@ -1,62 +1,71 @@
|
|
1
|
-
module Mocha
|
2
|
-
|
3
|
-
# Configuration settings
|
1
|
+
module Mocha
|
2
|
+
|
3
|
+
# Configuration settings.
|
4
4
|
class Configuration
|
5
|
-
|
6
|
-
DEFAULTS = {
|
7
|
-
|
5
|
+
|
6
|
+
DEFAULTS = {
|
7
|
+
:stubbing_method_unnecessarily => :allow,
|
8
|
+
:stubbing_method_on_non_mock_object => :allow,
|
9
|
+
:stubbing_non_existent_method => :allow,
|
10
|
+
:stubbing_non_public_method => :allow,
|
11
|
+
:stubbing_method_on_nil => :prevent,
|
12
|
+
}
|
13
|
+
|
8
14
|
class << self
|
9
|
-
|
10
|
-
#
|
15
|
+
|
16
|
+
# Allow the specified +action+.
|
11
17
|
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
# If given a block, the configuration for the action will only be changed for the duration of the block, and will then be restored to the previous value.
|
18
|
+
# @param [Symbol] action one of +:stubbing_method_unnecessarily+, +:stubbing_method_on_non_mock_object+, +:stubbing_non_existent_method+, +:stubbing_non_public_method+, +:stubbing_method_on_nil+.
|
19
|
+
# @yield optional block during which the configuration change will be changed before being returned to its original value at the end of the block.
|
15
20
|
def allow(action, &block)
|
16
21
|
change_config action, :allow, &block
|
17
22
|
end
|
18
|
-
|
19
|
-
|
23
|
+
|
24
|
+
# @private
|
25
|
+
def allow?(action)
|
20
26
|
configuration[action] == :allow
|
21
27
|
end
|
22
|
-
|
23
|
-
#
|
28
|
+
|
29
|
+
# Warn if the specified +action+ is attempted.
|
24
30
|
#
|
25
|
-
#
|
26
|
-
#
|
27
|
-
# If given a block, the configuration for the action will only be changed for the duration of the block, and will then be restored to the previous value.
|
31
|
+
# @param [Symbol] action one of +:stubbing_method_unnecessarily+, +:stubbing_method_on_non_mock_object+, +:stubbing_non_existent_method+, +:stubbing_non_public_method+, +:stubbing_method_on_nil+.
|
32
|
+
# @yield optional block during which the configuration change will be changed before being returned to its original value at the end of the block.
|
28
33
|
def warn_when(action, &block)
|
29
34
|
change_config action, :warn, &block
|
30
35
|
end
|
31
|
-
|
32
|
-
|
36
|
+
|
37
|
+
# @private
|
38
|
+
def warn_when?(action)
|
33
39
|
configuration[action] == :warn
|
34
40
|
end
|
35
|
-
|
36
|
-
#
|
41
|
+
|
42
|
+
# Raise a {StubbingError} if if the specified +action+ is attempted.
|
37
43
|
#
|
38
|
-
#
|
39
|
-
#
|
40
|
-
# If given a block, the configuration for the action will only be changed for the duration of the block, and will then be restored to the previous value.
|
44
|
+
# @param [Symbol] action one of +:stubbing_method_unnecessarily+, +:stubbing_method_on_non_mock_object+, +:stubbing_non_existent_method+, +:stubbing_non_public_method+, +:stubbing_method_on_nil+.
|
45
|
+
# @yield optional block during which the configuration change will be changed before being returned to its original value at the end of the block.
|
41
46
|
def prevent(action, &block)
|
42
47
|
change_config action, :prevent, &block
|
43
48
|
end
|
44
|
-
|
45
|
-
|
49
|
+
|
50
|
+
# @private
|
51
|
+
def prevent?(action)
|
46
52
|
configuration[action] == :prevent
|
47
53
|
end
|
48
|
-
|
49
|
-
|
54
|
+
|
55
|
+
# @private
|
56
|
+
def reset_configuration
|
50
57
|
@configuration = nil
|
51
58
|
end
|
52
|
-
|
59
|
+
|
53
60
|
private
|
54
|
-
|
55
|
-
|
61
|
+
|
62
|
+
# @private
|
63
|
+
def configuration
|
56
64
|
@configuration ||= DEFAULTS.dup
|
57
65
|
end
|
58
66
|
|
59
|
-
|
67
|
+
# @private
|
68
|
+
def change_config(action, new_value, &block)
|
60
69
|
if block_given?
|
61
70
|
temporarily_change_config action, new_value, &block
|
62
71
|
else
|
@@ -64,16 +73,17 @@ module Mocha # :nodoc:
|
|
64
73
|
end
|
65
74
|
end
|
66
75
|
|
67
|
-
|
76
|
+
# @private
|
77
|
+
def temporarily_change_config(action, new_value, &block)
|
68
78
|
original_value = configuration[action]
|
69
79
|
configuration[action] = new_value
|
70
80
|
yield
|
71
81
|
ensure
|
72
82
|
configuration[action] = original_value
|
73
83
|
end
|
74
|
-
|
84
|
+
|
75
85
|
end
|
76
|
-
|
86
|
+
|
77
87
|
end
|
78
|
-
|
88
|
+
|
79
89
|
end
|
data/lib/mocha/deprecation.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
module Mocha
|
2
|
-
|
2
|
+
|
3
3
|
class Deprecation
|
4
|
-
|
4
|
+
|
5
5
|
class << self
|
6
|
-
|
6
|
+
|
7
7
|
attr_accessor :mode, :messages
|
8
|
-
|
8
|
+
|
9
9
|
def warning(message)
|
10
10
|
@messages << message
|
11
11
|
$stderr.puts "Mocha deprecation warning: #{message}" unless mode == :disabled
|
@@ -13,10 +13,10 @@ module Mocha
|
|
13
13
|
end
|
14
14
|
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
self.mode = :enabled
|
18
18
|
self.messages = []
|
19
|
-
|
19
|
+
|
20
20
|
end
|
21
|
-
|
22
|
-
end
|
21
|
+
|
22
|
+
end
|
@@ -1,17 +1,17 @@
|
|
1
|
-
module Mocha
|
2
|
-
|
3
|
-
class ExceptionRaiser
|
4
|
-
|
1
|
+
module Mocha
|
2
|
+
|
3
|
+
class ExceptionRaiser
|
4
|
+
|
5
5
|
def initialize(exception, message)
|
6
6
|
@exception, @message = exception, message
|
7
7
|
end
|
8
|
-
|
8
|
+
|
9
9
|
def evaluate
|
10
10
|
raise @exception, @exception.to_s if @exception.is_a?(Module) && @exception.ancestors.include?(Interrupt)
|
11
11
|
raise @exception, @message if @message
|
12
12
|
raise @exception
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
end
|
data/lib/mocha/expectation.rb
CHANGED
@@ -10,16 +10,17 @@ require 'mocha/in_state_ordering_constraint'
|
|
10
10
|
require 'mocha/change_state_side_effect'
|
11
11
|
require 'mocha/cardinality'
|
12
12
|
|
13
|
-
module Mocha
|
13
|
+
module Mocha
|
14
14
|
|
15
|
-
# Methods on expectations returned from Mock#expects, Mock#stubs,
|
15
|
+
# Methods on expectations returned from {Mock#expects}, {Mock#stubs}, {ObjectMethods#expects} and {ObjectMethods#stubs}.
|
16
16
|
class Expectation
|
17
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 }
|
@@ -44,9 +46,11 @@ module Mocha # :nodoc:
|
|
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,8 +61,7 @@ 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
|
@@ -69,10 +72,13 @@ module Mocha # :nodoc:
|
|
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,8 +87,7 @@ 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
|
@@ -92,13 +97,14 @@ module Mocha # :nodoc:
|
|
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
|
@@ -108,9 +114,12 @@ module Mocha # :nodoc:
|
|
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 }
|
@@ -125,9 +134,11 @@ module Mocha # :nodoc:
|
|
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
|
@@ -141,9 +152,12 @@ module Mocha # :nodoc:
|
|
141
152
|
self
|
142
153
|
end
|
143
154
|
|
144
|
-
# :call-seq: at_most(maximum_number_of_times) -> expectation
|
145
|
-
#
|
146
155
|
# Modifies expectation so that the expected method must be called at most a +maximum_number_of_times+.
|
156
|
+
#
|
157
|
+
# @param [Integer] maximum_number_of_times maximum number of expected invocations.
|
158
|
+
# @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
|
159
|
+
#
|
160
|
+
# @example Expected method must be called at most twice.
|
147
161
|
# object = mock()
|
148
162
|
# object.expects(:expected_method).at_most(2)
|
149
163
|
# 2.times { object.expected_method }
|
@@ -151,16 +165,17 @@ module Mocha # :nodoc:
|
|
151
165
|
#
|
152
166
|
# object = mock()
|
153
167
|
# object.expects(:expected_method).at_most(2)
|
154
|
-
# 3.times { object.expected_method }
|
155
|
-
# # => verify fails
|
168
|
+
# 3.times { object.expected_method } # => unexpected invocation
|
156
169
|
def at_most(maximum_number_of_times)
|
157
170
|
@cardinality = Cardinality.at_most(maximum_number_of_times)
|
158
171
|
self
|
159
172
|
end
|
160
173
|
|
161
|
-
# :call-seq: at_most_once() -> expectation
|
162
|
-
#
|
163
174
|
# Modifies expectation so that the expected method must be called at most once.
|
175
|
+
#
|
176
|
+
# @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
|
177
|
+
#
|
178
|
+
# @example Expected method must be called at most once.
|
164
179
|
# object = mock()
|
165
180
|
# object.expects(:expected_method).at_most_once
|
166
181
|
# object.expected_method
|
@@ -168,16 +183,23 @@ module Mocha # :nodoc:
|
|
168
183
|
#
|
169
184
|
# object = mock()
|
170
185
|
# object.expects(:expected_method).at_most_once
|
171
|
-
# 2.times { object.expected_method }
|
172
|
-
# # => verify fails
|
186
|
+
# 2.times { object.expected_method } # => unexpected invocation
|
173
187
|
def at_most_once()
|
174
188
|
at_most(1)
|
175
189
|
self
|
176
190
|
end
|
177
191
|
|
178
|
-
# :call-seq: with(*expected_parameters, &matching_block) -> expectation
|
179
|
-
#
|
180
192
|
# Modifies expectation so that the expected method must be called with +expected_parameters+.
|
193
|
+
#
|
194
|
+
# May be used with parameter matchers in {ParameterMatchers}.
|
195
|
+
#
|
196
|
+
# @param [*Array] expected_parameters parameters expected.
|
197
|
+
# @yield optional block specifying custom matching.
|
198
|
+
# @yieldparam [*Array] actual_parameters parameters with which expected method was invoked.
|
199
|
+
# @yieldreturn [Boolean] +true+ if +actual_parameters+ are acceptable.
|
200
|
+
# @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
|
201
|
+
#
|
202
|
+
# @example Expected method must be called with expected parameters.
|
181
203
|
# object = mock()
|
182
204
|
# object.expects(:expected_method).with(:param1, :param2)
|
183
205
|
# object.expected_method(:param1, :param2)
|
@@ -187,10 +209,8 @@ module Mocha # :nodoc:
|
|
187
209
|
# object.expects(:expected_method).with(:param1, :param2)
|
188
210
|
# object.expected_method(:param3)
|
189
211
|
# # => verify fails
|
190
|
-
# May be used with parameter matchers in Mocha::ParameterMatchers.
|
191
212
|
#
|
192
|
-
#
|
193
|
-
# The expectation is matched if the block evaluates to +true+.
|
213
|
+
# @example Expected method must be called with a value divisible by 4.
|
194
214
|
# object = mock()
|
195
215
|
# object.expects(:expected_method).with() { |value| value % 4 == 0 }
|
196
216
|
# object.expected_method(16)
|
@@ -205,15 +225,22 @@ module Mocha # :nodoc:
|
|
205
225
|
self
|
206
226
|
end
|
207
227
|
|
208
|
-
# :call-seq: yields(*parameters) -> expectation
|
209
|
-
#
|
210
228
|
# Modifies expectation so that when the expected method is called, it yields with the specified +parameters+.
|
229
|
+
#
|
230
|
+
# May be called multiple times on the same expectation for consecutive invocations.
|
231
|
+
#
|
232
|
+
# @param [*Array] parameters parameters to be yielded.
|
233
|
+
# @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
|
234
|
+
# @see #then
|
235
|
+
#
|
236
|
+
# @example Yield parameters when expected method is invoked.
|
211
237
|
# object = mock()
|
212
238
|
# object.expects(:expected_method).yields('result')
|
213
239
|
# yielded_value = nil
|
214
240
|
# object.expected_method { |value| yielded_value = value }
|
215
241
|
# yielded_value # => 'result'
|
216
|
-
#
|
242
|
+
#
|
243
|
+
# @example Yield different parameters on different invocations of the expected method.
|
217
244
|
# object = mock()
|
218
245
|
# object.stubs(:expected_method).yields(1).then.yields(2)
|
219
246
|
# yielded_values_from_first_invocation = []
|
@@ -227,18 +254,20 @@ module Mocha # :nodoc:
|
|
227
254
|
self
|
228
255
|
end
|
229
256
|
|
230
|
-
# :call-seq: multiple_yields(*parameter_groups) -> expectation
|
231
|
-
#
|
232
257
|
# Modifies expectation so that when the expected method is called, it yields multiple times per invocation with the specified +parameter_groups+.
|
233
258
|
#
|
234
|
-
#
|
259
|
+
# @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.
|
260
|
+
# @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
|
261
|
+
# @see #then
|
235
262
|
#
|
263
|
+
# @example When the +expected_method+ is called, the stub will invoke the block twice, the first time it passes +'result_1'+, +'result_2'+ as the parameters, and the second time it passes 'result_3' as the parameters.
|
236
264
|
# object = mock()
|
237
265
|
# object.expects(:expected_method).multiple_yields(['result_1', 'result_2'], ['result_3'])
|
238
266
|
# yielded_values = []
|
239
267
|
# object.expected_method { |*values| yielded_values << values }
|
240
268
|
# yielded_values # => [['result_1', 'result_2'], ['result_3]]
|
241
|
-
#
|
269
|
+
#
|
270
|
+
# @example Yield different groups of parameters on different invocations of the expected method.
|
242
271
|
# object = mock()
|
243
272
|
# object.stubs(:expected_method).multiple_yields([1, 2], [3]).then.multiple_yields([4], [5, 6])
|
244
273
|
# yielded_values_from_first_invocation = []
|
@@ -252,32 +281,43 @@ module Mocha # :nodoc:
|
|
252
281
|
self
|
253
282
|
end
|
254
283
|
|
255
|
-
# :call-seq: returns(value) -> expectation
|
256
|
-
# returns(*values) -> expectation
|
257
|
-
#
|
258
284
|
# Modifies expectation so that when the expected method is called, it returns the specified +value+.
|
285
|
+
#
|
286
|
+
# @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
|
287
|
+
# @see #then
|
288
|
+
#
|
289
|
+
# @overload def returns(value)
|
290
|
+
# @param [Object] value value to return on invocation of expected method.
|
291
|
+
# @overload def returns(*values)
|
292
|
+
# @param [*Array] values values to return on consecutive invocations of expected method.
|
293
|
+
#
|
294
|
+
# @example Return the same value on every invocation.
|
259
295
|
# object = mock()
|
260
296
|
# object.stubs(:stubbed_method).returns('result')
|
261
297
|
# object.stubbed_method # => 'result'
|
262
298
|
# object.stubbed_method # => 'result'
|
263
|
-
#
|
299
|
+
#
|
300
|
+
# @example Return a different value on consecutive invocations.
|
264
301
|
# object = mock()
|
265
302
|
# object.stubs(:stubbed_method).returns(1, 2)
|
266
303
|
# object.stubbed_method # => 1
|
267
304
|
# object.stubbed_method # => 2
|
268
|
-
#
|
305
|
+
#
|
306
|
+
# @example Alternative way to return a different value on consecutive invocations.
|
269
307
|
# object = mock()
|
270
308
|
# object.stubs(:expected_method).returns(1, 2).then.returns(3)
|
271
309
|
# object.expected_method # => 1
|
272
310
|
# object.expected_method # => 2
|
273
311
|
# object.expected_method # => 3
|
274
|
-
#
|
312
|
+
#
|
313
|
+
# @example May be called in conjunction with {#raises} on the same expectation.
|
275
314
|
# object = mock()
|
276
315
|
# object.stubs(:expected_method).returns(1, 2).then.raises(Exception)
|
277
316
|
# object.expected_method # => 1
|
278
317
|
# object.expected_method # => 2
|
279
318
|
# object.expected_method # => raises exception of class Exception1
|
280
|
-
#
|
319
|
+
#
|
320
|
+
# @example Note that in Ruby a method returning multiple values is exactly equivalent to a method returning an +Array+ of those values.
|
281
321
|
# object = mock()
|
282
322
|
# object.stubs(:expected_method).returns([1, 2])
|
283
323
|
# x, y = object.expected_method
|
@@ -288,22 +328,36 @@ module Mocha # :nodoc:
|
|
288
328
|
self
|
289
329
|
end
|
290
330
|
|
291
|
-
#
|
331
|
+
# 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)+.
|
332
|
+
#
|
333
|
+
# @param [Class,Exception,String,#exception] exception exception to be raised or message to be passed to RuntimeError.
|
334
|
+
# @param [String] message exception message.
|
335
|
+
# @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
|
336
|
+
#
|
337
|
+
# @see Kernel#raise
|
338
|
+
# @see #then
|
292
339
|
#
|
293
|
-
#
|
340
|
+
# @overload def raises
|
341
|
+
# @overload def raises(exception)
|
342
|
+
# @overload def raises(exception, message)
|
343
|
+
#
|
344
|
+
# @example Raise specified exception if expected method is invoked.
|
294
345
|
# object = stub()
|
295
346
|
# object.stubs(:expected_method).raises(Exception, 'message')
|
296
347
|
# object.expected_method # => raises exception of class Exception and with message 'message'
|
297
|
-
#
|
348
|
+
#
|
349
|
+
# @example Raise custom exception with extra constructor parameters by passing in an instance of the exception.
|
298
350
|
# object = stub()
|
299
351
|
# object.stubs(:expected_method).raises(MyException.new('message', 1, 2, 3))
|
300
352
|
# object.expected_method # => raises the specified instance of MyException
|
301
|
-
#
|
353
|
+
#
|
354
|
+
# @example Raise different exceptions on consecutive invocations of the expected method.
|
302
355
|
# object = stub()
|
303
356
|
# object.stubs(:expected_method).raises(Exception1).then.raises(Exception2)
|
304
357
|
# object.expected_method # => raises exception of class Exception1
|
305
358
|
# object.expected_method # => raises exception of class Exception2
|
306
|
-
#
|
359
|
+
#
|
360
|
+
# @example Raise an exception on first invocation of expected method and then return values on subsequent invocations.
|
307
361
|
# object = stub()
|
308
362
|
# object.stubs(:expected_method).raises(Exception).then.returns(2, 3)
|
309
363
|
# object.expected_method # => raises exception of class Exception1
|
@@ -314,22 +368,35 @@ module Mocha # :nodoc:
|
|
314
368
|
self
|
315
369
|
end
|
316
370
|
|
317
|
-
#
|
371
|
+
# 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)+.
|
372
|
+
#
|
373
|
+
# @param [Symbol,String] tag tag to throw to transfer control to the active catch block.
|
374
|
+
# @param [Object] object return value for the catch block.
|
375
|
+
# @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
|
318
376
|
#
|
319
|
-
#
|
377
|
+
# @see Kernel#throw
|
378
|
+
# @see #then
|
379
|
+
#
|
380
|
+
# @overload def throw(tag)
|
381
|
+
# @overload def throw(tag, object)
|
382
|
+
#
|
383
|
+
# @example Throw tag when expected method is invoked.
|
320
384
|
# object = stub()
|
321
385
|
# object.stubs(:expected_method).throws(:done)
|
322
386
|
# object.expected_method # => throws tag :done
|
323
|
-
#
|
387
|
+
#
|
388
|
+
# @example Throw tag with return value +object+ c.f. +Kernel#throw+.
|
324
389
|
# object = stub()
|
325
390
|
# object.stubs(:expected_method).throws(:done, 'result')
|
326
391
|
# object.expected_method # => throws tag :done and causes catch block to return 'result'
|
327
|
-
#
|
392
|
+
#
|
393
|
+
# @example Throw different tags on consecutive invocations of the expected method.
|
328
394
|
# object = stub()
|
329
395
|
# object.stubs(:expected_method).throws(:done).then.throws(:continue)
|
330
396
|
# object.expected_method # => throws :done
|
331
397
|
# object.expected_method # => throws :continue
|
332
|
-
#
|
398
|
+
#
|
399
|
+
# @example Throw tag on first invocation of expected method and then return values for subsequent invocations.
|
333
400
|
# object = stub()
|
334
401
|
# object.stubs(:expected_method).throws(:done).then.returns(2, 3)
|
335
402
|
# object.expected_method # => throws :done
|
@@ -340,10 +407,19 @@ module Mocha # :nodoc:
|
|
340
407
|
self
|
341
408
|
end
|
342
409
|
|
343
|
-
#
|
344
|
-
#
|
410
|
+
# @overload def then
|
411
|
+
# Used as syntactic sugar to improve readability. It has no effect on state of the expectation.
|
412
|
+
# @overload def then(state_machine.is(state_name))
|
413
|
+
# Used to change the +state_machine+ to the state specified by +state_name+ when the expected invocation occurs.
|
414
|
+
# @param [StateMachine::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.
|
415
|
+
#
|
416
|
+
# @see API#states
|
417
|
+
# @see StateMachine
|
418
|
+
# @see #when
|
419
|
+
#
|
420
|
+
# @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
|
345
421
|
#
|
346
|
-
#
|
422
|
+
# @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
423
|
# object = mock()
|
348
424
|
# object.stubs(:expected_method).returns(1, 2).then.raises(Exception).then.returns(4)
|
349
425
|
# object.expected_method # => 1
|
@@ -351,9 +427,7 @@ module Mocha # :nodoc:
|
|
351
427
|
# object.expected_method # => raises exception of class Exception
|
352
428
|
# object.expected_method # => 4
|
353
429
|
#
|
354
|
-
#
|
355
|
-
#
|
356
|
-
# See also API#states, StateMachine and Expectation#when.
|
430
|
+
# @example Using {#then} to change the +state+ of a +state_machine+ on the invocation of an expected method.
|
357
431
|
# power = states('power').starts_as('off')
|
358
432
|
#
|
359
433
|
# radio = mock('radio')
|
@@ -371,11 +445,16 @@ module Mocha # :nodoc:
|
|
371
445
|
self
|
372
446
|
end
|
373
447
|
|
374
|
-
#
|
448
|
+
# Constrains the expectation to occur only when the +state_machine+ is in the state specified by +state_name+.
|
449
|
+
#
|
450
|
+
# @param [StateMachine::StatePredicate] state_machine.is(state_name) provides a mechanism to determine whether the +state_machine+ is in the state specified by +state_name+ when the expected method is invoked.
|
451
|
+
# @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
|
375
452
|
#
|
376
|
-
#
|
453
|
+
# @see API#states
|
454
|
+
# @see StateMachine
|
455
|
+
# @see #then
|
377
456
|
#
|
378
|
-
#
|
457
|
+
# @example Using {#when} to only allow invocation of methods when "power" state machine is in the "on" state.
|
379
458
|
# power = states('power').starts_as('off')
|
380
459
|
#
|
381
460
|
# radio = mock('radio')
|
@@ -390,17 +469,22 @@ module Mocha # :nodoc:
|
|
390
469
|
self
|
391
470
|
end
|
392
471
|
|
393
|
-
#
|
472
|
+
# Constrains the expectation so that it must be invoked at the current point in the +sequence+.
|
394
473
|
#
|
395
|
-
#
|
474
|
+
# To expect a sequence of invocations, write the expectations in order and add the +in_sequence(sequence)+ clause to each one.
|
396
475
|
#
|
397
|
-
#
|
476
|
+
# Expectations in a +sequence+ can have any invocation count.
|
398
477
|
#
|
399
|
-
#
|
478
|
+
# If an expectation in a sequence is stubbed, rather than expected, it can be skipped in the +sequence+.
|
400
479
|
#
|
401
|
-
#
|
480
|
+
# An expected method can appear in multiple sequences.
|
402
481
|
#
|
403
|
-
#
|
482
|
+
# @param [*Array<Sequence>] sequences sequences in which expected method should appear.
|
483
|
+
# @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
|
484
|
+
#
|
485
|
+
# @see API#sequence
|
486
|
+
#
|
487
|
+
# @example Ensure methods are invoked in a specified order.
|
404
488
|
# breakfast = sequence('breakfast')
|
405
489
|
#
|
406
490
|
# egg = mock('egg')
|
@@ -412,10 +496,10 @@ module Mocha # :nodoc:
|
|
412
496
|
self
|
413
497
|
end
|
414
498
|
|
415
|
-
#
|
416
|
-
|
499
|
+
# @private
|
417
500
|
attr_reader :backtrace
|
418
501
|
|
502
|
+
# @private
|
419
503
|
def initialize(mock, expected_method_name, backtrace = nil)
|
420
504
|
@mock = mock
|
421
505
|
@method_matcher = MethodMatcher.new(expected_method_name.to_sym)
|
@@ -428,42 +512,52 @@ module Mocha # :nodoc:
|
|
428
512
|
@backtrace = backtrace || caller
|
429
513
|
end
|
430
514
|
|
515
|
+
# @private
|
431
516
|
def add_ordering_constraint(ordering_constraint)
|
432
517
|
@ordering_constraints << ordering_constraint
|
433
518
|
end
|
434
519
|
|
520
|
+
# @private
|
435
521
|
def add_in_sequence_ordering_constraint(sequence)
|
436
522
|
sequence.constrain_as_next_in_sequence(self)
|
437
523
|
end
|
438
524
|
|
525
|
+
# @private
|
439
526
|
def add_side_effect(side_effect)
|
440
527
|
@side_effects << side_effect
|
441
528
|
end
|
442
529
|
|
530
|
+
# @private
|
443
531
|
def perform_side_effects
|
444
532
|
@side_effects.each { |side_effect| side_effect.perform }
|
445
533
|
end
|
446
534
|
|
535
|
+
# @private
|
447
536
|
def in_correct_order?
|
448
537
|
@ordering_constraints.all? { |ordering_constraint| ordering_constraint.allows_invocation_now? }
|
449
538
|
end
|
450
539
|
|
540
|
+
# @private
|
451
541
|
def matches_method?(method_name)
|
452
542
|
@method_matcher.match?(method_name)
|
453
543
|
end
|
454
544
|
|
545
|
+
# @private
|
455
546
|
def match?(actual_method_name, *actual_parameters)
|
456
547
|
@method_matcher.match?(actual_method_name) && @parameters_matcher.match?(actual_parameters) && in_correct_order?
|
457
548
|
end
|
458
549
|
|
550
|
+
# @private
|
459
551
|
def invocations_allowed?
|
460
552
|
@cardinality.invocations_allowed?(@invocation_count)
|
461
553
|
end
|
462
554
|
|
555
|
+
# @private
|
463
556
|
def satisfied?
|
464
557
|
@cardinality.satisfied?(@invocation_count)
|
465
558
|
end
|
466
559
|
|
560
|
+
# @private
|
467
561
|
def invoke
|
468
562
|
@invocation_count += 1
|
469
563
|
perform_side_effects()
|
@@ -475,15 +569,18 @@ module Mocha # :nodoc:
|
|
475
569
|
@return_values.next
|
476
570
|
end
|
477
571
|
|
572
|
+
# @private
|
478
573
|
def verified?(assertion_counter = nil)
|
479
574
|
assertion_counter.increment if assertion_counter && @cardinality.needs_verifying?
|
480
575
|
@cardinality.verified?(@invocation_count)
|
481
576
|
end
|
482
577
|
|
578
|
+
# @private
|
483
579
|
def used?
|
484
580
|
@cardinality.used?(@invocation_count)
|
485
581
|
end
|
486
582
|
|
583
|
+
# @private
|
487
584
|
def mocha_inspect
|
488
585
|
message = "#{@cardinality.mocha_inspect}, "
|
489
586
|
message << case @invocation_count
|
@@ -498,12 +595,11 @@ module Mocha # :nodoc:
|
|
498
595
|
message
|
499
596
|
end
|
500
597
|
|
598
|
+
# @private
|
501
599
|
def method_signature
|
502
600
|
"#{@mock.mocha_inspect}.#{@method_matcher.mocha_inspect}#{@parameters_matcher.mocha_inspect}"
|
503
601
|
end
|
504
602
|
|
505
|
-
# :startdoc:
|
506
|
-
|
507
603
|
end
|
508
604
|
|
509
605
|
end
|