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