| 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.FunctionMockTest'); |
| 16 |
goog.setTestOnly('goog.testing.FunctionMockTest'); |
| 17 |
|
| 18 |
goog.require('goog.array'); |
| 19 |
goog.require('goog.string'); |
| 20 |
goog.require('goog.testing'); |
| 21 |
goog.require('goog.testing.FunctionMock'); |
| 22 |
goog.require('goog.testing.Mock'); |
| 23 |
goog.require('goog.testing.ObjectPropertyString'); |
| 24 |
goog.require('goog.testing.StrictMock'); |
| 25 |
goog.require('goog.testing.asserts'); |
| 26 |
goog.require('goog.testing.jsunit'); |
| 27 |
goog.require('goog.testing.mockmatchers'); |
| 28 |
|
| 29 |
// Global scope so we can tear it down safely |
| 30 |
var mockGlobal; |
| 31 |
|
| 32 |
function tearDown() { |
| 33 |
if (mockGlobal) { |
| 34 |
mockGlobal.$tearDown(); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
//----- Tests for goog.testing.FunctionMock |
| 40 |
|
| 41 |
function testMockFunctionCallOrdering() { |
| 42 |
var doOneTest = function(mockFunction, success, expected_args, actual_args) { |
| 43 |
goog.array.forEach(expected_args, function(arg) { mockFunction(arg); }); |
| 44 |
mockFunction.$replay(); |
| 45 |
var callFunction = function() { |
| 46 |
goog.array.forEach(actual_args, function(arg) { mockFunction(arg); }); |
| 47 |
mockFunction.$verify(); |
| 48 |
}; |
| 49 |
if (success) { |
| 50 |
callFunction(); |
| 51 |
} else { |
| 52 |
assertThrowsJsUnitException(callFunction); |
| 53 |
} |
| 54 |
}; |
| 55 |
|
| 56 |
var doTest = function(strict_ok, loose_ok, expected_args, actual_args) { |
| 57 |
doOneTest( |
| 58 |
goog.testing.createFunctionMock(), strict_ok, expected_args, |
| 59 |
actual_args); |
| 60 |
doOneTest( |
| 61 |
goog.testing.createFunctionMock('name'), strict_ok, expected_args, |
| 62 |
actual_args); |
| 63 |
doOneTest( |
| 64 |
goog.testing.createFunctionMock('name', goog.testing.Mock.STRICT), |
| 65 |
strict_ok, expected_args, actual_args); |
| 66 |
doOneTest( |
| 67 |
goog.testing.createFunctionMock('name', goog.testing.Mock.LOOSE), |
| 68 |
loose_ok, expected_args, actual_args); |
| 69 |
}; |
| 70 |
|
| 71 |
doTest(true, true, [1, 2], [1, 2]); |
| 72 |
doTest(false, true, [1, 2], [2, 1]); |
| 73 |
doTest(false, false, [1, 2], [2, 2]); |
| 74 |
doTest(false, false, [1, 2], [1]); |
| 75 |
doTest(false, false, [1, 2], [1, 1]); |
| 76 |
doTest(false, false, [1, 2], [1]); |
| 77 |
} |
| 78 |
|
| 79 |
function testMocksFunctionWithNoArgs() { |
| 80 |
var mockFoo = goog.testing.createFunctionMock(); |
| 81 |
mockFoo(); |
| 82 |
mockFoo.$replay(); |
| 83 |
mockFoo(); |
| 84 |
mockFoo.$verify(); |
| 85 |
} |
| 86 |
|
| 87 |
function testMocksFunctionWithOneArg() { |
| 88 |
var mockFoo = goog.testing.createFunctionMock(); |
| 89 |
mockFoo('x'); |
| 90 |
mockFoo.$replay(); |
| 91 |
mockFoo('x'); |
| 92 |
mockFoo.$verify(); |
| 93 |
} |
| 94 |
|
| 95 |
function testMocksFunctionWithMultipleArgs() { |
| 96 |
var mockFoo = goog.testing.createFunctionMock(); |
| 97 |
mockFoo('x', 'y'); |
| 98 |
mockFoo.$replay(); |
| 99 |
mockFoo('x', 'y'); |
| 100 |
mockFoo.$verify(); |
| 101 |
} |
| 102 |
|
| 103 |
function testFailsIfCalledWithIncorrectArgs() { |
| 104 |
var mockFoo = goog.testing.createFunctionMock(); |
| 105 |
|
| 106 |
mockFoo(); |
| 107 |
mockFoo.$replay(); |
| 108 |
assertThrowsJsUnitException(function() { mockFoo('x'); }); |
| 109 |
mockFoo.$reset(); |
| 110 |
|
| 111 |
mockFoo('x'); |
| 112 |
mockFoo.$replay(); |
| 113 |
assertThrowsJsUnitException(function() { mockFoo(); }); |
| 114 |
mockFoo.$reset(); |
| 115 |
|
| 116 |
mockFoo('x'); |
| 117 |
mockFoo.$replay(); |
| 118 |
assertThrowsJsUnitException(function() { mockFoo('x', 'y'); }); |
| 119 |
mockFoo.$reset(); |
| 120 |
|
| 121 |
mockFoo('x', 'y'); |
| 122 |
mockFoo.$replay(); |
| 123 |
assertThrowsJsUnitException(function() { mockFoo('x'); }); |
| 124 |
mockFoo.$reset(); |
| 125 |
|
| 126 |
mockFoo('correct'); |
| 127 |
mockFoo.$replay(); |
| 128 |
assertThrowsJsUnitException(function() { mockFoo('wrong'); }); |
| 129 |
mockFoo.$reset(); |
| 130 |
|
| 131 |
mockFoo('correct', 'args'); |
| 132 |
mockFoo.$replay(); |
| 133 |
assertThrowsJsUnitException(function() { mockFoo('wrong', 'args'); }); |
| 134 |
mockFoo.$reset(); |
| 135 |
} |
| 136 |
|
| 137 |
function testMocksFunctionWithReturnValue() { |
| 138 |
var mockFoo = goog.testing.createFunctionMock(); |
| 139 |
mockFoo().$returns('bar'); |
| 140 |
mockFoo.$replay(); |
| 141 |
assertEquals('bar', mockFoo()); |
| 142 |
mockFoo.$verify(); |
| 143 |
} |
| 144 |
|
| 145 |
function testFunctionMockWorksWhenPassedAsACallback() { |
| 146 |
var invoker = { |
| 147 |
register: function(callback) { this.callback = callback; }, |
| 148 |
|
| 149 |
invoke: function(args) { return this.callback(args); } |
| 150 |
}; |
| 151 |
|
| 152 |
var mockFunction = goog.testing.createFunctionMock(); |
| 153 |
mockFunction('bar').$returns('baz'); |
| 154 |
|
| 155 |
mockFunction.$replay(); |
| 156 |
invoker.register(mockFunction); |
| 157 |
assertEquals('baz', invoker.invoke('bar')); |
| 158 |
mockFunction.$verify(); |
| 159 |
} |
| 160 |
|
| 161 |
function testFunctionMockQuacksLikeAStrictMock() { |
| 162 |
var mockFunction = goog.testing.createFunctionMock(); |
| 163 |
assertQuacksLike(mockFunction, goog.testing.StrictMock); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
//----- Global functions for goog.testing.GlobalFunctionMock to mock |
| 168 |
|
| 169 |
function globalFoo() { |
| 170 |
return 'I am Spartacus!'; |
| 171 |
} |
| 172 |
|
| 173 |
function globalBar(who, what) { |
| 174 |
return [who, 'is', what].join(' '); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
//----- Tests for goog.testing.createGlobalFunctionMock |
| 179 |
|
| 180 |
function testMocksGlobalFunctionWithNoArgs() { |
| 181 |
mockGlobal = goog.testing.createGlobalFunctionMock('globalFoo'); |
| 182 |
mockGlobal().$returns('No, I am Spartacus!'); |
| 183 |
|
| 184 |
mockGlobal.$replay(); |
| 185 |
assertEquals('No, I am Spartacus!', globalFoo()); |
| 186 |
mockGlobal.$verify(); |
| 187 |
} |
| 188 |
|
| 189 |
function testMocksGlobalFunctionUsingGlobalName() { |
| 190 |
goog.testing.createGlobalFunctionMock('globalFoo'); |
| 191 |
globalFoo().$returns('No, I am Spartacus!'); |
| 192 |
|
| 193 |
globalFoo.$replay(); |
| 194 |
assertEquals('No, I am Spartacus!', globalFoo()); |
| 195 |
globalFoo.$verify(); |
| 196 |
globalFoo.$tearDown(); |
| 197 |
} |
| 198 |
|
| 199 |
function testMocksGlobalFunctionWithArgs() { |
| 200 |
var mockReturnValue = 'Noam is Chomsky!'; |
| 201 |
mockGlobal = goog.testing.createGlobalFunctionMock('globalBar'); |
| 202 |
mockGlobal('Noam', 'Spartacus').$returns(mockReturnValue); |
| 203 |
|
| 204 |
mockGlobal.$replay(); |
| 205 |
assertEquals(mockReturnValue, globalBar('Noam', 'Spartacus')); |
| 206 |
mockGlobal.$verify(); |
| 207 |
} |
| 208 |
|
| 209 |
function testGlobalFunctionMockFailsWithIncorrectArgs() { |
| 210 |
mockGlobal = goog.testing.createGlobalFunctionMock('globalBar'); |
| 211 |
mockGlobal('a', 'b'); |
| 212 |
|
| 213 |
mockGlobal.$replay(); |
| 214 |
|
| 215 |
assertThrowsJsUnitException(function() { globalBar('b', 'a'); }); |
| 216 |
} |
| 217 |
|
| 218 |
function testGlobalFunctionMockQuacksLikeAFunctionMock() { |
| 219 |
mockGlobal = goog.testing.createGlobalFunctionMock('globalFoo'); |
| 220 |
assertQuacksLike(mockGlobal, goog.testing.FunctionMock); |
| 221 |
} |
| 222 |
|
| 223 |
function testMockedFunctionsAvailableInGlobalAndGoogGlobalAndWindowScope() { |
| 224 |
mockGlobal = goog.testing.createGlobalFunctionMock('globalFoo'); |
| 225 |
|
| 226 |
// we expect this call 3 times through global, goog.global and window scope |
| 227 |
mockGlobal().$times(3); |
| 228 |
|
| 229 |
mockGlobal.$replay(); |
| 230 |
goog.global.globalFoo(); |
| 231 |
window.globalFoo(); |
| 232 |
globalFoo(); |
| 233 |
mockGlobal.$verify(); |
| 234 |
} |
| 235 |
|
| 236 |
function testTearDownRestoresOriginalGlobalFunction() { |
| 237 |
mockGlobal = goog.testing.createGlobalFunctionMock('globalFoo'); |
| 238 |
mockGlobal().$returns('No, I am Spartacus!'); |
| 239 |
|
| 240 |
mockGlobal.$replay(); |
| 241 |
assertEquals('No, I am Spartacus!', globalFoo()); |
| 242 |
mockGlobal.$tearDown(); |
| 243 |
assertEquals('I am Spartacus!', globalFoo()); |
| 244 |
mockGlobal.$verify(); |
| 245 |
} |
| 246 |
|
| 247 |
function testTearDownHandlesMultipleMocking() { |
| 248 |
var mock1 = goog.testing.createGlobalFunctionMock('globalFoo'); |
| 249 |
var mock2 = goog.testing.createGlobalFunctionMock('globalFoo'); |
| 250 |
var mock3 = goog.testing.createGlobalFunctionMock('globalFoo'); |
| 251 |
mock1().$returns('No, I am Spartacus 1!'); |
| 252 |
mock2().$returns('No, I am Spartacus 2!'); |
| 253 |
mock3().$returns('No, I am Spartacus 3!'); |
| 254 |
|
| 255 |
mock1.$replay(); |
| 256 |
mock2.$replay(); |
| 257 |
mock3.$replay(); |
| 258 |
assertEquals('No, I am Spartacus 3!', globalFoo()); |
| 259 |
mock3.$tearDown(); |
| 260 |
assertEquals('No, I am Spartacus 2!', globalFoo()); |
| 261 |
mock2.$tearDown(); |
| 262 |
assertEquals('No, I am Spartacus 1!', globalFoo()); |
| 263 |
mock1.$tearDown(); |
| 264 |
assertEquals('I am Spartacus!', globalFoo()); |
| 265 |
} |
| 266 |
|
| 267 |
function testGlobalFunctionMockCallOrdering() { |
| 268 |
var mock = goog.testing.createGlobalFunctionMock('globalFoo'); |
| 269 |
mock(1); |
| 270 |
mock(2); |
| 271 |
mock.$replay(); |
| 272 |
assertThrowsJsUnitException(function() { globalFoo(2); }); |
| 273 |
mock.$tearDown(); |
| 274 |
|
| 275 |
mock = goog.testing.createGlobalFunctionMock( |
| 276 |
'globalFoo', goog.testing.Mock.STRICT); |
| 277 |
mock(1); |
| 278 |
mock(2); |
| 279 |
mock.$replay(); |
| 280 |
globalFoo(1); |
| 281 |
globalFoo(2); |
| 282 |
mock.$verify(); |
| 283 |
mock.$tearDown(); |
| 284 |
|
| 285 |
mock = goog.testing.createGlobalFunctionMock( |
| 286 |
'globalFoo', goog.testing.Mock.STRICT); |
| 287 |
mock(1); |
| 288 |
mock(2); |
| 289 |
mock.$replay(); |
| 290 |
assertThrowsJsUnitException(function() { globalFoo(2); }); |
| 291 |
mock.$tearDown(); |
| 292 |
|
| 293 |
mock = goog.testing.createGlobalFunctionMock( |
| 294 |
'globalFoo', goog.testing.Mock.LOOSE); |
| 295 |
mock(1); |
| 296 |
mock(2); |
| 297 |
mock.$replay(); |
| 298 |
globalFoo(2); |
| 299 |
globalFoo(1); |
| 300 |
mock.$verify(); |
| 301 |
mock.$tearDown(); |
| 302 |
} |
| 303 |
|
| 304 |
//----- Functions for goog.testing.MethodMock to mock |
| 305 |
|
| 306 |
var mynamespace = {}; |
| 307 |
|
| 308 |
mynamespace.myMethod = function() { |
| 309 |
return 'I should be mocked.'; |
| 310 |
}; |
| 311 |
|
| 312 |
function testMocksMethod() { |
| 313 |
mockMethod = goog.testing.createMethodMock(mynamespace, 'myMethod'); |
| 314 |
mockMethod().$returns('I have been mocked!'); |
| 315 |
|
| 316 |
mockMethod.$replay(); |
| 317 |
assertEquals('I have been mocked!', mockMethod()); |
| 318 |
mockMethod.$verify(); |
| 319 |
} |
| 320 |
|
| 321 |
function testMocksMethodInNamespace() { |
| 322 |
goog.testing.createMethodMock(mynamespace, 'myMethod'); |
| 323 |
mynamespace.myMethod().$returns('I have been mocked!'); |
| 324 |
|
| 325 |
mynamespace.myMethod.$replay(); |
| 326 |
assertEquals('I have been mocked!', mynamespace.myMethod()); |
| 327 |
mynamespace.myMethod.$verify(); |
| 328 |
mynamespace.myMethod.$tearDown(); |
| 329 |
} |
| 330 |
|
| 331 |
function testMethodMockCanOnlyMockExistingMethods() { |
| 332 |
assertThrows(function() { |
| 333 |
goog.testing.createMethodMock(mynamespace, 'doesNotExist'); |
| 334 |
}); |
| 335 |
} |
| 336 |
|
| 337 |
function testMethodMockCallOrdering() { |
| 338 |
goog.testing.createMethodMock(mynamespace, 'myMethod'); |
| 339 |
mynamespace.myMethod(1); |
| 340 |
mynamespace.myMethod(2); |
| 341 |
mynamespace.myMethod.$replay(); |
| 342 |
assertThrowsJsUnitException(function() { mynamespace.myMethod(2); }); |
| 343 |
mynamespace.myMethod.$tearDown(); |
| 344 |
|
| 345 |
goog.testing.createMethodMock( |
| 346 |
mynamespace, 'myMethod', goog.testing.Mock.STRICT); |
| 347 |
mynamespace.myMethod(1); |
| 348 |
mynamespace.myMethod(2); |
| 349 |
mynamespace.myMethod.$replay(); |
| 350 |
mynamespace.myMethod(1); |
| 351 |
mynamespace.myMethod(2); |
| 352 |
mynamespace.myMethod.$verify(); |
| 353 |
mynamespace.myMethod.$tearDown(); |
| 354 |
|
| 355 |
goog.testing.createMethodMock( |
| 356 |
mynamespace, 'myMethod', goog.testing.Mock.STRICT); |
| 357 |
mynamespace.myMethod(1); |
| 358 |
mynamespace.myMethod(2); |
| 359 |
mynamespace.myMethod.$replay(); |
| 360 |
assertThrowsJsUnitException(function() { mynamespace.myMethod(2); }); |
| 361 |
mynamespace.myMethod.$tearDown(); |
| 362 |
|
| 363 |
goog.testing.createMethodMock( |
| 364 |
mynamespace, 'myMethod', goog.testing.Mock.LOOSE); |
| 365 |
mynamespace.myMethod(1); |
| 366 |
mynamespace.myMethod(2); |
| 367 |
mynamespace.myMethod.$replay(); |
| 368 |
mynamespace.myMethod(2); |
| 369 |
mynamespace.myMethod(1); |
| 370 |
mynamespace.myMethod.$verify(); |
| 371 |
mynamespace.myMethod.$tearDown(); |
| 372 |
} |
| 373 |
|
| 374 |
function testMocksMethodFromObjectPropertyString() { |
| 375 |
mockMethod = goog.testing.MethodMock.fromObjectPropertyString( |
| 376 |
new goog.testing.ObjectPropertyString(mynamespace, 'myMethod')); |
| 377 |
mockMethod().$returns('I have been mocked!'); |
| 378 |
|
| 379 |
mockMethod.$replay(); |
| 380 |
assertEquals('I have been mocked!', mynamespace.myMethod()); |
| 381 |
mockMethod.$verify(); |
| 382 |
} |
| 383 |
|
| 384 |
//----- Functions for goog.testing.createConstructorMock to mock |
| 385 |
|
| 386 |
var constructornamespace = {}; |
| 387 |
|
| 388 |
constructornamespace.MyConstructor = function() {}; |
| 389 |
|
| 390 |
constructornamespace.MyConstructor.prototype.myMethod = function() { |
| 391 |
return 'I should be mocked.'; |
| 392 |
}; |
| 393 |
|
| 394 |
constructornamespace.MyConstructorWithArgument = function(argument) { |
| 395 |
this.argument_ = argument; |
| 396 |
}; |
| 397 |
|
| 398 |
constructornamespace.MyConstructorWithArgument.prototype.myMethod = function() { |
| 399 |
return this.argument_; |
| 400 |
}; |
| 401 |
|
| 402 |
constructornamespace.MyConstructorWithClassMembers = function() {}; |
| 403 |
|
| 404 |
constructornamespace.MyConstructorWithClassMembers.CONSTANT = 42; |
| 405 |
|
| 406 |
constructornamespace.MyConstructorWithClassMembers.classMethod = function() { |
| 407 |
return 'class method return value'; |
| 408 |
}; |
| 409 |
|
| 410 |
function testConstructorMock() { |
| 411 |
var mockObject = |
| 412 |
new goog.testing.StrictMock(constructornamespace.MyConstructor); |
| 413 |
var mockConstructor = |
| 414 |
goog.testing.createConstructorMock(constructornamespace, 'MyConstructor'); |
| 415 |
mockConstructor().$returns(mockObject); |
| 416 |
mockObject.myMethod().$returns('I have been mocked!'); |
| 417 |
|
| 418 |
mockConstructor.$replay(); |
| 419 |
mockObject.$replay(); |
| 420 |
assertEquals( |
| 421 |
'I have been mocked!', |
| 422 |
new constructornamespace.MyConstructor().myMethod()); |
| 423 |
mockConstructor.$verify(); |
| 424 |
mockObject.$verify(); |
| 425 |
mockConstructor.$tearDown(); |
| 426 |
} |
| 427 |
|
| 428 |
function testConstructorMockWithArgument() { |
| 429 |
var mockObject = new goog.testing.StrictMock( |
| 430 |
constructornamespace.MyConstructorWithArgument); |
| 431 |
var mockConstructor = goog.testing.createConstructorMock( |
| 432 |
constructornamespace, 'MyConstructorWithArgument'); |
| 433 |
mockConstructor(goog.testing.mockmatchers.isString).$returns(mockObject); |
| 434 |
mockObject.myMethod().$returns('I have been mocked!'); |
| 435 |
|
| 436 |
mockConstructor.$replay(); |
| 437 |
mockObject.$replay(); |
| 438 |
assertEquals( |
| 439 |
'I have been mocked!', |
| 440 |
new constructornamespace.MyConstructorWithArgument('I should be mocked.') |
| 441 |
.myMethod()); |
| 442 |
mockConstructor.$verify(); |
| 443 |
mockObject.$verify(); |
| 444 |
mockConstructor.$tearDown(); |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
/** |
| 449 |
* Test that class members are copied to the mock constructor. |
| 450 |
*/ |
| 451 |
function testConstructorMockWithClassMembers() { |
| 452 |
var mockConstructor = goog.testing.createConstructorMock( |
| 453 |
constructornamespace, 'MyConstructorWithClassMembers'); |
| 454 |
assertEquals(42, constructornamespace.MyConstructorWithClassMembers.CONSTANT); |
| 455 |
assertEquals( |
| 456 |
'class method return value', |
| 457 |
constructornamespace.MyConstructorWithClassMembers.classMethod()); |
| 458 |
mockConstructor.$tearDown(); |
| 459 |
} |
| 460 |
|
| 461 |
function testConstructorMockCallOrdering() { |
| 462 |
var instance = {}; |
| 463 |
|
| 464 |
goog.testing.createConstructorMock( |
| 465 |
constructornamespace, 'MyConstructorWithArgument'); |
| 466 |
constructornamespace.MyConstructorWithArgument(1).$returns(instance); |
| 467 |
constructornamespace.MyConstructorWithArgument(2).$returns(instance); |
| 468 |
constructornamespace.MyConstructorWithArgument.$replay(); |
| 469 |
assertThrowsJsUnitException(function() { |
| 470 |
new constructornamespace.MyConstructorWithArgument(2); |
| 471 |
}); |
| 472 |
constructornamespace.MyConstructorWithArgument.$tearDown(); |
| 473 |
|
| 474 |
goog.testing.createConstructorMock( |
| 475 |
constructornamespace, 'MyConstructorWithArgument', |
| 476 |
goog.testing.Mock.STRICT); |
| 477 |
constructornamespace.MyConstructorWithArgument(1).$returns(instance); |
| 478 |
constructornamespace.MyConstructorWithArgument(2).$returns(instance); |
| 479 |
constructornamespace.MyConstructorWithArgument.$replay(); |
| 480 |
new constructornamespace.MyConstructorWithArgument(1); |
| 481 |
new constructornamespace.MyConstructorWithArgument(2); |
| 482 |
constructornamespace.MyConstructorWithArgument.$verify(); |
| 483 |
constructornamespace.MyConstructorWithArgument.$tearDown(); |
| 484 |
|
| 485 |
goog.testing.createConstructorMock( |
| 486 |
constructornamespace, 'MyConstructorWithArgument', |
| 487 |
goog.testing.Mock.STRICT); |
| 488 |
constructornamespace.MyConstructorWithArgument(1).$returns(instance); |
| 489 |
constructornamespace.MyConstructorWithArgument(2).$returns(instance); |
| 490 |
constructornamespace.MyConstructorWithArgument.$replay(); |
| 491 |
assertThrowsJsUnitException(function() { |
| 492 |
new constructornamespace.MyConstructorWithArgument(2); |
| 493 |
}); |
| 494 |
constructornamespace.MyConstructorWithArgument.$tearDown(); |
| 495 |
|
| 496 |
goog.testing.createConstructorMock( |
| 497 |
constructornamespace, 'MyConstructorWithArgument', |
| 498 |
goog.testing.Mock.LOOSE); |
| 499 |
constructornamespace.MyConstructorWithArgument(1).$returns(instance); |
| 500 |
constructornamespace.MyConstructorWithArgument(2).$returns(instance); |
| 501 |
constructornamespace.MyConstructorWithArgument.$replay(); |
| 502 |
new constructornamespace.MyConstructorWithArgument(2); |
| 503 |
new constructornamespace.MyConstructorWithArgument(1); |
| 504 |
constructornamespace.MyConstructorWithArgument.$verify(); |
| 505 |
constructornamespace.MyConstructorWithArgument.$tearDown(); |
| 506 |
} |
| 507 |
|
| 508 |
//----- Helper assertions |
| 509 |
|
| 510 |
function assertQuacksLike(obj, target) { |
| 511 |
for (meth in target.prototype) { |
| 512 |
if (!goog.string.endsWith(meth, '_')) { |
| 513 |
assertNotUndefined('Should have implemented ' + meth + '()', obj[meth]); |
| 514 |
} |
| 515 |
} |
| 516 |
} |