Debian Codegraph @

closure-library/closure/goog/testing/loosemock_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.LooseMockTest');
16 goog.setTestOnly('goog.testing.LooseMockTest');
17
18 goog.require('goog.testing.LooseMock');
19 goog.require('goog.testing.PropertyReplacer');
20 goog.require('goog.testing.jsunit');
21 goog.require('goog.testing.mockmatchers');
22
23 // The object that we will be mocking
24 var RealObject = function() {};
25
26 RealObject.prototype.a = function() {
27 fail('real object should never be called');
28 };
29
30 RealObject.prototype.b = function() {
31 fail('real object should never be called');
32 };
33
34 var mock;
35
36 var stubs;
37
38 function setUp() {
39 var obj = new RealObject();
40 mock = new goog.testing.LooseMock(obj);
41 stubs = new goog.testing.PropertyReplacer();
42 }
43
44 function tearDown() {
45 stubs.reset();
46 }
47
48 /*
49 * Calling this method evades the logTestFailure in
50 * goog.testing.Mock.prototype.$recordAndThrow, so it doesn't look like the
51 * test failed.
52 */
53 function silenceFailureLogging() {
54 if (goog.global['G_testRunner']) {
55 stubs.set(goog.global['G_testRunner'], 'logTestFailure', goog.nullFunction);
56 }
57 }
58
59 function unsilenceFailureLogging() {
60 stubs.reset();
61 }
62
63
64 /**
65 * Version of assertThrows that doesn't log the exception generated by the
66 * mocks under test.
67 * TODO: would be nice to check that a particular substring is in the thrown
68 * message so we know it's not something dumb like a syntax error
69 */
70 function assertThrowsQuiet(var_args) {
71 silenceFailureLogging();
72 assertThrowsJsUnitException.apply(null, arguments);
73 unsilenceFailureLogging();
74 }
75
76 // Most of the basic functionality is tested in strictmock_test; these tests
77 // cover the cases where loose mocks are different from strict mocks
78 function testSimpleExpectations() {
79 mock.a(5);
80 mock.b();
81 mock.$replay();
82 mock.a(5);
83 mock.b();
84 mock.$verify();
85
86 mock.$reset();
87
88 mock.a();
89 mock.b();
90 mock.$replay();
91 mock.b();
92 mock.a();
93 mock.$verify();
94
95 mock.$reset();
96
97 mock.a(5).$times(2);
98 mock.a(5);
99 mock.a(2);
100 mock.$replay();
101 mock.a(5);
102 mock.a(5);
103 mock.a(5);
104 mock.a(2);
105 mock.$verify();
106 }
107
108
109 function testMultipleExpectations() {
110 mock.a().$returns(1);
111 mock.a().$returns(2);
112 mock.$replay();
113 assertEquals(1, mock.a());
114 assertEquals(2, mock.a());
115 mock.$verify();
116 }
117
118
119 function testMultipleExpectationArgs() {
120 mock.a('asdf').$anyTimes();
121 mock.a('qwer').$anyTimes();
122 mock.b().$times(3);
123 mock.$replay();
124 mock.a('asdf');
125 mock.b();
126 mock.a('asdf');
127 mock.a('qwer');
128 mock.b();
129 mock.a('qwer');
130 mock.b();
131 mock.$verify();
132
133 mock.$reset();
134
135 mock.a('asdf').$anyTimes();
136 mock.a('qwer').$anyTimes();
137 mock.$replay();
138 mock.a('asdf');
139 mock.a('qwer');
140 goog.bind(mock.a, mock, 'asdf');
141 goog.bind(mock.$verify, mock);
142 }
143
144 function testSameMethodOutOfOrder() {
145 mock.a('foo').$returns(1);
146 mock.a('bar').$returns(2);
147 mock.$replay();
148 assertEquals(2, mock.a('bar'));
149 assertEquals(1, mock.a('foo'));
150 }
151
152 function testSameMethodDifferentReturnValues() {
153 mock.a('foo').$returns(1).$times(2);
154 mock.a('foo').$returns(3);
155 mock.a('bar').$returns(2);
156 mock.$replay();
157 assertEquals(1, mock.a('foo'));
158 assertEquals(2, mock.a('bar'));
159 assertEquals(1, mock.a('foo'));
160 assertEquals(3, mock.a('foo'));
161 assertThrowsQuiet(function() {
162 mock.a('foo');
163 mock.$verify();
164 });
165 }
166
167 function testSameMethodBrokenExpectations() {
168 // This is a weird corner case.
169 // No way to ever make this verify no matter what you call after replaying,
170 // because the second expectation of mock.a('foo') will be masked by
171 // the first expectation that can be called any number of times, and so we
172 // can never satisfy that second expectation.
173 mock.a('foo').$returns(1).$anyTimes();
174 mock.a('bar').$returns(2);
175 mock.a('foo').$returns(3);
176
177 // LooseMock can detect this case and fail on $replay.
178 assertThrowsQuiet(goog.bind(mock.$replay, mock));
179 mock.$reset();
180
181 // This is a variant of the corner case above, but it's harder to determine
182 // that the expectation to mock.a('bar') can never be satisfied. So we don't
183 // fail on $replay, but we do fail on $verify.
184 mock.a(goog.testing.mockmatchers.isString).$returns(1).$anyTimes();
185 mock.a('bar').$returns(2);
186 mock.$replay();
187
188 assertEquals(1, mock.a('foo'));
189 assertEquals(1, mock.a('bar'));
190 assertThrowsQuiet(goog.bind(mock.$verify, mock));
191 }
192
193 function testSameMethodMultipleAnyTimes() {
194 mock.a('foo').$returns(1).$anyTimes();
195 mock.a('foo').$returns(2).$anyTimes();
196 mock.$replay();
197 assertEquals(1, mock.a('foo'));
198 assertEquals(1, mock.a('foo'));
199 assertEquals(1, mock.a('foo'));
200 // Note we'll never return 2 but that's ok.
201 mock.$verify();
202 }
203
204 function testFailingFast() {
205 mock.a().$anyTimes();
206 mock.$replay();
207 mock.a();
208 mock.a();
209 assertThrowsQuiet(goog.bind(mock.b, mock));
210 mock.$reset();
211
212 // too many
213 mock.a();
214 mock.b();
215 mock.$replay();
216 mock.a();
217 mock.b();
218
219 var message;
220 silenceFailureLogging();
221 try {
222 mock.a();
223 } catch (e) {
224 message = e.message;
225 }
226 unsilenceFailureLogging();
227
228 assertTrue('No exception thrown on unexpected call', goog.isDef(message));
229 assertContains('Too many calls to a', message);
230 }
231
232 function testTimes() {
233 mock.a().$times(3);
234 mock.b().$times(2);
235 mock.$replay();
236 mock.a();
237 mock.b();
238 mock.b();
239 mock.a();
240 mock.a();
241 mock.$verify();
242 }
243
244
245 function testFailingSlow() {
246 // not enough
247 mock.a().$times(3);
248 mock.$replay();
249 mock.a();
250 mock.a();
251 assertThrowsQuiet(goog.bind(mock.$verify, mock));
252
253 mock.$reset();
254
255 // not enough, interleaved order
256 mock.a().$times(3);
257 mock.b().$times(3);
258 mock.$replay();
259 mock.a();
260 mock.b();
261 mock.a();
262 mock.b();
263 assertThrowsQuiet(goog.bind(mock.$verify, mock));
264
265 mock.$reset();
266 // bad args
267 mock.a('asdf').$anyTimes();
268 mock.$replay();
269 mock.a('asdf');
270 assertThrowsQuiet(goog.bind(mock.a, mock, 'qwert'));
271 assertThrowsQuiet(goog.bind(mock.$verify, mock));
272 }
273
274
275 function testArgsAndReturns() {
276 mock.a('asdf').$atLeastOnce().$returns(5);
277 mock.b('qwer').$times(2).$returns(3);
278 mock.$replay();
279 assertEquals(5, mock.a('asdf'));
280 assertEquals(3, mock.b('qwer'));
281 assertEquals(5, mock.a('asdf'));
282 assertEquals(5, mock.a('asdf'));
283 assertEquals(3, mock.b('qwer'));
284 mock.$verify();
285 }
286
287
288 function testThrows() {
289 mock.a().$throws('exception!');
290 mock.$replay();
291 assertThrows(goog.bind(mock.a, mock));
292 mock.$verify();
293 }
294
295
296 function testDoes() {
297 mock.a(1, 2).$does(function(a, b) { return a + b; });
298 mock.$replay();
299 assertEquals('Mock should call the function', 3, mock.a(1, 2));
300 mock.$verify();
301 }
302
303 function testIgnoresExtraCalls() {
304 mock = new goog.testing.LooseMock(RealObject, true);
305 mock.a();
306 mock.$replay();
307 mock.a();
308 mock.b(); // doesn't throw
309 mock.$verify();
310 }
311
312 function testSkipAnyTimes() {
313 mock = new goog.testing.LooseMock(RealObject);
314 mock.a(1).$anyTimes();
315 mock.a(2).$anyTimes();
316 mock.a(3).$anyTimes();
317 mock.$replay();
318 mock.a(1);
319 mock.a(3);
320 mock.$verify();
321 }
322
323 function testErrorMessageForBadArgs() {
324 mock.a();
325 mock.$anyTimes();
326
327 mock.$replay();
328
329 var message;
330 silenceFailureLogging();
331 try {
332 mock.a('a');
333 } catch (e) {
334 message = e.message;
335 }
336 unsilenceFailureLogging();
337
338 assertTrue('No exception thrown on verify', goog.isDef(message));
339 assertContains('Bad arguments to a()', message);
340 }