Path: blob/trunk/third_party/closure/goog/testing/mockcontrol.js
4500 views
/**1* @license2* Copyright The Closure Library Authors.3* SPDX-License-Identifier: Apache-2.04*/56/**7* @fileoverview A MockControl holds a set of mocks for a particular test.8* It consolidates calls to $replay, $verify, and $tearDown, which simplifies9* the test and helps avoid omissions.10*11* You can create and control a mock:12* var mockFoo = mockControl.addMock(new MyMock(Foo));13*14* MockControl also exposes some convenience functions that create15* controlled mocks for common mocks: StrictMock, LooseMock,16* FunctionMock, MethodMock, and GlobalFunctionMock.17*/181920goog.setTestOnly('goog.testing.MockControl');21goog.provide('goog.testing.MockControl');2223goog.require('goog.Promise');24goog.require('goog.testing');25goog.require('goog.testing.LooseMock');26goog.require('goog.testing.StrictMock');27goog.requireType('goog.testing.MockInterface');28293031/**32* Controls a set of mocks. Controlled mocks are replayed, verified, and33* cleaned-up at the same time.34* @constructor35*/36goog.testing.MockControl = function() {37'use strict';38/**39* The list of mocks being controlled.40* @type {Array<goog.testing.MockInterface>}41* @private42*/43this.mocks_ = [];44};454647/**48* Takes control of this mock.49* @param {goog.testing.MockInterface} mock Mock to be controlled.50* @return {goog.testing.MockInterface} The same mock passed in,51* for convenience.52*/53goog.testing.MockControl.prototype.addMock = function(mock) {54'use strict';55this.mocks_.push(mock);56return mock;57};585960/**61* Calls replay on each controlled mock.62*/63goog.testing.MockControl.prototype.$replayAll = function() {64'use strict';65this.mocks_.forEach(function(m) {66'use strict';67m.$replay();68});69};707172/**73* Calls reset on each controlled mock.74*/75goog.testing.MockControl.prototype.$resetAll = function() {76'use strict';77this.mocks_.forEach(function(m) {78'use strict';79m.$reset();80});81};828384/**85* Returns a Promise that resolves when all of the controlled mocks have86* finished and verified.87* @return {!goog.Promise<!Array<undefined>>}88*/89goog.testing.MockControl.prototype.$waitAndVerifyAll = function() {90'use strict';91return goog.Promise.all(this.mocks_.map(function(m) {92'use strict';93return m.$waitAndVerify();94}));95};969798/**99* Calls verify on each controlled mock.100*/101goog.testing.MockControl.prototype.$verifyAll = function() {102'use strict';103this.mocks_.forEach(function(m) {104'use strict';105m.$verify();106});107};108109110/**111* Calls tearDown on each controlled mock, if necesssary.112*/113goog.testing.MockControl.prototype.$tearDown = function() {114'use strict';115this.mocks_.forEach(function(m) {116'use strict';117if (!m) {118return;119}120m = /** @type {?} */ (m);121// $tearDown if defined.122if (m.$tearDown) {123m.$tearDown();124}125});126};127128129/**130* Creates a controlled StrictMock. Passes its arguments through to the131* StrictMock constructor.132* @param {Object|Function} objectToMock The object that should be mocked, or133* the constructor of an object to mock.134* @param {boolean=} opt_mockStaticMethods An optional argument denoting that135* a mock should be constructed from the static functions of a class.136* @param {boolean=} opt_createProxy An optional argument denoting that137* a proxy for the target mock should be created.138* @return {!goog.testing.StrictMock} The mock object.139*/140goog.testing.MockControl.prototype.createStrictMock = function(141objectToMock, opt_mockStaticMethods, opt_createProxy) {142'use strict';143var m = new goog.testing.StrictMock(144objectToMock, opt_mockStaticMethods, opt_createProxy);145this.addMock(m);146return m;147};148149150/**151* Creates a controlled LooseMock. Passes its arguments through to the152* LooseMock constructor.153* @param {Object|Function} objectToMock The object that should be mocked, or154* the constructor of an object to mock.155* @param {boolean=} opt_ignoreUnexpectedCalls Whether to ignore unexpected156* calls.157* @param {boolean=} opt_mockStaticMethods An optional argument denoting that158* a mock should be constructed from the static functions of a class.159* @param {boolean=} opt_createProxy An optional argument denoting that160* a proxy for the target mock should be created.161* @return {!goog.testing.LooseMock} The mock object.162*/163goog.testing.MockControl.prototype.createLooseMock = function(164objectToMock, opt_ignoreUnexpectedCalls, opt_mockStaticMethods,165opt_createProxy) {166'use strict';167var m = new goog.testing.LooseMock(168objectToMock, opt_ignoreUnexpectedCalls, opt_mockStaticMethods,169opt_createProxy);170this.addMock(m);171return m;172};173174175/**176* Creates a controlled FunctionMock. Passes its arguments through to the177* FunctionMock constructor.178* @param {string=} opt_functionName The optional name of the function to mock179* set to '[anonymous mocked function]' if not passed in.180* @param {number=} opt_strictness One of goog.testing.Mock.LOOSE or181* goog.testing.Mock.STRICT. The default is STRICT.182* @return {!goog.testing.MockInterface} The mocked function.183*/184goog.testing.MockControl.prototype.createFunctionMock = function(185opt_functionName, opt_strictness) {186'use strict';187var m = goog.testing.createFunctionMock(opt_functionName, opt_strictness);188this.addMock(m);189return m;190};191192193/**194* Creates a controlled MethodMock. Passes its arguments through to the195* MethodMock constructor.196* @param {Object} scope The scope of the method to be mocked out.197* @param {string} functionName The name of the function we're going to mock.198* @param {number=} opt_strictness One of goog.testing.Mock.LOOSE or199* goog.testing.Mock.STRICT. The default is STRICT.200* @return {!goog.testing.MockInterface} The mocked method.201*/202goog.testing.MockControl.prototype.createMethodMock = function(203scope, functionName, opt_strictness) {204'use strict';205var m = goog.testing.createMethodMock(scope, functionName, opt_strictness);206this.addMock(m);207return m;208};209210211/**212* Creates a controlled MethodMock for a constructor. Passes its arguments213* through to the MethodMock constructor. See214* {@link goog.testing.createConstructorMock} for details.215* @param {Object} scope The scope of the constructor to be mocked out.216* @param {string} constructorName The name of the function we're going to mock.217* @param {number=} opt_strictness One of goog.testing.Mock.LOOSE or218* goog.testing.Mock.STRICT. The default is STRICT.219* @return {!goog.testing.MockInterface} The mocked method.220*/221goog.testing.MockControl.prototype.createConstructorMock = function(222scope, constructorName, opt_strictness) {223'use strict';224var m = goog.testing.createConstructorMock(225scope, constructorName, opt_strictness);226this.addMock(m);227return m;228};229230231/**232* Creates a controlled GlobalFunctionMock. Passes its arguments through to the233* GlobalFunctionMock constructor.234* @param {string} functionName The name of the function we're going to mock.235* @param {number=} opt_strictness One of goog.testing.Mock.LOOSE or236* goog.testing.Mock.STRICT. The default is STRICT.237* @return {!goog.testing.MockInterface} The mocked function.238*/239goog.testing.MockControl.prototype.createGlobalFunctionMock = function(240functionName, opt_strictness) {241'use strict';242var m = goog.testing.createGlobalFunctionMock(functionName, opt_strictness);243this.addMock(m);244return m;245};246247248