Debian Codegraph @

closure-library/closure/goog/testing/strictmock_test.js

raw
1 // Copyright 2008 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.testing.StrictMockTest');
16 goog.setTestOnly('goog.testing.StrictMockTest');
17
18 goog.require('goog.testing.StrictMock');
19 goog.require('goog.testing.jsunit');
20
21 // The object that we will be mocking
22 var RealObject = function() {};
23
24 RealObject.prototype.a = function() {
25 fail('real object should never be called');
26 };
27
28 RealObject.prototype.b = function() {
29 fail('real object should never be called');
30 };
31
32 RealObject.prototype.c = function() {
33 fail('real object should never be called');
34 };
35
36 var mock;
37
38 function setUp() {
39 var obj = new RealObject();
40 mock = new goog.testing.StrictMock(obj);
41 }
42
43
44 function testMockFunction() {
45 var mock = new goog.testing.StrictMock(RealObject);
46 mock.a();
47 mock.b();
48 mock.c();
49 mock.$replay();
50 mock.a();
51 mock.b();
52 mock.c();
53 mock.$verify();
54
55 mock.$reset();
56
57 assertThrows(function() { mock.x() });
58 }
59
60
61 function testSimpleExpectations() {
62 mock.a();
63 mock.$replay();
64 mock.a();
65 mock.$verify();
66
67 mock.$reset();
68
69 mock.a();
70 mock.b();
71 mock.a();
72 mock.a();
73 mock.$replay();
74 mock.a();
75 mock.b();
76 mock.a();
77 mock.a();
78 mock.$verify();
79 }
80
81
82 function testFailToSetExpectation() {
83 mock.$replay();
84 assertThrowsJsUnitException(goog.bind(mock.a, mock));
85
86 mock.$reset();
87
88 mock.$replay();
89 assertThrowsJsUnitException(goog.bind(mock.b, mock));
90 }
91
92
93 function testUnexpectedCall() {
94 mock.a();
95 mock.$replay();
96 mock.a();
97 assertThrowsJsUnitException(goog.bind(mock.a, mock));
98
99 mock.$reset();
100
101 mock.a();
102 mock.$replay();
103 assertThrowsJsUnitException(goog.bind(mock.b, mock));
104 }
105
106
107 function testNotEnoughCalls() {
108 mock.a();
109 mock.$replay();
110 assertThrowsJsUnitException(goog.bind(mock.$verify, mock));
111
112 mock.$reset();
113
114 mock.a();
115 mock.b();
116 mock.$replay();
117 mock.a();
118 assertThrowsJsUnitException(goog.bind(mock.$verify, mock));
119 }
120
121
122 function testOutOfOrderCalls() {
123 mock.a();
124 mock.b();
125 mock.$replay();
126 assertThrowsJsUnitException(goog.bind(mock.b, mock));
127 }
128
129
130 function testVerify() {
131 mock.a();
132 mock.$replay();
133 mock.a();
134 mock.$verify();
135
136 mock.$reset();
137
138 mock.a();
139 mock.$replay();
140 assertThrowsJsUnitException(goog.bind(mock.$verify, mock));
141 }
142
143
144 function testArgumentMatching() {
145 mock.a('foo');
146 mock.b('bar');
147 mock.$replay();
148 mock.a('foo');
149 assertThrowsJsUnitException(function() { mock.b('foo') });
150
151 mock.$reset();
152 mock.a('foo');
153 mock.a('bar');
154 mock.$replay();
155 mock.a('foo');
156 mock.a('bar');
157 mock.$verify();
158
159 mock.$reset();
160 mock.a('foo');
161 mock.a('bar');
162 mock.$replay();
163 assertThrowsJsUnitException(function() { mock.a('bar') });
164 }
165
166
167 function testReturnValue() {
168 mock.a().$returns(5);
169 mock.$replay();
170
171 assertEquals('Mock should return the right value', 5, mock.a());
172
173 mock.$verify();
174 }
175
176 function testMultipleReturnValues() {
177 mock.a().$returns(3);
178 mock.a().$returns(2);
179 mock.a().$returns(1);
180
181 mock.$replay();
182
183 assertArrayEquals(
184 'Mock should return the right value sequence', [3, 2, 1],
185 [mock.a(), mock.a(), mock.a()]);
186
187 mock.$verify();
188 }
189
190
191 function testAtMostOnce() {
192 // Zero times SUCCESS.
193 mock.a().$atMostOnce();
194 mock.$replay();
195 mock.$verify();
196
197 mock.$reset();
198
199 // One time SUCCESS.
200 mock.a().$atMostOnce();
201 mock.$replay();
202 mock.a();
203 mock.$verify();
204
205 mock.$reset();
206
207 // Many times FAIL.
208 mock.a().$atMostOnce();
209 mock.$replay();
210 mock.a();
211 assertThrowsJsUnitException(goog.bind(mock.a, mock));
212
213 mock.$reset();
214
215 // atMostOnce only lasts until a new method is called.
216 mock.a().$atMostOnce();
217 mock.b();
218 mock.a();
219 mock.$replay();
220 mock.b();
221 assertThrowsJsUnitException(goog.bind(mock.$verify, mock));
222 }
223
224
225 function testAtLeastOnce() {
226 // atLeastOnce does not mean zero times
227 mock.a().$atLeastOnce();
228 mock.$replay();
229 assertThrowsJsUnitException(goog.bind(mock.$verify, mock));
230
231 mock.$reset();
232
233 // atLeastOnce does mean three times
234 mock.a().$atLeastOnce();
235 mock.$replay();
236 mock.a();
237 mock.a();
238 mock.a();
239 mock.$verify();
240
241 mock.$reset();
242
243 // atLeastOnce only lasts until a new method is called
244 mock.a().$atLeastOnce();
245 mock.b();
246 mock.a();
247 mock.$replay();
248 mock.a();
249 mock.a();
250 mock.b();
251 mock.a();
252 assertThrowsJsUnitException(goog.bind(mock.a, mock));
253 }
254
255
256 function testAtLeastOnceWithArgs() {
257 mock.a('asdf').$atLeastOnce();
258 mock.a('qwert');
259 mock.$replay();
260 mock.a('asdf');
261 mock.a('asdf');
262 mock.a('qwert');
263 mock.$verify();
264
265 mock.$reset();
266
267 mock.a('asdf').$atLeastOnce();
268 mock.a('qwert');
269 mock.$replay();
270 mock.a('asdf');
271 mock.a('asdf');
272 assertThrowsJsUnitException(function() { mock.a('zxcv') });
273 assertThrowsJsUnitException(goog.bind(mock.$verify, mock));
274 }
275
276
277 function testAnyTimes() {
278 mock.a().$anyTimes();
279 mock.$replay();
280 mock.$verify();
281
282 mock.$reset();
283
284 mock.a().$anyTimes();
285 mock.$replay();
286 mock.a();
287 mock.a();
288 mock.a();
289 mock.a();
290 mock.a();
291 mock.$verify();
292 }
293
294
295 function testAnyTimesWithArguments() {
296 mock.a('foo').$anyTimes();
297 mock.$replay();
298 mock.$verify();
299
300 mock.$reset();
301
302 mock.a('foo').$anyTimes();
303 mock.a('bar').$anyTimes();
304 mock.$replay();
305 mock.a('foo');
306 mock.a('foo');
307 mock.a('foo');
308 mock.a('bar');
309 mock.a('bar');
310 mock.$verify();
311 }
312
313
314 function testZeroTimes() {
315 mock.a().$times(0);
316 mock.$replay();
317 mock.$verify();
318
319 mock.$reset();
320
321 mock.a().$times(0);
322 mock.$replay();
323 assertThrowsJsUnitException(function() { mock.a() });
324 }
325
326
327 function testZeroTimesWithArguments() {
328 mock.a('foo').$times(0);
329 mock.$replay();
330 mock.$verify();
331
332 mock.$reset();
333
334 mock.a('foo').$times(0);
335 mock.$replay();
336 assertThrowsJsUnitException(function() { mock.a('foo') });
337 }
338
339
340 function testTooManyCalls() {
341 mock.a().$times(2);
342 mock.$replay();
343 mock.a();
344 mock.a();
345 assertThrowsJsUnitException(function() { mock.a() });
346 }
347
348
349 function testTooManyCallsWithArguments() {
350 mock.a('foo').$times(2);
351 mock.$replay();
352 mock.a('foo');
353 mock.a('foo');
354 assertThrowsJsUnitException(function() { mock.a('foo') });
355 }
356
357
358 function testMultipleSkippedAnyTimes() {
359 mock.a().$anyTimes();
360 mock.b().$anyTimes();
361 mock.c().$anyTimes();
362 mock.$replay();
363 mock.c();
364 mock.$verify();
365 }
366
367
368 function testMultipleSkippedAnyTimesWithArguments() {
369 mock.a('foo').$anyTimes();
370 mock.a('bar').$anyTimes();
371 mock.a('baz').$anyTimes();
372 mock.$replay();
373 mock.a('baz');
374 mock.$verify();
375 }
376
377
378 function testVerifyThrows() {
379 mock.a(1);
380 mock.$replay();
381 mock.a(1);
382 try {
383 mock.a(2);
384 fail('bad mock, should fail');
385 } catch (ex) {
386 // this could be an event handler, for example
387 }
388 assertThrowsJsUnitException(goog.bind(mock.$verify, mock));
389 }
390
391
392 function testThrows() {
393 mock.a().$throws('exception!');
394 mock.$replay();
395 assertThrows(goog.bind(mock.a, mock));
396 mock.$verify();
397 }
398
399
400 function testDoes() {
401 mock.a(1, 2).$does(function(a, b) { return a + b; });
402 mock.$replay();
403 assertEquals('Mock should call the function', 3, mock.a(1, 2));
404 mock.$verify();
405 }
406
407 function testErrorMessageForBadArgs() {
408 mock.a();
409 mock.$anyTimes();
410
411 mock.$replay();
412
413 var message;
414 try {
415 mock.a('a');
416 } catch (e) {
417 message = e.message;
418 }
419
420 assertTrue('No exception thrown on verify', goog.isDef(message));
421 assertContains('Bad arguments to a()', message);
422 }