mocha 1.9.0 → 1.10.0.alpha
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/.rubocop_todo.yml +7 -30
- data/README.md +10 -11
- data/RELEASE.md +48 -0
- data/Rakefile +9 -3
- data/docs/CNAME +1 -0
- data/docs/Mocha.html +132 -5
- data/docs/Mocha/API.html +289 -216
- data/docs/Mocha/ClassMethods.html +88 -16
- data/docs/Mocha/Configuration.html +931 -174
- data/docs/Mocha/Expectation.html +200 -255
- data/docs/Mocha/ExpectationError.html +5 -10
- data/docs/Mocha/ExpectationErrorFactory.html +9 -18
- data/docs/Mocha/Hooks.html +12 -27
- data/docs/Mocha/Integration.html +3 -3
- data/docs/Mocha/Integration/MiniTest.html +3 -3
- data/docs/Mocha/Integration/MiniTest/Adapter.html +5 -6
- data/docs/Mocha/Integration/TestUnit.html +3 -3
- data/docs/Mocha/Integration/TestUnit/Adapter.html +5 -6
- data/docs/Mocha/Mock.html +202 -162
- data/docs/Mocha/ObjectMethods.html +121 -68
- data/docs/Mocha/ParameterMatchers.html +21 -109
- data/docs/Mocha/ParameterMatchers/AllOf.html +4 -5
- data/docs/Mocha/ParameterMatchers/AnyOf.html +4 -5
- data/docs/Mocha/ParameterMatchers/AnyParameters.html +3 -3
- data/docs/Mocha/ParameterMatchers/Anything.html +3 -3
- data/docs/Mocha/ParameterMatchers/Base.html +8 -15
- data/docs/Mocha/ParameterMatchers/Equals.html +4 -5
- data/docs/Mocha/ParameterMatchers/EquivalentUri.html +3 -3
- data/docs/Mocha/ParameterMatchers/HasEntries.html +4 -5
- data/docs/Mocha/ParameterMatchers/HasEntry.html +4 -5
- data/docs/Mocha/ParameterMatchers/HasKey.html +4 -5
- data/docs/Mocha/ParameterMatchers/HasValue.html +4 -5
- data/docs/Mocha/ParameterMatchers/Includes.html +4 -5
- data/docs/Mocha/ParameterMatchers/InstanceOf.html +4 -5
- data/docs/Mocha/ParameterMatchers/IsA.html +3 -3
- data/docs/Mocha/ParameterMatchers/KindOf.html +4 -5
- data/docs/Mocha/ParameterMatchers/Not.html +4 -5
- data/docs/Mocha/ParameterMatchers/Optionally.html +3 -3
- data/docs/Mocha/ParameterMatchers/RegexpMatches.html +4 -5
- data/docs/Mocha/ParameterMatchers/RespondsWith.html +4 -5
- data/docs/Mocha/ParameterMatchers/YamlEquivalent.html +4 -5
- data/docs/Mocha/Sequence.html +3 -3
- data/docs/Mocha/StateMachine.html +13 -25
- data/docs/Mocha/StateMachine/State.html +4 -5
- data/docs/Mocha/StateMachine/StatePredicate.html +4 -5
- data/docs/Mocha/StubbingError.html +3 -3
- data/docs/_index.html +4 -22
- data/docs/class_list.html +1 -1
- data/docs/file.COPYING.html +3 -3
- data/docs/file.MIT-LICENSE.html +3 -3
- data/docs/file.README.html +18 -15
- data/docs/file.RELEASE.html +58 -3
- data/docs/frames.html +1 -1
- data/docs/index.html +18 -15
- data/docs/method_list.html +103 -39
- data/docs/top-level-namespace.html +3 -3
- data/gemfiles/Gemfile.minitest.5.11.3 +7 -0
- data/init.rb +1 -3
- data/lib/mocha.rb +8 -0
- data/lib/mocha/any_instance_method.rb +9 -25
- data/lib/mocha/api.rb +62 -66
- data/lib/mocha/cardinality.rb +26 -11
- data/lib/mocha/class_methods.rb +16 -15
- data/lib/mocha/configuration.rb +295 -67
- data/lib/mocha/detection/test_unit.rb +1 -3
- data/lib/mocha/exception_raiser.rb +2 -1
- data/lib/mocha/expectation.rb +56 -58
- data/lib/mocha/expectation_error.rb +1 -3
- data/lib/mocha/expectation_list.rb +6 -6
- data/lib/mocha/inspect.rb +28 -26
- data/lib/mocha/instance_method.rb +19 -2
- data/lib/mocha/integration.rb +1 -3
- data/lib/mocha/integration/mini_test.rb +7 -0
- data/lib/mocha/integration/test_unit.rb +7 -0
- data/lib/mocha/invocation.rb +61 -0
- data/lib/mocha/macos_version.rb +5 -0
- data/lib/mocha/minitest.rb +6 -1
- data/lib/mocha/mock.rb +46 -31
- data/lib/mocha/mockery.rb +28 -61
- data/lib/mocha/names.rb +1 -1
- data/lib/mocha/object_methods.rb +13 -19
- data/lib/mocha/parameter_matchers.rb +1 -1
- data/lib/mocha/parameter_matchers/all_of.rb +1 -1
- data/lib/mocha/parameter_matchers/any_of.rb +1 -1
- data/lib/mocha/parameter_matchers/equivalent_uri.rb +0 -9
- data/lib/mocha/parameter_matchers/includes.rb +2 -0
- data/lib/mocha/parameter_matchers/instance_methods.rb +18 -0
- data/lib/mocha/raised_exception.rb +11 -0
- data/lib/mocha/return_values.rb +3 -3
- data/lib/mocha/setup.rb +5 -0
- data/lib/mocha/single_return_value.rb +2 -1
- data/lib/mocha/singleton_class.rb +9 -0
- data/lib/mocha/{class_method.rb → stubbed_method.rb} +23 -43
- data/lib/mocha/test_unit.rb +6 -1
- data/lib/mocha/thrower.rb +2 -1
- data/lib/mocha/thrown_object.rb +12 -0
- data/lib/mocha/version.rb +1 -1
- data/mocha.gemspec +1 -3
- data/test/acceptance/acceptance_test_helper.rb +6 -0
- data/test/acceptance/bug_18914_test.rb +0 -1
- data/test/acceptance/bug_21465_test.rb +0 -1
- data/test/acceptance/bug_21563_test.rb +0 -1
- data/test/acceptance/display_matching_invocations_alongside_expectations_test.rb +69 -0
- data/test/acceptance/exception_rescue_test.rb +1 -2
- data/test/acceptance/expectations_on_multiple_methods_test.rb +0 -1
- data/test/acceptance/expected_invocation_count_test.rb +2 -3
- data/test/acceptance/failure_messages_test.rb +0 -1
- data/test/acceptance/issue_272_test.rb +1 -2
- data/test/acceptance/issue_65_test.rb +0 -1
- data/test/acceptance/issue_70_test.rb +0 -1
- data/test/acceptance/mocha_example_test.rb +0 -1
- data/test/acceptance/mocha_test_result_test.rb +0 -1
- data/test/acceptance/mock_test.rb +47 -6
- data/test/acceptance/mocked_methods_dispatch_test.rb +0 -1
- data/test/acceptance/multiple_expectations_failure_message_test.rb +0 -1
- data/test/acceptance/optional_parameters_test.rb +0 -1
- data/test/acceptance/parameter_matcher_test.rb +0 -1
- data/test/acceptance/partial_mocks_test.rb +0 -1
- data/test/acceptance/prepend_test.rb +0 -1
- data/test/acceptance/prevent_use_of_mocha_outside_test_test.rb +0 -1
- data/test/acceptance/raise_exception_test.rb +0 -1
- data/test/acceptance/return_value_test.rb +0 -1
- data/test/acceptance/sequence_test.rb +0 -1
- data/test/acceptance/states_test.rb +0 -1
- data/test/acceptance/stub_any_instance_method_defined_on_superclass_test.rb +1 -2
- data/test/acceptance/stub_any_instance_method_test.rb +20 -1
- data/test/acceptance/stub_class_method_defined_on_active_record_association_proxy_test.rb +0 -1
- data/test/acceptance/stub_class_method_defined_on_class_test.rb +0 -1
- data/test/acceptance/stub_class_method_defined_on_module_test.rb +0 -1
- data/test/acceptance/stub_class_method_defined_on_superclass_test.rb +1 -2
- data/test/acceptance/stub_everything_test.rb +0 -1
- data/test/acceptance/stub_instance_method_defined_on_active_record_association_proxy_test.rb +0 -1
- data/test/acceptance/stub_instance_method_defined_on_class_and_aliased_test.rb +0 -1
- data/test/acceptance/stub_instance_method_defined_on_class_test.rb +0 -1
- data/test/acceptance/stub_instance_method_defined_on_kernel_module_test.rb +0 -1
- data/test/acceptance/stub_instance_method_defined_on_module_test.rb +0 -1
- data/test/acceptance/stub_instance_method_defined_on_object_class_test.rb +0 -1
- data/test/acceptance/stub_instance_method_defined_on_singleton_class_test.rb +0 -1
- data/test/acceptance/stub_instance_method_defined_on_superclass_test.rb +0 -1
- data/test/acceptance/stub_method_defined_on_module_and_aliased_test.rb +0 -1
- data/test/acceptance/stub_module_method_test.rb +0 -1
- data/test/acceptance/stub_test.rb +0 -1
- data/test/acceptance/stubba_example_test.rb +0 -1
- data/test/acceptance/stubba_test_result_test.rb +0 -1
- data/test/acceptance/stubbing_error_backtrace_test.rb +4 -5
- data/test/acceptance/stubbing_frozen_object_test.rb +0 -1
- data/test/acceptance/stubbing_method_accepting_block_parameter_test.rb +0 -1
- data/test/acceptance/stubbing_method_unnecessarily_test.rb +5 -5
- data/test/acceptance/stubbing_nil_test.rb +5 -5
- data/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +11 -11
- data/test/acceptance/stubbing_non_existent_class_method_test.rb +11 -11
- data/test/acceptance/stubbing_non_existent_instance_method_test.rb +11 -11
- data/test/acceptance/stubbing_non_public_any_instance_method_test.rb +8 -8
- data/test/acceptance/stubbing_non_public_class_method_test.rb +9 -9
- data/test/acceptance/stubbing_non_public_instance_method_test.rb +9 -9
- data/test/acceptance/stubbing_on_non_mock_object_test.rb +5 -5
- data/test/acceptance/stubbing_same_class_method_on_parent_and_child_classes_test.rb +0 -1
- data/test/acceptance/throw_test.rb +0 -1
- data/test/acceptance/unexpected_invocation_test.rb +0 -1
- data/test/acceptance/unstubbing_test.rb +0 -1
- data/test/integration/shared_tests.rb +5 -3
- data/test/method_definer.rb +11 -17
- data/test/test_runner.rb +2 -0
- data/test/unit/any_instance_method_test.rb +41 -40
- data/test/unit/cardinality_test.rb +41 -23
- data/test/unit/central_test.rb +0 -1
- data/test/unit/class_methods_test.rb +1 -1
- data/test/unit/configuration_test.rb +12 -12
- data/test/unit/exception_raiser_test.rb +10 -5
- data/test/unit/expectation_list_test.rb +13 -11
- data/test/unit/expectation_test.rb +88 -103
- data/test/unit/instance_method_test.rb +282 -0
- data/test/unit/mock_test.rb +28 -19
- data/test/unit/mockery_test.rb +8 -11
- data/test/unit/module_methods_test.rb +2 -3
- data/test/unit/object_inspect_test.rb +6 -4
- data/test/unit/object_methods_test.rb +3 -2
- data/test/unit/parameter_matchers/equivalent_uri_test.rb +0 -9
- data/test/unit/parameter_matchers/has_entries_test.rb +1 -1
- data/test/unit/parameter_matchers/has_entry_test.rb +1 -1
- data/test/unit/parameter_matchers/has_key_test.rb +1 -1
- data/test/unit/parameter_matchers/has_value_test.rb +1 -1
- data/test/unit/parameter_matchers/includes_test.rb +1 -1
- data/test/unit/parameter_matchers/responds_with_test.rb +1 -1
- data/test/unit/return_values_test.rb +25 -20
- data/test/unit/single_return_value_test.rb +6 -1
- data/test/unit/thrower_test.rb +7 -2
- metadata +16 -30
- data/docs/Mocha/UnexpectedInvocation.html +0 -140
- data/lib/mocha/mini_test.rb +0 -5
- data/lib/mocha/module_method.rb +0 -6
- data/lib/mocha/module_methods.rb +0 -10
- data/lib/mocha/parameter_matchers/object.rb +0 -15
- data/lib/mocha/standalone.rb +0 -4
- data/lib/mocha/unexpected_invocation.rb +0 -24
- data/lib/mocha_standalone.rb +0 -4
- data/test/acceptance/mock_with_initializer_block_test.rb +0 -56
- data/test/unit/class_method_test.rb +0 -276
@@ -0,0 +1,282 @@
|
|
1
|
+
require File.expand_path('../../test_helper', __FILE__)
|
2
|
+
require 'method_definer'
|
3
|
+
require 'mocha/class_methods'
|
4
|
+
require 'mocha/mock'
|
5
|
+
require 'mocha/singleton_class'
|
6
|
+
|
7
|
+
require 'mocha/instance_method'
|
8
|
+
|
9
|
+
class InstanceMethodTest < Mocha::TestCase
|
10
|
+
include MethodDefiner
|
11
|
+
include Mocha
|
12
|
+
|
13
|
+
def class_with_method(method, result = nil)
|
14
|
+
Class.new do
|
15
|
+
extend ClassMethods
|
16
|
+
singleton_class.extend(ClassMethods)
|
17
|
+
singleton_class.send(:define_method, method) { result } if method
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
unless RUBY_V2_PLUS
|
22
|
+
def test_should_hide_original_method
|
23
|
+
klass = class_with_method(:method_x)
|
24
|
+
klass.singleton_class.send(:alias_method, :_method, :method)
|
25
|
+
method = InstanceMethod.new(klass, :method_x)
|
26
|
+
|
27
|
+
method.hide_original_method
|
28
|
+
|
29
|
+
assert_equal false, klass.respond_to?(:method_x)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_should_not_raise_error_hiding_method_that_isnt_defined
|
34
|
+
klass = class_with_method(:irrelevant)
|
35
|
+
method = InstanceMethod.new(klass, :method_x)
|
36
|
+
|
37
|
+
assert_nothing_raised { method.hide_original_method }
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_should_not_raise_error_hiding_method_in_class_that_implements_method_called_method
|
41
|
+
klass = class_with_method(:method)
|
42
|
+
method = InstanceMethod.new(klass, :method)
|
43
|
+
|
44
|
+
assert_nothing_raised { method.hide_original_method }
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_should_define_a_new_method_which_should_call_mocha_method_missing
|
48
|
+
klass = class_with_method(:method_x)
|
49
|
+
mocha = build_mock
|
50
|
+
define_instance_method(klass, :mocha) { mocha }
|
51
|
+
mocha.expects(:method_x).with(:param1, :param2).returns(:result)
|
52
|
+
method = InstanceMethod.new(klass, :method_x)
|
53
|
+
|
54
|
+
method.hide_original_method
|
55
|
+
method.define_new_method
|
56
|
+
result = klass.method_x(:param1, :param2)
|
57
|
+
|
58
|
+
assert_equal :result, result
|
59
|
+
assert mocha.__verified__?
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_should_include_the_filename_and_line_number_in_exceptions
|
63
|
+
klass = class_with_method(:method_x)
|
64
|
+
mocha = build_mock
|
65
|
+
define_instance_method(klass, :mocha) { mocha }
|
66
|
+
mocha.stubs(:method_x).raises(Exception)
|
67
|
+
method = InstanceMethod.new(klass, :method_x)
|
68
|
+
|
69
|
+
method.hide_original_method
|
70
|
+
method.define_new_method
|
71
|
+
|
72
|
+
expected_filename = 'stubbed_method.rb'
|
73
|
+
expected_line_number = 61
|
74
|
+
|
75
|
+
exception = assert_raises(Exception) { klass.method_x }
|
76
|
+
matching_line = exception.backtrace.find do |line|
|
77
|
+
filename, line_number, _context = line.split(':')
|
78
|
+
filename.include?(expected_filename) && line_number.to_i == expected_line_number
|
79
|
+
end
|
80
|
+
|
81
|
+
assert_not_nil matching_line, "Expected to find #{expected_filename}:#{expected_line_number} in the backtrace:\n #{exception.backtrace.join("\n")}"
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_should_remove_new_method
|
85
|
+
klass = class_with_method(:method_x)
|
86
|
+
method = InstanceMethod.new(klass, :method_x)
|
87
|
+
|
88
|
+
method.remove_new_method
|
89
|
+
|
90
|
+
assert_equal false, klass.respond_to?(:method_x)
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_should_restore_original_method
|
94
|
+
klass = class_with_method(:method_x, :original_result)
|
95
|
+
klass.singleton_class.send(:alias_method, :_method, :method)
|
96
|
+
method = InstanceMethod.new(klass, :method_x)
|
97
|
+
|
98
|
+
method.hide_original_method
|
99
|
+
method.define_new_method
|
100
|
+
method.remove_new_method
|
101
|
+
method.restore_original_method
|
102
|
+
|
103
|
+
assert klass.respond_to?(:method_x)
|
104
|
+
assert_equal :original_result, klass.method_x
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_should_restore_original_method_accepting_a_block_parameter
|
108
|
+
klass = Class.new do
|
109
|
+
extend ClassMethods
|
110
|
+
singleton_class.extend(ClassMethods)
|
111
|
+
def self.method_x(&block)
|
112
|
+
block.call if block_given?
|
113
|
+
end
|
114
|
+
end
|
115
|
+
klass.singleton_class.send(:alias_method, :_method, :method)
|
116
|
+
method = InstanceMethod.new(klass, :method_x)
|
117
|
+
|
118
|
+
method.hide_original_method
|
119
|
+
method.define_new_method
|
120
|
+
method.remove_new_method
|
121
|
+
method.restore_original_method
|
122
|
+
|
123
|
+
block_called = false
|
124
|
+
klass.method_x { block_called = true }
|
125
|
+
assert block_called
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_should_not_restore_original_method_if_none_was_defined_in_first_place
|
129
|
+
klass = class_with_method(:method_x, :new_result)
|
130
|
+
method = InstanceMethod.new(klass, :method_x)
|
131
|
+
|
132
|
+
method.restore_original_method
|
133
|
+
|
134
|
+
assert_equal :new_result, klass.method_x
|
135
|
+
end
|
136
|
+
|
137
|
+
def test_should_call_hide_original_method
|
138
|
+
klass = class_with_method(:method_x)
|
139
|
+
method = InstanceMethod.new(klass, :method_x)
|
140
|
+
method.hide_original_method
|
141
|
+
define_instance_accessor(method, :hide_called)
|
142
|
+
replace_instance_method(method, :hide_original_method) { self.hide_called = true }
|
143
|
+
|
144
|
+
method.stub
|
145
|
+
|
146
|
+
assert method.hide_called
|
147
|
+
end
|
148
|
+
|
149
|
+
def test_should_call_define_new_method
|
150
|
+
klass = class_with_method(:method_x)
|
151
|
+
method = InstanceMethod.new(klass, :method_x)
|
152
|
+
define_instance_accessor(method, :define_called)
|
153
|
+
replace_instance_method(method, :define_new_method) { self.define_called = true }
|
154
|
+
|
155
|
+
method.stub
|
156
|
+
|
157
|
+
assert method.define_called
|
158
|
+
end
|
159
|
+
|
160
|
+
def test_should_call_remove_new_method
|
161
|
+
klass = class_with_method(:method_x)
|
162
|
+
method = InstanceMethod.new(klass, :method_x)
|
163
|
+
mocha = build_mock
|
164
|
+
define_instance_method(klass, :mocha) { mocha }
|
165
|
+
replace_instance_method(method, :reset_mocha) {}
|
166
|
+
define_instance_accessor(method, :remove_called)
|
167
|
+
replace_instance_method(method, :remove_new_method) { self.remove_called = true }
|
168
|
+
|
169
|
+
method.unstub
|
170
|
+
|
171
|
+
assert method.remove_called
|
172
|
+
end
|
173
|
+
|
174
|
+
def test_should_call_restore_original_method
|
175
|
+
klass = class_with_method(:method_x)
|
176
|
+
mocha = build_mock
|
177
|
+
define_instance_method(klass, :mocha) { mocha }
|
178
|
+
method = InstanceMethod.new(klass, :method_x)
|
179
|
+
replace_instance_method(method, :reset_mocha) {}
|
180
|
+
define_instance_accessor(method, :restore_called)
|
181
|
+
replace_instance_method(method, :restore_original_method) { self.restore_called = true }
|
182
|
+
|
183
|
+
method.unstub
|
184
|
+
|
185
|
+
assert method.restore_called
|
186
|
+
end
|
187
|
+
|
188
|
+
def test_should_call_mocha_unstub
|
189
|
+
klass = class_with_method(:method_x)
|
190
|
+
method = InstanceMethod.new(klass, :method_x)
|
191
|
+
replace_instance_method(method, :restore_original_method) {}
|
192
|
+
mocha = Class.new do
|
193
|
+
class << self
|
194
|
+
attr_accessor :unstub_method
|
195
|
+
end
|
196
|
+
def self.unstub(method)
|
197
|
+
self.unstub_method = method
|
198
|
+
end
|
199
|
+
end
|
200
|
+
define_instance_method(mocha, :any_expectations?) { true }
|
201
|
+
replace_instance_method(method, :mock) { mocha }
|
202
|
+
|
203
|
+
method.unstub
|
204
|
+
assert_equal mocha.unstub_method, :method_x
|
205
|
+
end
|
206
|
+
|
207
|
+
def test_should_call_stubbee_reset_mocha_if_no_expectations_remaining
|
208
|
+
klass = class_with_method(:method_x)
|
209
|
+
method = InstanceMethod.new(klass, :method_x)
|
210
|
+
replace_instance_method(method, :remove_new_method) {}
|
211
|
+
replace_instance_method(method, :restore_original_method) {}
|
212
|
+
mocha = Class.new
|
213
|
+
define_instance_method(mocha, :unstub) { |method_name| }
|
214
|
+
define_instance_method(mocha, :any_expectations?) { false }
|
215
|
+
replace_instance_method(method, :mock) { mocha }
|
216
|
+
stubbee = Class.new do
|
217
|
+
attr_accessor :reset_mocha_called
|
218
|
+
def reset_mocha
|
219
|
+
self.reset_mocha_called = true
|
220
|
+
end
|
221
|
+
end.new
|
222
|
+
replace_instance_method(method, :stubbee) { stubbee }
|
223
|
+
|
224
|
+
method.unstub
|
225
|
+
|
226
|
+
assert stubbee.reset_mocha_called
|
227
|
+
end
|
228
|
+
|
229
|
+
def test_should_return_mock_for_stubbee
|
230
|
+
mocha = Object.new
|
231
|
+
stubbee = Object.new
|
232
|
+
define_instance_method(stubbee, :mocha) { mocha }
|
233
|
+
method = InstanceMethod.new(stubbee, :method_name)
|
234
|
+
assert_equal mocha, method.mock
|
235
|
+
end
|
236
|
+
|
237
|
+
def test_should_not_match_if_other_object_has_a_different_class
|
238
|
+
method = InstanceMethod.new(Object.new, :method)
|
239
|
+
other_object = Object.new
|
240
|
+
assert !method.matches?(other_object)
|
241
|
+
end
|
242
|
+
|
243
|
+
def test_should_not_match_if_other_instance_method_has_different_stubbee
|
244
|
+
stubbee1 = Object.new
|
245
|
+
stubbee2 = Object.new
|
246
|
+
method1 = InstanceMethod.new(stubbee1, :method)
|
247
|
+
method2 = InstanceMethod.new(stubbee2, :method)
|
248
|
+
assert !method1.matches?(method2)
|
249
|
+
end
|
250
|
+
|
251
|
+
def test_should_not_match_if_other_instance_method_has_different_method
|
252
|
+
stubbee = Object.new
|
253
|
+
method1 = InstanceMethod.new(stubbee, :method_1)
|
254
|
+
method2 = InstanceMethod.new(stubbee, :method_2)
|
255
|
+
assert !method1.matches?(method2)
|
256
|
+
end
|
257
|
+
|
258
|
+
def test_should_match_if_other_instance_method_has_same_stubbee_and_same_method_so_no_attempt_is_made_to_stub_a_method_twice
|
259
|
+
stubbee = Object.new
|
260
|
+
method1 = InstanceMethod.new(stubbee, :method)
|
261
|
+
method2 = InstanceMethod.new(stubbee, :method)
|
262
|
+
assert method1.matches?(method2)
|
263
|
+
end
|
264
|
+
|
265
|
+
def test_should_match_if_other_instance_method_has_same_stubbee_and_same_method_but_stubbee_equal_method_lies_like_active_record_association_proxy
|
266
|
+
stubbee = Class.new do
|
267
|
+
extend ClassMethods
|
268
|
+
def equal?(_other)
|
269
|
+
false
|
270
|
+
end
|
271
|
+
end.new
|
272
|
+
method1 = InstanceMethod.new(stubbee, :method)
|
273
|
+
method2 = InstanceMethod.new(stubbee, :method)
|
274
|
+
assert method1.matches?(method2)
|
275
|
+
end
|
276
|
+
|
277
|
+
private
|
278
|
+
|
279
|
+
def build_mock
|
280
|
+
Mock.new(nil)
|
281
|
+
end
|
282
|
+
end
|
data/test/unit/mock_test.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require File.expand_path('../../test_helper', __FILE__)
|
2
2
|
require 'mocha/ruby_version'
|
3
|
+
require 'mocha/macos_version'
|
3
4
|
require 'mocha/mock'
|
4
5
|
require 'mocha/expectation_error_factory'
|
5
6
|
require 'set'
|
@@ -44,24 +45,29 @@ class MockTest < Mocha::TestCase
|
|
44
45
|
assert_equal true, mock.eql?(mock)
|
45
46
|
end
|
46
47
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
48
|
+
PRE_RUBY_V19_EXCLUDED_METHODS = %w[
|
49
|
+
method_missing
|
50
|
+
singleton_method_undefined
|
51
|
+
initialize
|
52
|
+
].freeze
|
53
|
+
|
54
|
+
MACOS_EXCLUDED_METHODS =
|
55
|
+
MACOS && MACOS_VERSION >= MACOS_MOJAVE_VERSION ? [:syscall] : []
|
56
|
+
|
57
|
+
RUBY_V19_AND_LATER_EXCLUDED_METHODS = [
|
58
|
+
:object_id,
|
59
|
+
:method_missing,
|
60
|
+
:singleton_method_undefined,
|
61
|
+
:initialize,
|
62
|
+
:String,
|
63
|
+
:singleton_method_added,
|
64
|
+
*MACOS_EXCLUDED_METHODS
|
65
|
+
].freeze
|
62
66
|
|
63
67
|
OBJECT_METHODS = STANDARD_OBJECT_PUBLIC_INSTANCE_METHODS.reject do |m|
|
64
|
-
m =~ /^__.*__$/ ||
|
68
|
+
(m =~ /^__.*__$/) ||
|
69
|
+
(PRE_RUBY_V19 && PRE_RUBY_V19_EXCLUDED_METHODS.include?(m)) ||
|
70
|
+
(!PRE_RUBY_V19 && RUBY_V19_AND_LATER_EXCLUDED_METHODS.include?(m))
|
65
71
|
end
|
66
72
|
|
67
73
|
def test_should_be_able_to_mock_standard_object_methods
|
@@ -124,7 +130,7 @@ class MockTest < Mocha::TestCase
|
|
124
130
|
|
125
131
|
def test_should_raise_assertion_error_for_unexpected_method_call
|
126
132
|
mock = build_mock
|
127
|
-
error =
|
133
|
+
error = assert_raises(ExpectationErrorFactory.exception_class) do
|
128
134
|
mock.unexpected_method_called(:my_method, :argument1, :argument2)
|
129
135
|
end
|
130
136
|
assert_match(/unexpected invocation/, error.message)
|
@@ -339,12 +345,15 @@ class MockTest < Mocha::TestCase
|
|
339
345
|
assert mock.respond_to?(:xyz)
|
340
346
|
end
|
341
347
|
|
342
|
-
def
|
348
|
+
def test_should_remove_expectations_for_unstubbed_methods
|
343
349
|
mock = build_mock
|
344
350
|
mock.expects(:method1)
|
345
|
-
mock.
|
351
|
+
mock.expects(:method2)
|
352
|
+
mock.unstub(:method1, :method2)
|
346
353
|
e = assert_raises(ExpectationErrorFactory.exception_class) { mock.method1 }
|
347
354
|
assert_match(/unexpected invocation/, e.message)
|
355
|
+
e = assert_raises(ExpectationErrorFactory.exception_class) { mock.method2 }
|
356
|
+
assert_match(/unexpected invocation/, e.message)
|
348
357
|
end
|
349
358
|
|
350
359
|
unless PRE_RUBY_V19
|
data/test/unit/mockery_test.rb
CHANGED
@@ -2,11 +2,9 @@ require File.expand_path('../../test_helper', __FILE__)
|
|
2
2
|
require 'mocha/mockery'
|
3
3
|
require 'mocha/state_machine'
|
4
4
|
require 'mocha/expectation_error_factory'
|
5
|
-
require 'deprecation_disabler'
|
6
5
|
|
7
6
|
class MockeryTest < Mocha::TestCase
|
8
7
|
include Mocha
|
9
|
-
include DeprecationDisabler
|
10
8
|
|
11
9
|
def setup
|
12
10
|
Mockery.setup
|
@@ -44,20 +42,19 @@ class MockeryTest < Mocha::TestCase
|
|
44
42
|
|
45
43
|
def test_should_raise_expectation_error_because_not_all_expectations_are_satisfied
|
46
44
|
mockery = Mockery.new
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
45
|
+
mock1 = mockery.named_mock('mock-1')
|
46
|
+
mock1.expects(:method_1)
|
47
|
+
mock2 = mockery.named_mock('mock-2')
|
48
|
+
mock2.expects(:method_2)
|
49
|
+
1.times { mock1.method_1 }
|
50
|
+
0.times { mock2.method_2 }
|
53
51
|
assert_raises(ExpectationErrorFactory.exception_class) { mockery.verify }
|
54
52
|
end
|
55
53
|
|
56
54
|
def test_should_reset_list_of_mocks_on_teardown
|
57
55
|
mockery = Mockery.new
|
58
|
-
|
59
|
-
|
60
|
-
end
|
56
|
+
mock = mockery.unnamed_mock
|
57
|
+
mock.expects(:my_method)
|
61
58
|
mockery.teardown
|
62
59
|
assert_nothing_raised(ExpectationErrorFactory.exception_class) { mockery.verify }
|
63
60
|
end
|
@@ -1,14 +1,13 @@
|
|
1
1
|
require File.expand_path('../../test_helper', __FILE__)
|
2
|
-
require 'mocha/module_methods'
|
3
2
|
require 'mocha/object_methods'
|
4
3
|
|
5
4
|
class ModuleMethodsTest < Mocha::TestCase
|
6
5
|
def setup
|
7
|
-
@module = Module.new.extend(Mocha::
|
6
|
+
@module = Module.new.extend(Mocha::ObjectMethods)
|
8
7
|
end
|
9
8
|
|
10
9
|
def test_should_use_stubba_module_method_for_module
|
11
|
-
assert_equal Mocha::
|
10
|
+
assert_equal Mocha::InstanceMethod, @module.stubba_method
|
12
11
|
end
|
13
12
|
|
14
13
|
def test_should_stub_self_for_module
|
@@ -4,6 +4,8 @@ require 'mocha/inspect'
|
|
4
4
|
require 'method_definer'
|
5
5
|
|
6
6
|
class ObjectInspectTest < Mocha::TestCase
|
7
|
+
include MethodDefiner
|
8
|
+
|
7
9
|
def test_should_return_default_string_representation_of_object_not_including_instance_variables
|
8
10
|
object = Object.new
|
9
11
|
class << object
|
@@ -26,20 +28,20 @@ class ObjectInspectTest < Mocha::TestCase
|
|
26
28
|
calls = []
|
27
29
|
object = Object.new
|
28
30
|
if Mocha::PRE_RUBY_V19
|
29
|
-
|
31
|
+
replace_instance_method(object, :id) do
|
30
32
|
calls << :id
|
31
33
|
return 1
|
32
34
|
end
|
33
35
|
end
|
34
|
-
|
36
|
+
replace_instance_method(object, :object_id) do
|
35
37
|
calls << :object_id
|
36
38
|
return 1
|
37
39
|
end
|
38
|
-
|
40
|
+
replace_instance_method(object, :__id__) do
|
39
41
|
calls << :__id__
|
40
42
|
return 1
|
41
43
|
end
|
42
|
-
|
44
|
+
replace_instance_method(object, :inspect) { 'object-description' }
|
43
45
|
|
44
46
|
object.mocha_inspect
|
45
47
|
|