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/mockery.rb
CHANGED
@@ -7,41 +7,41 @@ require 'mocha/configuration'
|
|
7
7
|
require 'mocha/stubbing_error'
|
8
8
|
|
9
9
|
module Mocha
|
10
|
-
|
10
|
+
|
11
11
|
class Mockery
|
12
|
-
|
12
|
+
|
13
13
|
class << self
|
14
|
-
|
14
|
+
|
15
15
|
def instance
|
16
16
|
@instance ||= new
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
19
|
def reset_instance
|
20
20
|
@instance = nil
|
21
21
|
end
|
22
|
-
|
22
|
+
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
def named_mock(name, &block)
|
26
26
|
add_mock(Mock.new(self, Name.new(name), &block))
|
27
27
|
end
|
28
|
-
|
28
|
+
|
29
29
|
def unnamed_mock(&block)
|
30
30
|
add_mock(Mock.new(self, &block))
|
31
31
|
end
|
32
|
-
|
32
|
+
|
33
33
|
def mock_impersonating(object, &block)
|
34
34
|
add_mock(Mock.new(self, ImpersonatingName.new(object), &block))
|
35
35
|
end
|
36
|
-
|
36
|
+
|
37
37
|
def mock_impersonating_any_instance_of(klass, &block)
|
38
38
|
add_mock(Mock.new(self, ImpersonatingAnyInstanceName.new(klass), &block))
|
39
39
|
end
|
40
|
-
|
40
|
+
|
41
41
|
def new_state_machine(name)
|
42
42
|
add_state_machine(StateMachine.new(name))
|
43
43
|
end
|
44
|
-
|
44
|
+
|
45
45
|
def verify(assertion_counter = nil)
|
46
46
|
unless mocks.all? { |mock| mock.__verified__?(assertion_counter) }
|
47
47
|
message = "not all expectations were satisfied\n#{mocha_inspect}"
|
@@ -60,24 +60,24 @@ module Mocha
|
|
60
60
|
end
|
61
61
|
end
|
62
62
|
end
|
63
|
-
|
63
|
+
|
64
64
|
def teardown
|
65
65
|
stubba.unstub_all
|
66
66
|
reset
|
67
67
|
end
|
68
|
-
|
68
|
+
|
69
69
|
def stubba
|
70
70
|
@stubba ||= Central.new
|
71
71
|
end
|
72
|
-
|
72
|
+
|
73
73
|
def mocks
|
74
74
|
@mocks ||= []
|
75
75
|
end
|
76
|
-
|
76
|
+
|
77
77
|
def state_machines
|
78
78
|
@state_machines ||= []
|
79
79
|
end
|
80
|
-
|
80
|
+
|
81
81
|
def mocha_inspect
|
82
82
|
message = ""
|
83
83
|
message << "unsatisfied expectations:\n- #{unsatisfied_expectations.map { |e| e.mocha_inspect }.join("\n- ")}\n" unless unsatisfied_expectations.empty?
|
@@ -85,7 +85,7 @@ module Mocha
|
|
85
85
|
message << "states:\n- #{state_machines.map { |sm| sm.mocha_inspect }.join("\n- ")}" unless state_machines.empty?
|
86
86
|
message
|
87
87
|
end
|
88
|
-
|
88
|
+
|
89
89
|
def on_stubbing(object, method)
|
90
90
|
method = RUBY_VERSION < '1.9' ? method.to_s : method.to_sym
|
91
91
|
unless Mocha::Configuration.allow?(:stubbing_non_existent_method)
|
@@ -98,11 +98,16 @@ module Mocha
|
|
98
98
|
on_stubbing_non_public_method(object, method)
|
99
99
|
end
|
100
100
|
end
|
101
|
+
unless Mocha::Configuration.allow?(:stubbing_method_on_nil)
|
102
|
+
if object.nil?
|
103
|
+
on_stubbing_method_on_nil(object, method)
|
104
|
+
end
|
105
|
+
end
|
101
106
|
unless Mocha::Configuration.allow?(:stubbing_method_on_non_mock_object)
|
102
107
|
on_stubbing_method_on_non_mock_object(object, method)
|
103
108
|
end
|
104
109
|
end
|
105
|
-
|
110
|
+
|
106
111
|
def on_stubbing_non_existent_method(object, method)
|
107
112
|
if Mocha::Configuration.prevent?(:stubbing_non_existent_method)
|
108
113
|
raise StubbingError.new("stubbing non-existent method: #{object.mocha_inspect}.#{method}", caller)
|
@@ -111,7 +116,7 @@ module Mocha
|
|
111
116
|
logger.warn "stubbing non-existent method: #{object.mocha_inspect}.#{method}"
|
112
117
|
end
|
113
118
|
end
|
114
|
-
|
119
|
+
|
115
120
|
def on_stubbing_non_public_method(object, method)
|
116
121
|
if Mocha::Configuration.prevent?(:stubbing_non_public_method)
|
117
122
|
raise StubbingError.new("stubbing non-public method: #{object.mocha_inspect}.#{method}", caller)
|
@@ -120,7 +125,16 @@ module Mocha
|
|
120
125
|
logger.warn "stubbing non-public method: #{object.mocha_inspect}.#{method}"
|
121
126
|
end
|
122
127
|
end
|
123
|
-
|
128
|
+
|
129
|
+
def on_stubbing_method_on_nil(object, method)
|
130
|
+
if Mocha::Configuration.prevent?(:stubbing_method_on_nil)
|
131
|
+
raise StubbingError.new("stubbing method on nil: #{object.mocha_inspect}.#{method}", caller)
|
132
|
+
end
|
133
|
+
if Mocha::Configuration.warn_when?(:stubbing_method_on_nil)
|
134
|
+
logger.warn "stubbing method on nil: #{object.mocha_inspect}.#{method}"
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
124
138
|
def on_stubbing_method_on_non_mock_object(object, method)
|
125
139
|
if Mocha::Configuration.prevent?(:stubbing_method_on_non_mock_object)
|
126
140
|
raise StubbingError.new("stubbing method on non-mock object: #{object.mocha_inspect}.#{method}", caller)
|
@@ -129,7 +143,7 @@ module Mocha
|
|
129
143
|
logger.warn "stubbing method on non-mock object: #{object.mocha_inspect}.#{method}"
|
130
144
|
end
|
131
145
|
end
|
132
|
-
|
146
|
+
|
133
147
|
def on_stubbing_method_unnecessarily(expectation)
|
134
148
|
if Mocha::Configuration.prevent?(:stubbing_method_unnecessarily)
|
135
149
|
raise StubbingError.new("stubbing method unnecessarily: #{expectation.method_signature}", expectation.backtrace)
|
@@ -138,44 +152,44 @@ module Mocha
|
|
138
152
|
logger.warn "stubbing method unnecessarily: #{expectation.method_signature}"
|
139
153
|
end
|
140
154
|
end
|
141
|
-
|
155
|
+
|
142
156
|
attr_writer :logger
|
143
|
-
|
157
|
+
|
144
158
|
def logger
|
145
159
|
@logger ||= Logger.new($stderr)
|
146
160
|
end
|
147
|
-
|
148
|
-
|
161
|
+
|
162
|
+
|
149
163
|
private
|
150
|
-
|
164
|
+
|
151
165
|
def expectations
|
152
166
|
mocks.map { |mock| mock.__expectations__.to_a }.flatten
|
153
167
|
end
|
154
|
-
|
168
|
+
|
155
169
|
def unsatisfied_expectations
|
156
170
|
expectations.reject { |e| e.verified? }
|
157
171
|
end
|
158
|
-
|
172
|
+
|
159
173
|
def satisfied_expectations
|
160
174
|
expectations.select { |e| e.verified? }
|
161
175
|
end
|
162
|
-
|
176
|
+
|
163
177
|
def add_mock(mock)
|
164
178
|
mocks << mock
|
165
179
|
mock
|
166
180
|
end
|
167
|
-
|
181
|
+
|
168
182
|
def add_state_machine(state_machine)
|
169
183
|
state_machines << state_machine
|
170
184
|
state_machine
|
171
185
|
end
|
172
|
-
|
186
|
+
|
173
187
|
def reset
|
174
188
|
@stubba = nil
|
175
189
|
@mocks = nil
|
176
190
|
@state_machines = nil
|
177
191
|
end
|
178
|
-
|
192
|
+
|
179
193
|
end
|
180
|
-
|
194
|
+
|
181
195
|
end
|
data/lib/mocha/module_method.rb
CHANGED
@@ -1,20 +1,20 @@
|
|
1
|
-
module Mocha
|
2
|
-
|
3
|
-
class MultipleYields
|
4
|
-
|
1
|
+
module Mocha
|
2
|
+
|
3
|
+
class MultipleYields
|
4
|
+
|
5
5
|
attr_reader :parameter_groups
|
6
|
-
|
6
|
+
|
7
7
|
def initialize(*parameter_groups)
|
8
8
|
@parameter_groups = parameter_groups
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
def each
|
12
12
|
@parameter_groups.each do |parameter_group|
|
13
13
|
yield(parameter_group)
|
14
14
|
end
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
19
|
end
|
20
|
-
|
20
|
+
|
data/lib/mocha/names.rb
CHANGED
@@ -1,53 +1,53 @@
|
|
1
1
|
module Mocha
|
2
|
-
|
2
|
+
|
3
3
|
class ImpersonatingName
|
4
|
-
|
4
|
+
|
5
5
|
def initialize(object)
|
6
6
|
@object = object
|
7
7
|
end
|
8
|
-
|
8
|
+
|
9
9
|
def mocha_inspect
|
10
10
|
@object.mocha_inspect
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
class ImpersonatingAnyInstanceName
|
16
|
-
|
16
|
+
|
17
17
|
def initialize(klass)
|
18
18
|
@klass = klass
|
19
19
|
end
|
20
|
-
|
20
|
+
|
21
21
|
def mocha_inspect
|
22
22
|
"#<AnyInstance:#{@klass.mocha_inspect}>"
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
end
|
26
|
-
|
26
|
+
|
27
27
|
class Name
|
28
|
-
|
28
|
+
|
29
29
|
def initialize(name)
|
30
30
|
@name = name
|
31
31
|
end
|
32
|
-
|
32
|
+
|
33
33
|
def mocha_inspect
|
34
34
|
"#<Mock:#{@name}>"
|
35
35
|
end
|
36
|
-
|
36
|
+
|
37
37
|
end
|
38
|
-
|
38
|
+
|
39
39
|
class DefaultName
|
40
|
-
|
40
|
+
|
41
41
|
def initialize(mock)
|
42
42
|
@mock = mock
|
43
43
|
end
|
44
|
-
|
44
|
+
|
45
45
|
def mocha_inspect
|
46
46
|
address = @mock.__id__ * 2
|
47
47
|
address += 0x100000000 if address < 0
|
48
48
|
"#<Mock:0x#{'%x' % address}>"
|
49
49
|
end
|
50
|
-
|
50
|
+
|
51
51
|
end
|
52
|
-
|
53
|
-
end
|
52
|
+
|
53
|
+
end
|
data/lib/mocha/no_yields.rb
CHANGED
data/lib/mocha/object.rb
CHANGED
@@ -6,42 +6,50 @@ require 'mocha/any_instance_method'
|
|
6
6
|
require 'mocha/argument_iterator'
|
7
7
|
|
8
8
|
module Mocha
|
9
|
-
|
10
|
-
# Methods added to all objects to allow mocking and stubbing on real objects.
|
9
|
+
|
10
|
+
# Methods added to all objects to allow mocking and stubbing on real (i.e. non-mock) objects.
|
11
11
|
#
|
12
|
-
#
|
12
|
+
# Both {#expects} and {#stubs} return an {Expectation} which can be further modified by methods on {Expectation}.
|
13
13
|
module ObjectMethods
|
14
|
-
|
15
|
-
|
14
|
+
|
15
|
+
# @private
|
16
|
+
def mocha
|
16
17
|
@mocha ||= Mocha::Mockery.instance.mock_impersonating(self)
|
17
18
|
end
|
18
|
-
|
19
|
-
|
19
|
+
|
20
|
+
# @private
|
21
|
+
def reset_mocha
|
20
22
|
@mocha = nil
|
21
23
|
end
|
22
|
-
|
23
|
-
|
24
|
+
|
25
|
+
# @private
|
26
|
+
def stubba_method
|
24
27
|
Mocha::InstanceMethod
|
25
28
|
end
|
26
|
-
|
27
|
-
|
29
|
+
|
30
|
+
# @private
|
31
|
+
def stubba_object
|
28
32
|
self
|
29
33
|
end
|
30
|
-
|
31
|
-
#
|
32
|
-
#
|
34
|
+
|
35
|
+
# Adds an expectation that the specified method must be called exactly once with any parameters.
|
36
|
+
#
|
37
|
+
# The original implementation of the method is replaced during the test and then restored at the end of the test.
|
38
|
+
#
|
39
|
+
# @param [Symbol,String] method_name name of expected method
|
40
|
+
# @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 {#expects} were called multiple times.
|
33
41
|
#
|
34
|
-
#
|
35
|
-
#
|
42
|
+
# @overload def expects(method_name)
|
43
|
+
# @overload def expects(expected_methods_vs_return_values)
|
44
|
+
# @return [Expectation] last-built expectation which can be further modified by methods on {Expectation}.
|
45
|
+
# @raise [StubbingError] if attempting to stub method which is not allowed.
|
46
|
+
#
|
47
|
+
# @example Setting up an expectation on a non-mock object.
|
36
48
|
# product = Product.new
|
37
49
|
# product.expects(:save).returns(true)
|
38
50
|
# assert_equal true, product.save
|
39
51
|
#
|
40
|
-
#
|
41
|
-
#
|
42
|
-
# The original implementation of <tt>Product#save</tt> is restored at the end of the test.
|
43
|
-
#
|
44
|
-
# If +method_names_vs_return_values+ is a +Hash+, an expectation will be set up for each entry using the key as +method_name+ and value as +return_value+.
|
52
|
+
# @example Setting up multiple expectations on a non-mock object.
|
45
53
|
# product = Product.new
|
46
54
|
# product.expects(:valid? => true, :save => true)
|
47
55
|
#
|
@@ -50,13 +58,18 @@ module Mocha
|
|
50
58
|
# product = Product.new
|
51
59
|
# product.expects(:valid?).returns(true)
|
52
60
|
# product.expects(:save).returns(true)
|
53
|
-
|
54
|
-
|
61
|
+
#
|
62
|
+
# @see Mock#expects
|
63
|
+
def expects(expected_methods_vs_return_values)
|
64
|
+
if expected_methods_vs_return_values.to_s =~ /the[^a-z]*spanish[^a-z]*inquisition/i
|
55
65
|
raise Mocha::ExpectationError.new('NOBODY EXPECTS THE SPANISH INQUISITION!')
|
56
66
|
end
|
67
|
+
if frozen?
|
68
|
+
raise StubbingError.new("can't stub method on frozen object: #{mocha_inspect}", caller)
|
69
|
+
end
|
57
70
|
expectation = nil
|
58
71
|
mockery = Mocha::Mockery.instance
|
59
|
-
iterator = ArgumentIterator.new(
|
72
|
+
iterator = ArgumentIterator.new(expected_methods_vs_return_values)
|
60
73
|
iterator.each { |*args|
|
61
74
|
method_name = args.shift
|
62
75
|
mockery.on_stubbing(self, method_name)
|
@@ -67,21 +80,23 @@ module Mocha
|
|
67
80
|
}
|
68
81
|
expectation
|
69
82
|
end
|
70
|
-
|
71
|
-
#
|
72
|
-
# stubs(method_names_vs_return_values) -> last expectation
|
83
|
+
|
84
|
+
# Adds an expectation that the specified method may be called any number of times with any parameters.
|
73
85
|
#
|
74
|
-
#
|
75
|
-
#
|
86
|
+
# @param [Symbol,String] method_name name of stubbed method
|
87
|
+
# @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 {#stubs} were called multiple times.
|
88
|
+
#
|
89
|
+
# @overload def stubs(method_name)
|
90
|
+
# @overload def stubs(stubbed_methods_vs_return_values)
|
91
|
+
# @return [Expectation] last-built expectation which can be further modified by methods on {Expectation}.
|
92
|
+
# @raise [StubbingError] if attempting to stub method which is not allowed.
|
93
|
+
#
|
94
|
+
# @example Setting up a stubbed methods on a non-mock object.
|
76
95
|
# product = Product.new
|
77
96
|
# product.stubs(:save).returns(true)
|
78
97
|
# assert_equal true, product.save
|
79
98
|
#
|
80
|
-
#
|
81
|
-
#
|
82
|
-
# The original implementation of <tt>Product#save</tt> is restored at the end of the test.
|
83
|
-
#
|
84
|
-
# If +method_names_vs_return_values+ is a +Hash+, an expectation will be set up for each entry using the key as +method_name+ and value as +return_value+.
|
99
|
+
# @example Setting up multiple stubbed methods on a non-mock object.
|
85
100
|
# product = Product.new
|
86
101
|
# product.stubs(:valid? => true, :save => true)
|
87
102
|
#
|
@@ -90,10 +105,15 @@ module Mocha
|
|
90
105
|
# product = Product.new
|
91
106
|
# product.stubs(:valid?).returns(true)
|
92
107
|
# product.stubs(:save).returns(true)
|
93
|
-
|
108
|
+
#
|
109
|
+
# @see Mock#stubs
|
110
|
+
def stubs(stubbed_methods_vs_return_values)
|
111
|
+
if frozen?
|
112
|
+
raise StubbingError.new("can't stub method on frozen object: #{mocha_inspect}", caller)
|
113
|
+
end
|
94
114
|
expectation = nil
|
95
115
|
mockery = Mocha::Mockery.instance
|
96
|
-
iterator = ArgumentIterator.new(
|
116
|
+
iterator = ArgumentIterator.new(stubbed_methods_vs_return_values)
|
97
117
|
iterator.each { |*args|
|
98
118
|
method_name = args.shift
|
99
119
|
mockery.on_stubbing(self, method_name)
|
@@ -104,26 +124,24 @@ module Mocha
|
|
104
124
|
}
|
105
125
|
expectation
|
106
126
|
end
|
107
|
-
|
108
|
-
#
|
127
|
+
|
128
|
+
# Removes the specified stubbed methods (added by calls to {#expects} or {#stubs}) and all expectations associated with them.
|
129
|
+
#
|
130
|
+
# Restores the original behaviour of the methods before they were stubbed.
|
109
131
|
#
|
110
|
-
#
|
111
|
-
#
|
132
|
+
# WARNING: If you {#unstub} a method which still has unsatisfied expectations, you may be removing the only way those expectations can be satisfied. Use {#unstub} with care.
|
133
|
+
#
|
134
|
+
# @param [Array<Symbol>] method_names names of methods to unstub.
|
135
|
+
#
|
136
|
+
# @example Stubbing and unstubbing a method on a real (non-mock) object.
|
112
137
|
# multiplier = Multiplier.new
|
113
138
|
# multiplier.double(2) # => 4
|
114
|
-
# multiplier.stubs(:double).raises
|
139
|
+
# multiplier.stubs(:double).raises # new behaviour defined
|
115
140
|
# multiplier.double(2) # => raises exception
|
116
|
-
# multiplier.unstub(:double)
|
141
|
+
# multiplier.unstub(:double) # original behaviour restored
|
117
142
|
# multiplier.double(2) # => 4
|
118
143
|
#
|
119
|
-
#
|
120
|
-
#
|
121
|
-
# The original implementation of <tt>Multiplier#double</tt> is restored when #unstub is called.
|
122
|
-
#
|
123
|
-
# WARNING: If you #unstub a method which still has unsatisfied expectations, you may be removing
|
124
|
-
# the only way those expectations can be satisfied. Use #unstub with care.
|
125
|
-
#
|
126
|
-
# If multiple +method_names+ are supplied, each method is unstubbed.
|
144
|
+
# @example Unstubbing multiple methods on a real (non-mock) object.
|
127
145
|
# multiplier.unstub(:double, :triple)
|
128
146
|
#
|
129
147
|
# # exactly equivalent to
|
@@ -137,8 +155,9 @@ module Mocha
|
|
137
155
|
mockery.stubba.unstub(method)
|
138
156
|
end
|
139
157
|
end
|
140
|
-
|
141
|
-
|
158
|
+
|
159
|
+
# @private
|
160
|
+
def method_exists?(method, include_public_methods = true)
|
142
161
|
if include_public_methods
|
143
162
|
return true if public_methods(include_superclass_methods = true).include?(method)
|
144
163
|
return true if respond_to?(method.to_sym)
|
@@ -147,30 +166,33 @@ module Mocha
|
|
147
166
|
return true if private_methods(include_superclass_methods = true).include?(method)
|
148
167
|
return false
|
149
168
|
end
|
150
|
-
|
169
|
+
|
151
170
|
end
|
152
|
-
|
153
|
-
|
154
|
-
|
171
|
+
|
172
|
+
# @private
|
173
|
+
module ModuleMethods
|
174
|
+
|
155
175
|
def stubba_method
|
156
176
|
Mocha::ModuleMethod
|
157
177
|
end
|
158
|
-
|
178
|
+
|
159
179
|
end
|
160
|
-
|
161
|
-
# Methods added all classes to allow mocking and stubbing on real objects.
|
180
|
+
|
181
|
+
# Methods added to all classes to allow mocking and stubbing on real (i.e. non-mock) objects.
|
162
182
|
module ClassMethods
|
163
|
-
|
164
|
-
|
183
|
+
|
184
|
+
# @private
|
185
|
+
def stubba_method
|
165
186
|
Mocha::ClassMethod
|
166
187
|
end
|
167
188
|
|
168
|
-
|
169
|
-
|
189
|
+
# @private
|
190
|
+
class AnyInstance
|
191
|
+
|
170
192
|
def initialize(klass)
|
171
193
|
@stubba_object = klass
|
172
194
|
end
|
173
|
-
|
195
|
+
|
174
196
|
def mocha
|
175
197
|
@mocha ||= Mocha::Mockery.instance.mock_impersonating_any_instance_of(@stubba_object)
|
176
198
|
end
|
@@ -178,11 +200,11 @@ module Mocha
|
|
178
200
|
def stubba_method
|
179
201
|
Mocha::AnyInstanceMethod
|
180
202
|
end
|
181
|
-
|
203
|
+
|
182
204
|
def stubba_object
|
183
205
|
@stubba_object
|
184
206
|
end
|
185
|
-
|
207
|
+
|
186
208
|
def method_exists?(method, include_public_methods = true)
|
187
209
|
if include_public_methods
|
188
210
|
return true if @stubba_object.public_instance_methods(include_superclass_methods = true).include?(method)
|
@@ -191,33 +213,40 @@ module Mocha
|
|
191
213
|
return true if @stubba_object.private_instance_methods(include_superclass_methods = true).include?(method)
|
192
214
|
return false
|
193
215
|
end
|
194
|
-
|
216
|
+
|
195
217
|
end
|
196
|
-
|
197
|
-
#
|
218
|
+
|
219
|
+
# @return [Mock] a mock object which will detect calls to any instance of this class.
|
220
|
+
# @raise [StubbingError] if attempting to stub method which is not allowed.
|
198
221
|
#
|
199
|
-
#
|
222
|
+
# @example Return false to invocation of +Product#save+ for any instance of +Product+.
|
200
223
|
# Product.any_instance.stubs(:save).returns(false)
|
201
224
|
# product_1 = Product.new
|
202
225
|
# assert_equal false, product_1.save
|
203
226
|
# product_2 = Product.new
|
204
227
|
# assert_equal false, product_2.save
|
205
228
|
def any_instance
|
229
|
+
if frozen?
|
230
|
+
raise StubbingError.new("can't stub method on frozen object: #{mocha_inspect}.any_instance", caller)
|
231
|
+
end
|
206
232
|
@any_instance ||= AnyInstance.new(self)
|
207
233
|
end
|
208
|
-
|
234
|
+
|
209
235
|
end
|
210
|
-
|
236
|
+
|
211
237
|
end
|
212
238
|
|
213
|
-
|
239
|
+
# @private
|
240
|
+
class Object
|
214
241
|
include Mocha::ObjectMethods
|
215
242
|
end
|
216
243
|
|
217
|
-
|
244
|
+
# @private
|
245
|
+
class Module
|
218
246
|
include Mocha::ModuleMethods
|
219
247
|
end
|
220
248
|
|
221
|
-
|
249
|
+
# @private
|
250
|
+
class Class
|
222
251
|
include Mocha::ClassMethods
|
223
252
|
end
|