Debian Codegraph @

closure-library/closure/goog/testing/mock_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.MockTest');
16 goog.setTestOnly('goog.testing.MockTest');
17
18 goog.require('goog.array');
19 goog.require('goog.testing');
20 goog.require('goog.testing.Mock');
21 goog.require('goog.testing.MockControl');
22 goog.require('goog.testing.MockExpectation');
23 goog.require('goog.testing.jsunit');
24
25 // The object that we will be mocking
26 var RealObject = function() {};
27
28 RealObject.prototype.a = function() {
29 fail('real object should never be called');
30 };
31
32 RealObject.prototype.b = function() {
33 fail('real object should never be called');
34 };
35
36 var matchers = goog.testing.mockmatchers;
37 var mock;
38
39 function setUp() {
40 var obj = new RealObject();
41 mock = new goog.testing.Mock(obj);
42 }
43
44 function testMockErrorMessage() {
45 var expectation = new goog.testing.MockExpectation('a');
46 assertEquals(0, expectation.getErrorMessageCount());
47 assertEquals('', expectation.getErrorMessage());
48
49 expectation.addErrorMessage('foo failed');
50 assertEquals(1, expectation.getErrorMessageCount());
51 assertEquals('foo failed', expectation.getErrorMessage());
52
53 expectation.addErrorMessage('bar failed');
54 assertEquals(2, expectation.getErrorMessageCount());
55 assertEquals('foo failed\nbar failed', expectation.getErrorMessage());
56 }
57
58 function testVerifyArgumentList() {
59 var expectation = new goog.testing.MockExpectation('a');
60 assertEquals('', expectation.getErrorMessage());
61
62 // test single string arg
63 expectation.argumentList = ['foo'];
64 assertTrue(mock.$verifyCall(expectation, 'a', ['foo']));
65
66 // single numeric arg
67 expectation.argumentList = [2];
68 assertTrue(mock.$verifyCall(expectation, 'a', [2]));
69
70 // single object arg (using standard === comparison)
71 var obj = {prop1: 'prop1', prop2: 2};
72 expectation.argumentList = [obj];
73 assertTrue(mock.$verifyCall(expectation, 'a', [obj]));
74
75 // make sure comparison succeeds if args are similar, but not ===
76 var obj2 = {prop1: 'prop1', prop2: 2};
77 expectation.argumentList = [obj];
78 assertTrue(mock.$verifyCall(expectation, 'a', [obj2]));
79 assertEquals('', expectation.getErrorMessage());
80
81 // multiple args
82 expectation.argumentList = ['foo', 2, obj, obj2];
83 assertTrue(mock.$verifyCall(expectation, 'a', ['foo', 2, obj, obj2]));
84
85 // test flexible arg matching.
86 expectation.argumentList = ['foo', matchers.isNumber];
87 assertTrue(mock.$verifyCall(expectation, 'a', ['foo', 1]));
88
89 expectation.argumentList = [new matchers.InstanceOf(RealObject)];
90 assertTrue(mock.$verifyCall(expectation, 'a', [new RealObject()]));
91 }
92
93 function testVerifyArgumentListForObjectMethods() {
94 var expectation = new goog.testing.MockExpectation('toString');
95 expectation.argumentList = [];
96 assertTrue(mock.$verifyCall(expectation, 'toString', []));
97 }
98
99 function testRegisterArgumentListVerifier() {
100 var expectationA = new goog.testing.MockExpectation('a');
101 var expectationB = new goog.testing.MockExpectation('b');
102
103 // Simple matcher that return true if all args are === equivalent.
104 mock.$registerArgumentListVerifier('a', function(expectedArgs, args) {
105 return goog.array.equals(
106 expectedArgs, args, function(a, b) { return (a === b); });
107 });
108
109 // test single string arg
110 expectationA.argumentList = ['foo'];
111 assertTrue(mock.$verifyCall(expectationA, 'a', ['foo']));
112
113 // single numeric arg
114 expectationA.argumentList = [2];
115 assertTrue(mock.$verifyCall(expectationA, 'a', [2]));
116
117 // single object arg (using standard === comparison)
118 var obj = {prop1: 'prop1', prop2: 2};
119 expectationA.argumentList = [obj];
120 expectationB.argumentList = [obj];
121 assertTrue(mock.$verifyCall(expectationA, 'a', [obj]));
122 assertTrue(mock.$verifyCall(expectationB, 'b', [obj]));
123
124 // if args are similar, but not ===, then comparison should succeed
125 // for method with registered object matcher, and fail for method without
126 var obj2 = {prop1: 'prop1', prop2: 2};
127 expectationA.argumentList = [obj];
128 expectationB.argumentList = [obj];
129 assertFalse(mock.$verifyCall(expectationA, 'a', [obj2]));
130 assertTrue(mock.$verifyCall(expectationB, 'b', [obj2]));
131
132
133 // multiple args, should fail for method with registered arg matcher,
134 // and succeed for method without.
135 expectationA.argumentList = ['foo', 2, obj, obj2];
136 expectationB.argumentList = ['foo', 2, obj, obj2];
137 assertFalse(mock.$verifyCall(expectationA, 'a', ['foo', 2, obj2, obj]));
138 assertTrue(mock.$verifyCall(expectationB, 'b', ['foo', 2, obj2, obj]));
139 }
140
141
142 function testCreateProxy() {
143 mock = new goog.testing.Mock(RealObject, false, true);
144 assertTrue(mock.$proxy instanceof RealObject);
145 assertThrows(function() { new goog.testing.Mock(RealObject, true, true); });
146 assertThrows(function() { new goog.testing.Mock(1, false, true); });
147 }
148
149
150 function testValidConstructorArgument() {
151 var someNamespace = {};
152 assertThrows(function() {
153 new goog.testing.Mock(someNamespace.RealObjectWithTypo);
154 });
155 }
156
157
158 function testArgumentsAsString() {
159 assertEquals('()', mock.$argumentsAsString([]));
160 assertEquals(
161 '(string, number, object, null)',
162 mock.$argumentsAsString(['red', 1, {}, null]));
163 }
164
165
166 function testThrowCallExceptionBadArgs() {
167 var msg;
168 mock.$throwException = function(m) { msg = m; };
169
170 mock.$throwCallException('fn1', ['b'], {
171 name: 'fn1',
172 argumentList: ['c'],
173 getErrorMessage: function() { return ''; }
174 });
175 assertContains(
176 'Bad arguments to fn1().\nActual: (string)\nExpected: (string)', msg);
177 }
178
179 function testThrowCallExceptionUnexpected() {
180 var msg;
181 mock.$throwException = function(m) { msg = m; };
182
183 mock.$throwCallException('fn1', ['b']);
184 assertEquals('Unexpected call to fn1(string).', msg);
185 }
186
187 function testThrowCallExceptionUnexpectedWithNext() {
188 var msg;
189 mock.$throwException = function(m) { msg = m; };
190
191 mock.$throwCallException('fn1', ['b'], {
192 name: 'fn2',
193 argumentList: [3],
194 getErrorMessage: function() { return ''; }
195 });
196 assertEquals(
197 'Unexpected call to fn1(string).\n' +
198 'Next expected call was to fn2(number)',
199 msg);
200 }
201
202 // This tests that base Object functions which are not enumerable in IE can
203 // be mocked correctly.
204 function testBindNonEnumerableFunctions() {
205 // Create Foo and override non enumerable functions.
206 var Foo = function() {};
207 Foo.prototype.constructor = function() {
208 fail('real object should never be called');
209 };
210 Foo.prototype.hasOwnProperty = function() {
211 fail('real object should never be called');
212 };
213 Foo.prototype.isPrototypeOf = function() {
214 fail('real object should never be called');
215 };
216 Foo.prototype.propertyIsEnumerable = function() {
217 fail('real object should never be called');
218 };
219 Foo.prototype.toLocaleString = function() {
220 fail('real object should never be called');
221 };
222 Foo.prototype.toString = function() {
223 fail('real object should never be called');
224 };
225 Foo.prototype.valueOf = function() {
226 fail('real object should never be called');
227 };
228
229 // Create Mock and set $returns for toString.
230 var mockControl = new goog.testing.MockControl();
231 var mock = mockControl.createLooseMock(Foo);
232 mock.constructor().$returns('constructor');
233 mock.hasOwnProperty().$returns('hasOwnProperty');
234 mock.isPrototypeOf().$returns('isPrototypeOf');
235 mock.propertyIsEnumerable().$returns('propertyIsEnumerable');
236 mock.toLocaleString().$returns('toLocaleString');
237 mock.toString().$returns('toString');
238 mock.valueOf().$returns('valueOf');
239
240 // Execute and assert that the Mock is working correctly.
241 mockControl.$replayAll();
242 assertEquals('constructor', mock.constructor());
243 assertEquals('hasOwnProperty', mock.hasOwnProperty());
244 assertEquals('isPrototypeOf', mock.isPrototypeOf());
245 assertEquals('propertyIsEnumerable', mock.propertyIsEnumerable());
246 assertEquals('toLocaleString', mock.toLocaleString());
247 assertEquals('toString', mock.toString());
248 assertEquals('valueOf', mock.valueOf());
249 mockControl.$verifyAll();
250 }
251
252 function testMockInheritedMethods() {
253 var SubType = function() {};
254 goog.inherits(SubType, RealObject);
255 SubType.prototype.c = function() {
256 fail('real object should never be called');
257 };
258
259 var mockControl = new goog.testing.MockControl();
260 var mock = mockControl.createLooseMock(SubType);
261 mock.a().$returns('a');
262 mock.b().$returns('b');
263 mock.c().$returns('c');
264
265 // Execute and assert that the Mock is working correctly.
266 mockControl.$replayAll();
267 assertEquals('a', mock.a());
268 assertEquals('b', mock.b());
269 assertEquals('c', mock.c());
270 mockControl.$verifyAll();
271 }
272
273 function testMockStaticMethods() {
274 var SomeType = function() {};
275 SomeType.staticMethod = function() {
276 fail('real object should never be called');
277 };
278
279 var mockControl = new goog.testing.MockControl();
280 var mock = mockControl.createLooseMock(
281 SomeType, false /* opt_ignoreUnexpectedCalls */,
282 true /* opt_mockStaticMethods */);
283 mock.staticMethod().$returns('staticMethod');
284
285 // Execute and assert that the Mock is working correctly.
286 mockControl.$replayAll();
287 assertEquals('staticMethod', mock.staticMethod());
288 mockControl.$verifyAll();
289 }
290
291 function testMockEs6ClassMethods() {
292 // Create an ES6 class via eval so we can bail out if it's a syntax error in
293 // browsers that don't support ES6 classes.
294 try {
295 eval(
296 'var Foo = class {' +
297 ' a() {' +
298 ' fail(\'real object should never be called\');' +
299 ' }' +
300 '}');
301 } catch (e) {
302 if (e instanceof SyntaxError) {
303 return;
304 }
305 }
306
307 var mockControl = new goog.testing.MockControl();
308 var mock = mockControl.createLooseMock(Foo);
309 mock.a().$returns('a');
310
311 // Execute and assert that the Mock is working correctly.
312 mockControl.$replayAll();
313 assertEquals('a', mock.a());
314 mockControl.$verifyAll();
315 }
316
317 function testMockEs6ClassStaticMethods() {
318 // Create an ES6 class via eval so we can bail out if it's a syntax error in
319 // browsers that don't support ES6 classes.
320 try {
321 eval(
322 'var Foo = class {' +
323 ' static a() {' +
324 ' fail(\'real object should never be called\');' +
325 ' }' +
326 ' static apply() {' +
327 ' fail(\'real object should never be called\');' +
328 ' }' +
329 '}');
330 } catch (e) {
331 if (e instanceof SyntaxError) {
332 return;
333 }
334 }
335
336 var mockControl = new goog.testing.MockControl();
337 var mock = mockControl.createLooseMock(
338 Foo, false /* opt_ignoreUnexpectedCalls */,
339 true /* opt_mockStaticMethods */);
340 mock.a().$returns('a');
341 mock.apply().$returns('apply');
342
343 // Execute and assert that the Mock is working correctly.
344 mockControl.$replayAll();
345 assertEquals('a', mock.a());
346 assertEquals('apply', mock.apply());
347 mockControl.$verifyAll();
348 }