-
-
Notifications
You must be signed in to change notification settings - Fork 156
/
mock_test.rb
350 lines (297 loc) · 10.4 KB
/
mock_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
require File.expand_path('../../test_helper', __FILE__)
require 'mocha/ruby_version'
require 'mocha/mock'
require 'mocha/expectation_error_factory'
require 'set'
require 'simple_counter'
class MockTest < Mocha::TestCase
include Mocha
def test_should_set_single_expectation
mock = build_mock
mock.expects(:method1).returns(1)
assert_nothing_raised(ExpectationErrorFactory.exception_class) do
assert_equal 1, mock.method1
end
end
def test_should_build_and_store_expectations
mock = build_mock
expectation = mock.expects(:method1)
assert_not_nil expectation
assert_equal [expectation], mock.__expectations__.to_a
end
def test_should_not_stub_everything_by_default
mock = build_mock
assert_equal false, mock.everything_stubbed
end
def test_should_stub_everything
mock = build_mock
mock.stub_everything
assert_equal true, mock.everything_stubbed
end
def test_should_be_able_to_extend_mock_object_with_module
mock = build_mock
assert_nothing_raised(ExpectationErrorFactory.exception_class) { mock.extend(Module.new) }
end
def test_should_be_equal
mock = build_mock
assert_equal true, mock.eql?(mock)
end
if PRE_RUBY_V19
OBJECT_METHODS = STANDARD_OBJECT_PUBLIC_INSTANCE_METHODS.reject { |m| m =~ /^__.*__$/ || ["method_missing", "singleton_method_undefined", "initialize"].include?(m)}
else
OBJECT_METHODS = STANDARD_OBJECT_PUBLIC_INSTANCE_METHODS.reject { |m| m =~ /^__.*__$/ || [:object_id, :method_missing, :singleton_method_undefined, :initialize, :String, :singleton_method_added].include?(m) }
end
def test_should_be_able_to_mock_standard_object_methods
mock = build_mock
OBJECT_METHODS.each { |method| mock.__expects__(method.to_sym).returns(method) }
OBJECT_METHODS.each { |method| assert_equal method, mock.__send__(method.to_sym) }
assert mock.__verified__?
end
def test_should_be_able_to_stub_standard_object_methods
mock = build_mock
OBJECT_METHODS.each { |method| mock.__stubs__(method.to_sym).returns(method) }
OBJECT_METHODS.each { |method| assert_equal method, mock.__send__(method.to_sym) }
end
def test_should_create_and_add_expectations
mock = build_mock
expectation1 = mock.expects(:method1)
expectation2 = mock.expects(:method2)
assert_equal [expectation1, expectation2].to_set, mock.__expectations__.to_set
end
def test_should_pass_backtrace_into_expectation
mock = build_mock
backtrace = Object.new
expectation = mock.expects(:method1, backtrace)
assert_equal backtrace, expectation.backtrace
end
def test_should_pass_backtrace_into_stub
mock = build_mock
backtrace = Object.new
stub = mock.stubs(:method1, backtrace)
assert_equal backtrace, stub.backtrace
end
def test_should_create_and_add_stubs
mock = build_mock
stub1 = mock.stubs(:method1)
stub2 = mock.stubs(:method2)
assert_equal [stub1, stub2].to_set, mock.__expectations__.to_set
end
def test_should_invoke_expectation_and_return_result
mock = build_mock
mock.expects(:my_method).returns(:result)
result = mock.my_method
assert_equal :result, result
end
def test_should_not_raise_error_if_stubbing_everything
mock = build_mock
mock.stub_everything
result = nil
assert_nothing_raised(ExpectationErrorFactory.exception_class) do
result = mock.unexpected_method
end
assert_nil result
end
def test_should_raise_assertion_error_for_unexpected_method_call
mock = build_mock
error = assert_raise(ExpectationErrorFactory.exception_class) do
mock.unexpected_method_called(:my_method, :argument1, :argument2)
end
assert_match(/unexpected invocation/, error.message)
assert_match(/my_method/, error.message)
assert_match(/argument1/, error.message)
assert_match(/argument2/, error.message)
end
def test_should_not_verify_successfully_because_not_all_expectations_have_been_satisfied
mock = build_mock
mock.expects(:method1)
mock.expects(:method2)
mock.method1
assert !mock.__verified__?
end
def test_should_increment_assertion_counter_for_every_verified_expectation
mock = build_mock
mock.expects(:method1)
mock.method1
mock.expects(:method2)
mock.method2
assertion_counter = SimpleCounter.new
mock.__verified__?(assertion_counter)
assert_equal 2, assertion_counter.count
end
def test_should_yield_supplied_parameters_to_block
mock = build_mock
parameters_for_yield = [1, 2, 3]
mock.expects(:method1).yields(*parameters_for_yield)
yielded_parameters = nil
mock.method1() { |*parameters| yielded_parameters = parameters }
assert_equal parameters_for_yield, yielded_parameters
end
def test_should_set_up_multiple_expectations_with_return_values
mock = build_mock
mock.expects(:method1 => :result1, :method2 => :result2)
assert_equal :result1, mock.method1
assert_equal :result2, mock.method2
end
def test_should_set_up_multiple_stubs_with_return_values
mock = build_mock
mock.stubs(:method1 => :result1, :method2 => :result2)
assert_equal :result1, mock.method1
assert_equal :result2, mock.method2
end
def test_should_keep_returning_specified_value_for_stubs
mock = build_mock
mock.stubs(:method1).returns(1)
assert_equal 1, mock.method1
assert_equal 1, mock.method1
end
def test_should_keep_returning_specified_value_for_expects
mock = build_mock
mock.expects(:method1).times(2).returns(1)
assert_equal 1, mock.method1
assert_equal 1, mock.method1
end
def test_should_match_most_recent_call_to_expects
mock = build_mock
mock.expects(:method1).returns(0)
mock.expects(:method1).returns(1)
assert_equal 1, mock.method1
end
def test_should_match_most_recent_call_to_stubs
mock = build_mock
mock.stubs(:method1).returns(0)
mock.stubs(:method1).returns(1)
assert_equal 1, mock.method1
end
def test_should_match_most_recent_call_to_stubs_or_expects
mock = build_mock
mock.stubs(:method1).returns(0)
mock.expects(:method1).returns(1)
assert_equal 1, mock.method1
end
def test_should_match_most_recent_call_to_expects_or_stubs
mock = build_mock
mock.expects(:method1).returns(0)
mock.stubs(:method1).returns(1)
assert_equal 1, mock.method1
end
def test_should_respond_to_expected_method
mock = build_mock
mock.expects(:method1)
assert_equal true, mock.respond_to?(:method1)
end
def test_should_respond_to_expected_method_as_string
mock = build_mock
mock.expects(:method1)
assert_equal true, mock.respond_to?('method1')
end
def test_should_not_respond_to_unexpected_method
mock = build_mock
assert_equal false, mock.respond_to?(:method1)
end
def test_should_respond_to_methods_which_the_responder_does_responds_to
instance = Class.new do
define_method(:respond_to?) { |symbol| true }
end.new
mock = build_mock
mock.responds_like(instance)
assert_equal true, mock.respond_to?(:invoked_method)
end
def test_should_not_respond_to_methods_which_the_responder_does_not_responds_to
instance = Class.new do
define_method(:respond_to?) { |symbol| false }
end.new
mock = build_mock
mock.responds_like(instance)
assert_equal false, mock.respond_to?(:invoked_method)
end
def test_should_respond_to_methods_which_the_responder_instance_does_responds_to
klass = Class.new do
define_method(:respond_to?) { |symbol| true }
end
mock = build_mock
mock.responds_like_instance_of(klass)
assert_equal true, mock.respond_to?(:invoked_method)
end
def test_should_not_respond_to_methods_which_the_responder_instance_does_not_responds_to
klass = Class.new do
define_method(:respond_to?) { |symbol| false }
end
mock = build_mock
mock.responds_like_instance_of(klass)
assert_equal false, mock.respond_to?(:invoked_method)
end
def test_respond_like_should_return_itself_to_allow_method_chaining
mock = build_mock
assert_same mock.responds_like(Object.new), mock
end
def test_respond_like_instance_of_should_return_itself_to_allow_method_chaining
mock = build_mock
assert_same mock.responds_like_instance_of(Object), mock
end
def test_should_not_raise_no_method_error_if_mock_is_not_restricted_to_respond_like_a_responder
mock = build_mock
mock.stubs(:invoked_method)
assert_nothing_raised(NoMethodError) { mock.invoked_method }
end
def test_should_not_raise_no_method_error_if_responder_does_respond_to_invoked_method
instance = Class.new do
define_method(:respond_to?) { |symbol| true }
end.new
mock = build_mock
mock.responds_like(instance)
mock.stubs(:invoked_method)
assert_nothing_raised(NoMethodError) { mock.invoked_method }
end
def test_should_raise_no_method_error_if_responder_does_not_respond_to_invoked_method
instance = Class.new do
define_method(:respond_to?) { |symbol| false }
define_method(:mocha_inspect) { 'mocha_inspect' }
end.new
mock = build_mock
mock.responds_like(instance)
mock.stubs(:invoked_method)
assert_raises(NoMethodError) { mock.invoked_method }
end
def test_should_raise_no_method_error_with_message_indicating_that_mock_is_constrained_to_respond_like_responder
instance = Class.new do
define_method(:respond_to?) { |symbol| false }
define_method(:mocha_inspect) { 'mocha_inspect' }
end.new
mock = build_mock
mock.responds_like(instance)
mock.stubs(:invoked_method)
begin
mock.invoked_method
rescue NoMethodError => e
assert_match(/which responds like mocha_inspect/, e.message)
end
end
def test_should_handle_respond_to_with_private_methods_param_without_error
mock = build_mock
assert_nothing_raised { mock.respond_to?(:object_id, false) }
end
def test_should_respond_to_any_method_if_stubbing_everything
mock = build_mock
mock.stub_everything
assert mock.respond_to?(:abc)
assert mock.respond_to?(:xyz)
end
def test_should_remove_expectation_for_unstubbed_method
mock = build_mock
mock.expects(:method1)
mock.unstub(:method1)
e = assert_raises(ExpectationErrorFactory.exception_class) { mock.method1 }
assert_match(/unexpected invocation/, e.message)
end
unless PRE_RUBY_V19
def test_expectation_is_defined_on_mock
mock = build_mock
mock.expects(:method1)
assert defined? mock.method1
end
end
private
def build_mock
Mock.new(nil)
end
end