mock_test.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. // Copyright 2012 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. goog.provide('goog.labs.mockTest');
  15. goog.setTestOnly('goog.labs.mockTest');
  16. goog.require('goog.array');
  17. goog.require('goog.labs.mock');
  18. goog.require('goog.labs.mock.VerificationError');
  19. /** @suppress {extraRequire} */
  20. goog.require('goog.labs.testing.AnythingMatcher');
  21. /** @suppress {extraRequire} */
  22. goog.require('goog.labs.testing.GreaterThanMatcher');
  23. goog.require('goog.string');
  24. goog.require('goog.testing.jsunit');
  25. var ParentClass = function() {};
  26. ParentClass.prototype.method1 = function() {};
  27. ParentClass.prototype.x = 1;
  28. ParentClass.prototype.val = 0;
  29. ParentClass.prototype.incrementVal = function() {
  30. this.val++;
  31. };
  32. var ChildClass = function() {};
  33. goog.inherits(ChildClass, ParentClass);
  34. ChildClass.prototype.method2 = function() {};
  35. ChildClass.prototype.y = 2;
  36. function testParentClass() {
  37. var parentMock = goog.labs.mock.mock(ParentClass);
  38. assertNotUndefined(parentMock.method1);
  39. assertUndefined(parentMock.method1());
  40. assertUndefined(parentMock.method2);
  41. assertNotUndefined(parentMock.x);
  42. assertUndefined(parentMock.y);
  43. assertTrue(
  44. 'Mock should be an instance of the mocked class.',
  45. parentMock instanceof ParentClass);
  46. }
  47. function testChildClass() {
  48. var childMock = goog.labs.mock.mock(ChildClass);
  49. assertNotUndefined(childMock.method1);
  50. assertUndefined(childMock.method1());
  51. assertNotUndefined(childMock.method2);
  52. assertUndefined(childMock.method2());
  53. assertNotUndefined(childMock.x);
  54. assertNotUndefined(childMock.y);
  55. assertTrue(
  56. 'Mock should be an instance of the mocked class.',
  57. childMock instanceof ChildClass);
  58. }
  59. function testParentClassInstance() {
  60. var parentMock = goog.labs.mock.mock(new ParentClass());
  61. assertNotUndefined(parentMock.method1);
  62. assertUndefined(parentMock.method1());
  63. assertUndefined(parentMock.method2);
  64. assertNotUndefined(parentMock.x);
  65. assertUndefined(parentMock.y);
  66. assertTrue(
  67. 'Mock should be an instance of the mocked class.',
  68. parentMock instanceof ParentClass);
  69. }
  70. function testChildClassInstance() {
  71. var childMock = goog.labs.mock.mock(new ChildClass());
  72. assertNotUndefined(childMock.method1);
  73. assertUndefined(childMock.method1());
  74. assertNotUndefined(childMock.method2);
  75. assertUndefined(childMock.method2());
  76. assertNotUndefined(childMock.x);
  77. assertNotUndefined(childMock.y);
  78. assertTrue(
  79. 'Mock should be an instance of the mocked class.',
  80. childMock instanceof ParentClass);
  81. }
  82. function testNonEnumerableProperties() {
  83. var mockObject = goog.labs.mock.mock({});
  84. assertNotUndefined(mockObject.toString);
  85. goog.labs.mock.when(mockObject).toString().then(function() {
  86. return 'toString';
  87. });
  88. assertEquals('toString', mockObject.toString());
  89. }
  90. function testBasicStubbing() {
  91. var obj = {
  92. method1: function(i) { return 2 * i; },
  93. method2: function(i, str) { return str; },
  94. method3: function(x) { return x; }
  95. };
  96. var mockObj = goog.labs.mock.mock(obj);
  97. goog.labs.mock.when(mockObj).method1(2).then(function(i) { return i; });
  98. assertEquals(4, obj.method1(2));
  99. assertEquals(2, mockObj.method1(2));
  100. assertUndefined(mockObj.method1(4));
  101. goog.labs.mock.when(mockObj).method2(1, 'hi').then(function(i) {
  102. return 'oh';
  103. });
  104. assertEquals('hi', obj.method2(1, 'hi'));
  105. assertEquals('oh', mockObj.method2(1, 'hi'));
  106. assertUndefined(mockObj.method2(3, 'foo'));
  107. goog.labs.mock.when(mockObj).method3(4).thenReturn(10);
  108. assertEquals(4, obj.method3(4));
  109. assertEquals(10, mockObj.method3(4));
  110. goog.labs.mock.verify(mockObj).method3(4);
  111. assertUndefined(mockObj.method3(5));
  112. }
  113. function testMockFunctions() {
  114. function x(i) { return i; }
  115. var mockedFunc = goog.labs.mock.mockFunction(x);
  116. goog.labs.mock.when(mockedFunc)(100).thenReturn(10);
  117. goog.labs.mock.when(mockedFunc)(50).thenReturn(25);
  118. assertEquals(100, x(100));
  119. assertEquals(10, mockedFunc(100));
  120. assertEquals(25, mockedFunc(50));
  121. }
  122. function testMockFunctionsWithNullableParameters() {
  123. var func = function(nullableObject) { return 0; };
  124. var mockedFunc = goog.labs.mock.mockFunction(func);
  125. goog.labs.mock.when(mockedFunc)(null).thenReturn(-1);
  126. assertEquals(0, func(null));
  127. assertEquals(-1, mockedFunc(null));
  128. }
  129. function testMockConstructor() {
  130. var Ctor = function() { this.isMock = false; };
  131. var mockInstance = {isMock: true};
  132. var MockCtor = goog.labs.mock.mockConstructor(Ctor);
  133. goog.labs.mock.when(MockCtor)().thenReturn(mockInstance);
  134. assertEquals(mockInstance, new MockCtor());
  135. }
  136. function testMockConstructorCopiesProperties() {
  137. var Ctor = function() {};
  138. Ctor.myParam = true;
  139. var MockCtor = goog.labs.mock.mockConstructor(Ctor);
  140. assertTrue(MockCtor.myParam);
  141. }
  142. function testStubbingConsecutiveCalls() {
  143. var obj = {method: function(i) { return i * 42; }};
  144. var mockObj = goog.labs.mock.mock(obj);
  145. goog.labs.mock.when(mockObj).method(1).thenReturn(3).thenReturn(4);
  146. assertEquals(42, obj.method(1));
  147. assertEquals(3, mockObj.method(1));
  148. assertEquals(4, mockObj.method(1));
  149. assertEquals(4, mockObj.method(1));
  150. var x = function(i) { return i; };
  151. var mockedFunc = goog.labs.mock.mockFunction(x);
  152. goog.labs.mock.when(mockedFunc)(100).thenReturn(10).thenReturn(25);
  153. assertEquals(100, x(100));
  154. assertEquals(10, mockedFunc(100));
  155. assertEquals(25, mockedFunc(100));
  156. assertEquals(25, mockedFunc(100));
  157. }
  158. function testStubbingMultipleObjectStubsNonConflictingArgsAllShouldWork() {
  159. var obj = {method: function(i) { return i * 2; }};
  160. var mockObj = goog.labs.mock.mock(obj);
  161. goog.labs.mock.when(mockObj).method(2).thenReturn(100);
  162. goog.labs.mock.when(mockObj).method(5).thenReturn(45);
  163. assertEquals(100, mockObj.method(2));
  164. assertEquals(45, mockObj.method(5));
  165. }
  166. function
  167. testStubbingMultipleObjectStubsConflictingArgsMostRecentShouldPrevail() {
  168. var obj = {method: function(i) { return i * 2; }};
  169. var mockObj = goog.labs.mock.mock(obj);
  170. goog.labs.mock.when(mockObj).method(2).thenReturn(100);
  171. goog.labs.mock.when(mockObj).method(2).thenReturn(45);
  172. assertEquals(45, mockObj.method(2));
  173. }
  174. function testStubbingMultipleFunctionStubsNonConflictingArgsAllShouldWork() {
  175. var x = function(i) { return i; };
  176. var mockedFunc = goog.labs.mock.mockFunction(x);
  177. goog.labs.mock.when(mockedFunc)(100).thenReturn(10);
  178. goog.labs.mock.when(mockedFunc)(10).thenReturn(132);
  179. assertEquals(10, mockedFunc(100));
  180. assertEquals(132, mockedFunc(10));
  181. }
  182. function
  183. testStubbingMultipleFunctionStubsConflictingArgsMostRecentShouldPrevail() {
  184. var x = function(i) { return i; };
  185. var mockedFunc = goog.labs.mock.mockFunction(x);
  186. goog.labs.mock.when(mockedFunc)(100).thenReturn(10);
  187. goog.labs.mock.when(mockedFunc)(100).thenReturn(132);
  188. assertEquals(132, mockedFunc(100));
  189. }
  190. function testSpying() {
  191. var obj = {
  192. method1: function(i) { return 2 * i; },
  193. method2: function(i) { return 5 * i; }
  194. };
  195. var spyObj = goog.labs.mock.spy(obj);
  196. goog.labs.mock.when(spyObj).method1(2).thenReturn(5);
  197. assertEquals(2, obj.method1(1));
  198. assertEquals(5, spyObj.method1(2));
  199. goog.labs.mock.verify(spyObj).method1(2);
  200. assertEquals(2, spyObj.method1(1));
  201. goog.labs.mock.verify(spyObj).method1(1);
  202. assertEquals(20, spyObj.method2(4));
  203. goog.labs.mock.verify(spyObj).method2(4);
  204. }
  205. function testSpyParentClassInstance() {
  206. var parent = new ParentClass();
  207. var parentMock = goog.labs.mock.spy(parent);
  208. assertNotUndefined(parentMock.method1);
  209. assertUndefined(parentMock.method1());
  210. assertUndefined(parentMock.method2);
  211. assertNotUndefined(parentMock.x);
  212. assertUndefined(parentMock.y);
  213. assertTrue(
  214. 'Mock should be an instance of the mocked class.',
  215. parentMock instanceof ParentClass);
  216. var incrementedOrigVal = parent.val + 1;
  217. parentMock.incrementVal();
  218. assertEquals(
  219. 'Changes in the spied object should reflect in the spy.',
  220. incrementedOrigVal, parentMock.val);
  221. }
  222. function testSpyChildClassInstance() {
  223. var child = new ChildClass();
  224. var childMock = goog.labs.mock.spy(child);
  225. assertNotUndefined(childMock.method1);
  226. assertUndefined(childMock.method1());
  227. assertNotUndefined(childMock.method2);
  228. assertUndefined(childMock.method2());
  229. assertNotUndefined(childMock.x);
  230. assertNotUndefined(childMock.y);
  231. assertTrue(
  232. 'Mock should be an instance of the mocked class.',
  233. childMock instanceof ParentClass);
  234. var incrementedOrigVal = child.val + 1;
  235. childMock.incrementVal();
  236. assertEquals(
  237. 'Changes in the spied object should reflect in the spy.',
  238. incrementedOrigVal, childMock.val);
  239. }
  240. function testVerifyForObjects() {
  241. var obj = {
  242. method1: function(i) { return 2 * i; },
  243. method2: function(i) { return 5 * i; }
  244. };
  245. var mockObj = goog.labs.mock.mock(obj);
  246. goog.labs.mock.when(mockObj).method1(2).thenReturn(5);
  247. assertEquals(5, mockObj.method1(2));
  248. goog.labs.mock.verify(mockObj).method1(2);
  249. var e = assertThrows(goog.partial(goog.labs.mock.verify(mockObj).method2, 2));
  250. assertTrue(e instanceof goog.labs.mock.VerificationError);
  251. }
  252. function testVerifyForFunctions() {
  253. var func = function(i) { return i; };
  254. var mockFunc = goog.labs.mock.mockFunction(func);
  255. goog.labs.mock.when(mockFunc)(2).thenReturn(55);
  256. assertEquals(55, mockFunc(2));
  257. goog.labs.mock.verify(mockFunc)(2);
  258. goog.labs.mock.verify(mockFunc)(lessThan(3));
  259. var e = assertThrows(goog.partial(goog.labs.mock.verify(mockFunc), 3));
  260. assertTrue(e instanceof goog.labs.mock.VerificationError);
  261. }
  262. function testVerifyForFunctionsWithNullableParameters() {
  263. var func = function(nullableObject) {};
  264. var mockFuncCalled = goog.labs.mock.mockFunction(func);
  265. var mockFuncNotCalled = goog.labs.mock.mockFunction(func);
  266. mockFuncCalled(null);
  267. goog.labs.mock.verify(mockFuncCalled)(null);
  268. var e = assertThrows(
  269. goog.partial(goog.labs.mock.verify(mockFuncNotCalled), null));
  270. assertTrue(e instanceof goog.labs.mock.VerificationError);
  271. }
  272. function testVerifyPassesWhenVerificationModeReturnsTrue() {
  273. var trueMode = {
  274. verify: function(number) { return true; },
  275. describe: function() { return ''; }
  276. };
  277. var mockObj = goog.labs.mock.mock({doThing: function() {}});
  278. goog.labs.mock.verify(mockObj, trueMode).doThing();
  279. }
  280. function testVerifyFailsWhenVerificationModeReturnsFalse() {
  281. var falseMode = {
  282. verify: function(number) { return false; },
  283. describe: function() { return ''; }
  284. };
  285. var mockObj = goog.labs.mock.mock({doThing: function() {}});
  286. assertThrows(goog.labs.mock.verify(mockObj, falseMode).doThing);
  287. }
  288. function testVerificationErrorMessagePutsVerificationModeInRightPlace() {
  289. var modeDescription = 'test';
  290. var mode = {
  291. verify: function(number) { return false; },
  292. describe: function() { return modeDescription; }
  293. };
  294. var mockObj = goog.labs.mock.mock({methodName: function() {}});
  295. mockObj.methodName(2);
  296. e = assertThrows(goog.labs.mock.verify(mockObj, mode).methodName);
  297. // The mode description should be between the expected method
  298. // invocation and a newline.
  299. assertTrue(goog.string.contains(
  300. e.message, 'methodName() ' + modeDescription + '\n'));
  301. }
  302. /**
  303. * When a function invocation verification fails, it should show the failed
  304. * expectation call, as well as the recorded calls to the same method.
  305. */
  306. function testVerificationErrorMessages() {
  307. var mock = goog.labs.mock.mock({method: function(i) { return i; }});
  308. // Failure when there are no recorded calls.
  309. var e = assertThrows(function() { goog.labs.mock.verify(mock).method(4); });
  310. assertTrue(e instanceof goog.labs.mock.VerificationError);
  311. var expected = '\nExpected: method(4) at least 1 times\n' +
  312. 'Recorded: No recorded calls';
  313. assertEquals(expected, e.message);
  314. // Failure when there are recorded calls with ints and functions
  315. // as arguments.
  316. var callback = function() {};
  317. var callbackId = goog.labs.mock.getUid(callback);
  318. mock.method(1);
  319. mock.method(2);
  320. mock.method(callback);
  321. e = assertThrows(function() { goog.labs.mock.verify(mock).method(3); });
  322. assertTrue(e instanceof goog.labs.mock.VerificationError);
  323. expected = '\nExpected: method(3) at least 1 times\n' +
  324. 'Recorded: method(1),\n' +
  325. ' method(2),\n' +
  326. ' method(<function #anonymous' + callbackId + '>)';
  327. assertEquals(expected, e.message);
  328. // With mockFunctions
  329. var mockCallback = goog.labs.mock.mockFunction(callback);
  330. e = assertThrows(function() { goog.labs.mock.verify(mockCallback)(5); });
  331. expected = '\nExpected: #mockFor<#anonymous' + callbackId + '>(5) at least' +
  332. ' 1 times\n' +
  333. 'Recorded: No recorded calls';
  334. mockCallback(8);
  335. goog.labs.mock.verify(mockCallback)(8);
  336. assertEquals(expected, e.message);
  337. // Objects with circular references should not fail.
  338. var obj = {x: 1};
  339. obj.y = obj;
  340. mockCallback(obj);
  341. e = assertThrows(function() { goog.labs.mock.verify(mockCallback)(5); });
  342. assertTrue(e instanceof goog.labs.mock.VerificationError);
  343. // Should respect string representation of different custom classes.
  344. var myClass = function() {};
  345. myClass.prototype.toString = function() { return '<superClass>'; };
  346. var mockFunction = goog.labs.mock.mockFunction(function f() {});
  347. mockFunction(new myClass());
  348. e = assertThrows(function() { goog.labs.mock.verify(mockFunction)(5); });
  349. expected = '\nExpected: #mockFor<f>(5) at least 1 times\n' +
  350. 'Recorded: #mockFor<f>(<superClass>)';
  351. assertEquals(expected, e.message);
  352. }
  353. /**
  354. * Asserts that the given string contains a list of others strings
  355. * in the given order.
  356. */
  357. function assertContainsInOrder(str, var_args) {
  358. var expected = goog.array.splice(arguments, 1);
  359. var indices =
  360. goog.array.map(expected, function(val) { return str.indexOf(val); });
  361. for (var i = 0; i < expected.length; i++) {
  362. var msg = 'Missing "' + expected[i] + '" from "' + str + '"';
  363. assertTrue(msg, indices[i] != -1);
  364. if (i > 0) {
  365. msg = '"' + expected[i - 1] + '" should come before "' + expected[i] +
  366. '" in "' + str + '"';
  367. assertTrue(msg, indices[i] > indices[i - 1]);
  368. }
  369. }
  370. }
  371. function testMatchers() {
  372. var obj = {
  373. method1: function(i) { return 2 * i; },
  374. method2: function(i) { return 5 * i; }
  375. };
  376. var mockObj = goog.labs.mock.mock(obj);
  377. goog.labs.mock.when(mockObj).method1(greaterThan(4)).thenReturn(100);
  378. goog.labs.mock.when(mockObj).method1(lessThan(4)).thenReturn(40);
  379. assertEquals(100, mockObj.method1(5));
  380. assertEquals(100, mockObj.method1(6));
  381. assertEquals(40, mockObj.method1(2));
  382. assertEquals(40, mockObj.method1(1));
  383. assertUndefined(mockObj.method1(4));
  384. }
  385. function testMatcherVerify() {
  386. var obj = {method: function(i) { return 2 * i; }};
  387. // Using spy objects.
  388. var spy = goog.labs.mock.spy(obj);
  389. spy.method(6);
  390. goog.labs.mock.verify(spy).method(greaterThan(4));
  391. var e = assertThrows(
  392. goog.partial(goog.labs.mock.verify(spy).method, lessThan(4)));
  393. assertTrue(e instanceof goog.labs.mock.VerificationError);
  394. // Using mocks
  395. var mockObj = goog.labs.mock.mock(obj);
  396. mockObj.method(8);
  397. goog.labs.mock.verify(mockObj).method(greaterThan(7));
  398. var e = assertThrows(
  399. goog.partial(goog.labs.mock.verify(mockObj).method, lessThan(7)));
  400. assertTrue(e instanceof goog.labs.mock.VerificationError);
  401. }
  402. function testMatcherVerifyCollision() {
  403. var obj = {method: function(i) { return 2 * i; }};
  404. var mockObj = goog.labs.mock.mock(obj);
  405. goog.labs.mock.when(mockObj).method(5).thenReturn(100);
  406. assertNotEquals(100, mockObj.method(greaterThan(2)));
  407. }
  408. function testMatcherVerifyCollisionBetweenMatchers() {
  409. var obj = {method: function(i) { return 2 * i; }};
  410. var mockObj = goog.labs.mock.mock(obj);
  411. goog.labs.mock.when(mockObj).method(anything()).thenReturn(100);
  412. var e = assertThrows(
  413. goog.partial(goog.labs.mock.verify(mockObj).method, anything()));
  414. assertTrue(e instanceof goog.labs.mock.VerificationError);
  415. }
  416. function testVerifyForUnmockedMethods() {
  417. var Task = function() {};
  418. Task.prototype.run = function() {};
  419. var mockTask = goog.labs.mock.mock(Task);
  420. mockTask.run();
  421. goog.labs.mock.verify(mockTask).run();
  422. }
  423. function testFormatMethodCall() {
  424. var formatMethodCall = goog.labs.mock.formatMethodCall_;
  425. assertEquals('alert()', formatMethodCall('alert'));
  426. assertEquals('sum(2, 4)', formatMethodCall('sum', [2, 4]));
  427. assertEquals('sum("2", "4")', formatMethodCall('sum', ['2', '4']));
  428. assertEquals(
  429. 'call(<function unicorn>)',
  430. formatMethodCall('call', [function unicorn() {}]));
  431. var arg = {x: 1, y: {hello: 'world'}};
  432. assertEquals(
  433. 'call(' + goog.labs.mock.formatValue_(arg) + ')',
  434. formatMethodCall('call', [arg]));
  435. }
  436. function testGetFunctionName() {
  437. var f1 = function() {};
  438. var f2 = function() {};
  439. var named = function myName() {};
  440. assert(
  441. goog.string.startsWith(
  442. goog.labs.mock.getFunctionName_(f1), '#anonymous'));
  443. assert(
  444. goog.string.startsWith(
  445. goog.labs.mock.getFunctionName_(f2), '#anonymous'));
  446. assertNotEquals(
  447. goog.labs.mock.getFunctionName_(f1), goog.labs.mock.getFunctionName_(f2));
  448. assertEquals('myName', goog.labs.mock.getFunctionName_(named));
  449. }
  450. function testFormatObject() {
  451. var obj, obj2, obj3;
  452. obj = {x: 1};
  453. assertEquals(
  454. '{"x":1 _id:' + goog.labs.mock.getUid(obj) + '}',
  455. goog.labs.mock.formatValue_(obj));
  456. assertEquals('{"x":1}', goog.labs.mock.formatValue_(obj, false /* id */));
  457. obj = {x: 'hello'};
  458. assertEquals(
  459. '{"x":"hello" _id:' + goog.labs.mock.getUid(obj) + '}',
  460. goog.labs.mock.formatValue_(obj));
  461. assertEquals(
  462. '{"x":"hello"}', goog.labs.mock.formatValue_(obj, false /* id */));
  463. obj3 = {};
  464. obj2 = {y: obj3};
  465. obj3.x = obj2;
  466. assertEquals(
  467. '{"x":{"y":<recursive/dupe obj_' + goog.labs.mock.getUid(obj3) + '> ' +
  468. '_id:' + goog.labs.mock.getUid(obj2) + '} ' +
  469. '_id:' + goog.labs.mock.getUid(obj3) + '}',
  470. goog.labs.mock.formatValue_(obj3));
  471. assertEquals(
  472. '{"x":{"y":<recursive/dupe>}}',
  473. goog.labs.mock.formatValue_(obj3, false /* id */));
  474. obj = {x: function y() {}};
  475. assertEquals(
  476. '{"x":<function y> _id:' + goog.labs.mock.getUid(obj) + '}',
  477. goog.labs.mock.formatValue_(obj));
  478. assertEquals(
  479. '{"x":<function y>}', goog.labs.mock.formatValue_(obj, false /* id */));
  480. }
  481. function testGetUid() {
  482. var obj1 = {};
  483. var obj2 = {};
  484. var func1 = function() {};
  485. var func2 = function() {};
  486. assertNotEquals(goog.labs.mock.getUid(obj1), goog.labs.mock.getUid(obj2));
  487. assertNotEquals(goog.labs.mock.getUid(func1), goog.labs.mock.getUid(func2));
  488. assertNotEquals(goog.labs.mock.getUid(obj1), goog.labs.mock.getUid(func2));
  489. assertEquals(goog.labs.mock.getUid(obj1), goog.labs.mock.getUid(obj1));
  490. assertEquals(goog.labs.mock.getUid(func1), goog.labs.mock.getUid(func1));
  491. }
  492. function testMockEs6ClassMethods() {
  493. // Create an ES6 class via eval so we can bail out if it's a syntax error in
  494. // browsers that don't support ES6 classes.
  495. try {
  496. eval(
  497. 'var Foo = class {' +
  498. ' a() {' +
  499. ' fail(\'real object should never be called\');' +
  500. ' }' +
  501. '}');
  502. } catch (e) {
  503. if (e instanceof SyntaxError) {
  504. return;
  505. }
  506. }
  507. var mockObj = goog.labs.mock.mock(Foo);
  508. goog.labs.mock.when(mockObj).a().thenReturn('a');
  509. assertThrowsJsUnitException(function() { new Foo().a(); });
  510. assertEquals('a', mockObj.a());
  511. goog.labs.mock.verify(mockObj).a();
  512. }