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
@@ -0,0 +1,105 @@
|
|
1
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
2
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
3
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
4
|
+
<head>
|
5
|
+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
6
|
+
<title>
|
7
|
+
Top Level Namespace
|
8
|
+
|
9
|
+
— Mocha 0.11.0
|
10
|
+
|
11
|
+
</title>
|
12
|
+
|
13
|
+
<link rel="stylesheet" href="css/style.css" type="text/css" media="screen" charset="utf-8" />
|
14
|
+
|
15
|
+
<link rel="stylesheet" href="css/common.css" type="text/css" media="screen" charset="utf-8" />
|
16
|
+
|
17
|
+
<script type="text/javascript" charset="utf-8">
|
18
|
+
relpath = '';
|
19
|
+
if (relpath != '') relpath += '/';
|
20
|
+
</script>
|
21
|
+
|
22
|
+
<script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
|
23
|
+
|
24
|
+
<script type="text/javascript" charset="utf-8" src="js/app.js"></script>
|
25
|
+
|
26
|
+
|
27
|
+
</head>
|
28
|
+
<body>
|
29
|
+
<script type="text/javascript" charset="utf-8">
|
30
|
+
if (window.top.frames.main) document.body.className = 'frames';
|
31
|
+
</script>
|
32
|
+
|
33
|
+
<div id="header">
|
34
|
+
<div id="menu">
|
35
|
+
|
36
|
+
<a href="_index.html">Index</a> »
|
37
|
+
|
38
|
+
|
39
|
+
<span class="title">Top Level Namespace</span>
|
40
|
+
|
41
|
+
|
42
|
+
<div class="noframes"><span class="title">(</span><a href="." target="_top">no frames</a><span class="title">)</span></div>
|
43
|
+
</div>
|
44
|
+
|
45
|
+
<div id="search">
|
46
|
+
|
47
|
+
<a id="class_list_link" href="#">Class List</a>
|
48
|
+
|
49
|
+
<a id="method_list_link" href="#">Method List</a>
|
50
|
+
|
51
|
+
<a id="file_list_link" href="#">File List</a>
|
52
|
+
|
53
|
+
</div>
|
54
|
+
<div class="clear"></div>
|
55
|
+
</div>
|
56
|
+
|
57
|
+
<iframe id="search_frame"></iframe>
|
58
|
+
|
59
|
+
<div id="content"><h1>Top Level Namespace
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
</h1>
|
64
|
+
|
65
|
+
<dl class="box">
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
|
74
|
+
</dl>
|
75
|
+
<div class="clear"></div>
|
76
|
+
|
77
|
+
<h2>Defined Under Namespace</h2>
|
78
|
+
<p class="children">
|
79
|
+
|
80
|
+
|
81
|
+
<strong class="modules">Modules:</strong> <span class='object_link'><a href="Mocha.html" title="Mocha (module)">Mocha</a></span>
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
|
86
|
+
</p>
|
87
|
+
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
|
96
|
+
</div>
|
97
|
+
|
98
|
+
<div id="footer">
|
99
|
+
Generated on Thu Apr 19 10:54:34 2012 by
|
100
|
+
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
101
|
+
0.7.5 (ruby-1.9.3).
|
102
|
+
</div>
|
103
|
+
|
104
|
+
</body>
|
105
|
+
</html>
|
@@ -3,7 +3,7 @@ require 'mocha/class_method'
|
|
3
3
|
module Mocha
|
4
4
|
|
5
5
|
class AnyInstanceMethod < ClassMethod
|
6
|
-
|
6
|
+
|
7
7
|
def mock
|
8
8
|
stubbee.any_instance.mocha
|
9
9
|
end
|
@@ -15,7 +15,16 @@ module Mocha
|
|
15
15
|
def hide_original_method
|
16
16
|
if method_exists?(method)
|
17
17
|
begin
|
18
|
-
stubbee.
|
18
|
+
@original_method = stubbee.instance_method(method)
|
19
|
+
if @original_method && @original_method.owner == stubbee
|
20
|
+
@original_visibility = :public
|
21
|
+
if stubbee.protected_instance_methods.include?(method)
|
22
|
+
@original_visibility = :protected
|
23
|
+
elsif stubbee.private_instance_methods.include?(method)
|
24
|
+
@original_visibility = :private
|
25
|
+
end
|
26
|
+
stubbee.send(:remove_method, method)
|
27
|
+
end
|
19
28
|
rescue NameError
|
20
29
|
# deal with nasties like ActiveRecord::Associations::AssociationProxy
|
21
30
|
end
|
@@ -35,13 +44,9 @@ module Mocha
|
|
35
44
|
end
|
36
45
|
|
37
46
|
def restore_original_method
|
38
|
-
if
|
39
|
-
|
40
|
-
|
41
|
-
stubbee.send(:remove_method, hidden_method)
|
42
|
-
rescue NameError
|
43
|
-
# deal with nasties like ActiveRecord::Associations::AssociationProxy
|
44
|
-
end
|
47
|
+
if @original_method && @original_method.owner == stubbee
|
48
|
+
stubbee.send(:define_method, method, @original_method)
|
49
|
+
stubbee.send(@original_visibility, method)
|
45
50
|
end
|
46
51
|
end
|
47
52
|
|
@@ -51,7 +56,7 @@ module Mocha
|
|
51
56
|
return true if stubbee.private_instance_methods(false).include?(method)
|
52
57
|
return false
|
53
58
|
end
|
54
|
-
|
59
|
+
|
55
60
|
end
|
56
|
-
|
61
|
+
|
57
62
|
end
|
data/lib/mocha/api.rb
CHANGED
@@ -2,41 +2,41 @@ require 'mocha/parameter_matchers'
|
|
2
2
|
require 'mocha/mockery'
|
3
3
|
require 'mocha/sequence'
|
4
4
|
|
5
|
-
module Mocha
|
6
|
-
|
7
|
-
# Methods added to Test::Unit::TestCase or equivalent.
|
5
|
+
module Mocha
|
6
|
+
|
7
|
+
# Methods added to +Test::Unit::TestCase+ or equivalent.
|
8
8
|
module API
|
9
|
-
|
9
|
+
|
10
10
|
include ParameterMatchers
|
11
|
-
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
22
|
-
#
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
26
|
-
#
|
27
|
-
#
|
28
|
-
#
|
29
|
-
#
|
30
|
-
#
|
31
|
-
# def
|
32
|
-
#
|
33
|
-
# expects(:
|
34
|
-
#
|
11
|
+
|
12
|
+
# Builds a new mock object
|
13
|
+
#
|
14
|
+
# @param [String] name identifies mock object in error messages.
|
15
|
+
# @param [Hash] expected_methods_vs_return_values expected method name symbols as keys and corresponding return values as values - these expectations are setup as if {Mock#expects} were called multiple times.
|
16
|
+
# @yield optional block to be evaluated against the mock object instance, giving an alternative way to setup expectations.
|
17
|
+
# @return [Mock] a new mock object
|
18
|
+
#
|
19
|
+
# @overload def mock(name, &block)
|
20
|
+
# @overload def mock(expected_methods_vs_return_values = {}, &block)
|
21
|
+
# @overload def mock(name, expected_methods_vs_return_values = {}, &block)
|
22
|
+
#
|
23
|
+
# @example Using expected_methods_vs_return_values Hash to setup expectations.
|
24
|
+
# def test_motor_starts_and_stops
|
25
|
+
# motor = mock('motor', :start => true, :stop => true)
|
26
|
+
# assert motor.start
|
27
|
+
# assert motor.stop
|
28
|
+
# # an error will be raised unless both Motor#start and Motor#stop have been called
|
29
|
+
# end
|
30
|
+
# @example Using the optional block to setup expectations & stubbed methods.
|
31
|
+
# def test_motor_starts_and_stops
|
32
|
+
# motor = mock('motor') do
|
33
|
+
# expects(:start).with(100.rpm).returns(true)
|
34
|
+
# stubs(:stop).returns(true)
|
35
35
|
# end
|
36
|
-
#
|
37
|
-
#
|
38
|
-
# # an error will be raised
|
39
|
-
# end
|
36
|
+
# assert motor.start(100.rpm)
|
37
|
+
# assert motor.stop
|
38
|
+
# # an error will only be raised if Motor#start(100.rpm) has not been called
|
39
|
+
# end
|
40
40
|
def mock(*arguments, &block)
|
41
41
|
name = arguments.shift if arguments.first.is_a?(String)
|
42
42
|
expectations = arguments.shift || {}
|
@@ -44,33 +44,35 @@ module Mocha # :nodoc:
|
|
44
44
|
mock.expects(expectations)
|
45
45
|
mock
|
46
46
|
end
|
47
|
-
|
48
|
-
#
|
49
|
-
#
|
50
|
-
#
|
51
|
-
#
|
52
|
-
#
|
53
|
-
#
|
54
|
-
#
|
55
|
-
#
|
56
|
-
#
|
57
|
-
#
|
58
|
-
#
|
59
|
-
#
|
60
|
-
#
|
61
|
-
#
|
62
|
-
#
|
47
|
+
|
48
|
+
# Builds a new mock object
|
49
|
+
#
|
50
|
+
# @param [String] name identifies mock object in error messages.
|
51
|
+
# @param [Hash] stubbed_methods_vs_return_values stubbed method name symbols as keys and corresponding return values as values - these stubbed methods are setup as if {Mock#stubs} were called multiple times.
|
52
|
+
# @yield optional block to be evaluated against the mock object instance, giving an alternative way to setup stubbed methods.
|
53
|
+
# @return [Mock] a new mock object
|
54
|
+
#
|
55
|
+
# @overload def stub(name, &block)
|
56
|
+
# @overload def stub(stubbed_methods_vs_return_values = {}, &block)
|
57
|
+
# @overload def stub(name, stubbed_methods_vs_return_values = {}, &block)
|
58
|
+
#
|
59
|
+
# @example Using stubbed_methods_vs_return_values Hash to setup stubbed methods.
|
60
|
+
# def test_motor_starts_and_stops
|
61
|
+
# motor = mock('motor', :start => true, :stop => true)
|
62
|
+
# assert motor.start
|
63
|
+
# assert motor.stop
|
64
|
+
# # an error will not be raised even if either Motor#start or Motor#stop has not been called
|
63
65
|
# end
|
64
66
|
#
|
65
|
-
#
|
66
|
-
# def
|
67
|
-
#
|
68
|
-
#
|
69
|
-
# stubs(:
|
67
|
+
# @example Using the optional block to setup expectations & stubbed methods.
|
68
|
+
# def test_motor_starts_and_stops
|
69
|
+
# motor = mock('motor') do
|
70
|
+
# expects(:start).with(100.rpm).returns(true)
|
71
|
+
# stubs(:stop).returns(true)
|
70
72
|
# end
|
71
|
-
#
|
72
|
-
#
|
73
|
-
# # an error will
|
73
|
+
# assert motor.start(100.rpm)
|
74
|
+
# assert motor.stop
|
75
|
+
# # an error will only be raised if Motor#start(100.rpm) has not been called
|
74
76
|
# end
|
75
77
|
def stub(*arguments, &block)
|
76
78
|
name = arguments.shift if arguments.first.is_a?(String)
|
@@ -79,23 +81,24 @@ module Mocha # :nodoc:
|
|
79
81
|
stub.stubs(expectations)
|
80
82
|
stub
|
81
83
|
end
|
82
|
-
|
83
|
-
#
|
84
|
-
#
|
85
|
-
#
|
86
|
-
#
|
87
|
-
#
|
88
|
-
#
|
89
|
-
#
|
90
|
-
#
|
91
|
-
#
|
92
|
-
#
|
93
|
-
#
|
94
|
-
#
|
95
|
-
#
|
96
|
-
#
|
97
|
-
# assert_nil
|
98
|
-
#
|
84
|
+
|
85
|
+
# Builds a mock object that accepts calls to any method. By default it will return +nil+ for any method call.
|
86
|
+
#
|
87
|
+
# @param [String] name identifies mock object in error messages.
|
88
|
+
# @param [Hash] stubbed_methods_vs_return_values stubbed method name symbols as keys and corresponding return values as values - these stubbed methods are setup as if {Mock#stubs} were called multiple times.
|
89
|
+
# @yield optional block to be evaluated against the mock object instance, giving an alternative way to setup stubbed methods.
|
90
|
+
# @return [Mock] a new mock object
|
91
|
+
#
|
92
|
+
# @overload def stub_everything(name, &block)
|
93
|
+
# @overload def stub_everything(stubbed_methods_vs_return_values = {}, &block)
|
94
|
+
# @overload def stub_everything(name, stubbed_methods_vs_return_values = {}, &block)
|
95
|
+
#
|
96
|
+
# @example Ignore invocations of irrelevant methods.
|
97
|
+
# def test_motor_stops
|
98
|
+
# motor = stub_everything('motor', :stop => true)
|
99
|
+
# assert_nil motor.irrelevant_method_1 # => no error raised
|
100
|
+
# assert_nil motor.irrelevant_method_2 # => no error raised
|
101
|
+
# assert motor.stop
|
99
102
|
# end
|
100
103
|
def stub_everything(*arguments, &block)
|
101
104
|
name = arguments.shift if arguments.first.is_a?(String)
|
@@ -105,69 +108,80 @@ module Mocha # :nodoc:
|
|
105
108
|
stub.stubs(expectations)
|
106
109
|
stub
|
107
110
|
end
|
108
|
-
|
109
|
-
#
|
111
|
+
|
112
|
+
# Builds a new sequence which can be used to constrain the order in which expectations can occur.
|
113
|
+
#
|
114
|
+
# Specify that an expected invocation must occur within a named {Sequence} by using {Expectation#in_sequence}.
|
110
115
|
#
|
111
|
-
#
|
116
|
+
# @return [Sequence] a new sequence
|
112
117
|
#
|
113
|
-
#
|
118
|
+
# @see Expectation#in_sequence
|
114
119
|
#
|
115
|
-
#
|
120
|
+
# @example Ensure methods on egg are invoked in correct order.
|
116
121
|
# breakfast = sequence('breakfast')
|
117
122
|
#
|
118
|
-
# egg = mock('egg')
|
119
|
-
#
|
120
|
-
#
|
121
|
-
#
|
123
|
+
# egg = mock('egg') do
|
124
|
+
# expects(:crack).in_sequence(breakfast)
|
125
|
+
# expects(:fry).in_sequence(breakfast)
|
126
|
+
# expects(:eat).in_sequence(breakfast)
|
127
|
+
# end
|
122
128
|
def sequence(name)
|
123
129
|
Sequence.new(name)
|
124
130
|
end
|
125
|
-
|
126
|
-
#
|
131
|
+
|
132
|
+
# Builds a new state machine which can be used to constrain the order in which expectations can occur.
|
127
133
|
#
|
128
|
-
#
|
134
|
+
# Specify the initial state of the state machine by using {StateMachine#starts_as}.
|
129
135
|
#
|
130
|
-
# Specify the
|
136
|
+
# Specify that an expected invocation should change the state of the state machine by using {Expectation#then}.
|
131
137
|
#
|
132
|
-
# Specify that an expected invocation should
|
138
|
+
# Specify that an expected invocation should be constrained to occur within a particular +state+ by using {Expectation#when}.
|
133
139
|
#
|
134
|
-
#
|
140
|
+
# A test can contain multiple state machines.
|
135
141
|
#
|
136
|
-
#
|
142
|
+
# @return [StateMachine] a new state machine
|
137
143
|
#
|
138
|
-
#
|
144
|
+
# @see Expectation#then
|
145
|
+
# @see Expectation#when
|
146
|
+
# @see StateMachine
|
147
|
+
# @example Constrain expected invocations to occur in particular states.
|
139
148
|
# power = states('power').starts_as('off')
|
140
149
|
#
|
141
|
-
# radio = mock('radio')
|
142
|
-
#
|
143
|
-
#
|
144
|
-
#
|
145
|
-
#
|
146
|
-
#
|
147
|
-
#
|
150
|
+
# radio = mock('radio') do
|
151
|
+
# expects(:switch_on).then(power.is('on'))
|
152
|
+
# expects(:select_channel).with('BBC Radio 4').when(power.is('on'))
|
153
|
+
# expects(:adjust_volume).with(+5).when(power.is('on'))
|
154
|
+
# expects(:select_channel).with('BBC World Service').when(power.is('on'))
|
155
|
+
# expects(:adjust_volume).with(-5).when(power.is('on'))
|
156
|
+
# expects(:switch_off).then(power.is('off'))
|
157
|
+
# end
|
148
158
|
def states(name)
|
149
159
|
Mockery.instance.new_state_machine(name)
|
150
160
|
end
|
151
|
-
|
152
|
-
|
161
|
+
|
162
|
+
# @private
|
163
|
+
def mocha_setup
|
153
164
|
end
|
154
|
-
|
155
|
-
|
165
|
+
|
166
|
+
# @private
|
167
|
+
def mocha_verify(assertion_counter = nil)
|
156
168
|
Mockery.instance.verify(assertion_counter)
|
157
169
|
end
|
158
|
-
|
159
|
-
|
170
|
+
|
171
|
+
# @private
|
172
|
+
def mocha_teardown
|
160
173
|
Mockery.instance.teardown
|
161
174
|
Mockery.reset_instance
|
162
175
|
end
|
163
|
-
|
176
|
+
|
164
177
|
end
|
165
|
-
|
178
|
+
|
179
|
+
# @private
|
166
180
|
def self.const_missing(name)
|
167
181
|
return super unless name == :Standalone
|
168
182
|
require 'mocha/deprecation'
|
169
183
|
Deprecation.warning "Mocha::Standalone has been renamed to Mocha::API"
|
170
184
|
return API
|
171
185
|
end
|
172
|
-
|
186
|
+
|
173
187
|
end
|
@@ -1,11 +1,11 @@
|
|
1
1
|
module Mocha
|
2
|
-
|
2
|
+
|
3
3
|
class ArgumentIterator
|
4
|
-
|
4
|
+
|
5
5
|
def initialize(argument)
|
6
6
|
@argument = argument
|
7
7
|
end
|
8
|
-
|
8
|
+
|
9
9
|
def each(&block)
|
10
10
|
if @argument.is_a?(Hash) then
|
11
11
|
@argument.each do |method_name, return_value|
|
@@ -15,7 +15,7 @@ module Mocha
|
|
15
15
|
block.call(@argument)
|
16
16
|
end
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
19
|
end
|
20
|
-
|
21
|
-
end
|
20
|
+
|
21
|
+
end
|