| 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 |
/** |
| 16 |
* @fileoverview A MockControl holds a set of mocks for a particular test. |
| 17 |
* It consolidates calls to $replay, $verify, and $tearDown, which simplifies |
| 18 |
* the test and helps avoid omissions. |
| 19 |
* |
| 20 |
* You can create and control a mock: |
| 21 |
* var mockFoo = mockControl.addMock(new MyMock(Foo)); |
| 22 |
* |
| 23 |
* MockControl also exposes some convenience functions that create |
| 24 |
* controlled mocks for common mocks: StrictMock, LooseMock, |
| 25 |
* FunctionMock, MethodMock, and GlobalFunctionMock. |
| 26 |
* |
| 27 |
*/ |
| 28 |
|
| 29 |
|
| 30 |
goog.setTestOnly('goog.testing.MockControl'); |
| 31 |
goog.provide('goog.testing.MockControl'); |
| 32 |
|
| 33 |
goog.require('goog.array'); |
| 34 |
goog.require('goog.testing'); |
| 35 |
goog.require('goog.testing.LooseMock'); |
| 36 |
goog.require('goog.testing.StrictMock'); |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
/** |
| 41 |
* Controls a set of mocks. Controlled mocks are replayed, verified, and |
| 42 |
* cleaned-up at the same time. |
| 43 |
* @constructor |
| 44 |
*/ |
| 45 |
goog.testing.MockControl = function() { |
| 46 |
/** |
| 47 |
* The list of mocks being controlled. |
| 48 |
* @type {Array<goog.testing.MockInterface>} |
| 49 |
* @private |
| 50 |
*/ |
| 51 |
this.mocks_ = []; |
| 52 |
}; |
| 53 |
|
| 54 |
|
| 55 |
/** |
| 56 |
* Takes control of this mock. |
| 57 |
* @param {goog.testing.MockInterface} mock Mock to be controlled. |
| 58 |
* @return {goog.testing.MockInterface} The same mock passed in, |
| 59 |
* for convenience. |
| 60 |
*/ |
| 61 |
goog.testing.MockControl.prototype.addMock = function(mock) { |
| 62 |
this.mocks_.push(mock); |
| 63 |
return mock; |
| 64 |
}; |
| 65 |
|
| 66 |
|
| 67 |
/** |
| 68 |
* Calls replay on each controlled mock. |
| 69 |
*/ |
| 70 |
goog.testing.MockControl.prototype.$replayAll = function() { |
| 71 |
goog.array.forEach(this.mocks_, function(m) { m.$replay(); }); |
| 72 |
}; |
| 73 |
|
| 74 |
|
| 75 |
/** |
| 76 |
* Calls reset on each controlled mock. |
| 77 |
*/ |
| 78 |
goog.testing.MockControl.prototype.$resetAll = function() { |
| 79 |
goog.array.forEach(this.mocks_, function(m) { m.$reset(); }); |
| 80 |
}; |
| 81 |
|
| 82 |
|
| 83 |
/** |
| 84 |
* Calls verify on each controlled mock. |
| 85 |
*/ |
| 86 |
goog.testing.MockControl.prototype.$verifyAll = function() { |
| 87 |
goog.array.forEach(this.mocks_, function(m) { m.$verify(); }); |
| 88 |
}; |
| 89 |
|
| 90 |
|
| 91 |
/** |
| 92 |
* Calls tearDown on each controlled mock, if necesssary. |
| 93 |
*/ |
| 94 |
goog.testing.MockControl.prototype.$tearDown = function() { |
| 95 |
goog.array.forEach(this.mocks_, function(m) { |
| 96 |
// $tearDown if defined. |
| 97 |
if (m.$tearDown) { |
| 98 |
m.$tearDown(); |
| 99 |
} |
| 100 |
// TODO(user): Somehow determine if verifyAll should have been called |
| 101 |
// but was not. |
| 102 |
}); |
| 103 |
}; |
| 104 |
|
| 105 |
|
| 106 |
/** |
| 107 |
* Creates a controlled StrictMock. Passes its arguments through to the |
| 108 |
* StrictMock constructor. |
| 109 |
* @param {Object|Function} objectToMock The object that should be mocked, or |
| 110 |
* the constructor of an object to mock. |
| 111 |
* @param {boolean=} opt_mockStaticMethods An optional argument denoting that |
| 112 |
* a mock should be constructed from the static functions of a class. |
| 113 |
* @param {boolean=} opt_createProxy An optional argument denoting that |
| 114 |
* a proxy for the target mock should be created. |
| 115 |
* @return {!goog.testing.StrictMock} The mock object. |
| 116 |
*/ |
| 117 |
goog.testing.MockControl.prototype.createStrictMock = function( |
| 118 |
objectToMock, opt_mockStaticMethods, opt_createProxy) { |
| 119 |
var m = new goog.testing.StrictMock( |
| 120 |
objectToMock, opt_mockStaticMethods, opt_createProxy); |
| 121 |
this.addMock(m); |
| 122 |
return m; |
| 123 |
}; |
| 124 |
|
| 125 |
|
| 126 |
/** |
| 127 |
* Creates a controlled LooseMock. Passes its arguments through to the |
| 128 |
* LooseMock constructor. |
| 129 |
* @param {Object|Function} objectToMock The object that should be mocked, or |
| 130 |
* the constructor of an object to mock. |
| 131 |
* @param {boolean=} opt_ignoreUnexpectedCalls Whether to ignore unexpected |
| 132 |
* calls. |
| 133 |
* @param {boolean=} opt_mockStaticMethods An optional argument denoting that |
| 134 |
* a mock should be constructed from the static functions of a class. |
| 135 |
* @param {boolean=} opt_createProxy An optional argument denoting that |
| 136 |
* a proxy for the target mock should be created. |
| 137 |
* @return {!goog.testing.LooseMock} The mock object. |
| 138 |
*/ |
| 139 |
goog.testing.MockControl.prototype.createLooseMock = function( |
| 140 |
objectToMock, opt_ignoreUnexpectedCalls, opt_mockStaticMethods, |
| 141 |
opt_createProxy) { |
| 142 |
var m = new goog.testing.LooseMock( |
| 143 |
objectToMock, opt_ignoreUnexpectedCalls, opt_mockStaticMethods, |
| 144 |
opt_createProxy); |
| 145 |
this.addMock(m); |
| 146 |
return m; |
| 147 |
}; |
| 148 |
|
| 149 |
|
| 150 |
/** |
| 151 |
* Creates a controlled FunctionMock. Passes its arguments through to the |
| 152 |
* FunctionMock constructor. |
| 153 |
* @param {string=} opt_functionName The optional name of the function to mock |
| 154 |
* set to '[anonymous mocked function]' if not passed in. |
| 155 |
* @param {number=} opt_strictness One of goog.testing.Mock.LOOSE or |
| 156 |
* goog.testing.Mock.STRICT. The default is STRICT. |
| 157 |
* @return {!goog.testing.MockInterface} The mocked function. |
| 158 |
*/ |
| 159 |
goog.testing.MockControl.prototype.createFunctionMock = function( |
| 160 |
opt_functionName, opt_strictness) { |
| 161 |
var m = goog.testing.createFunctionMock(opt_functionName, opt_strictness); |
| 162 |
this.addMock(m); |
| 163 |
return m; |
| 164 |
}; |
| 165 |
|
| 166 |
|
| 167 |
/** |
| 168 |
* Creates a controlled MethodMock. Passes its arguments through to the |
| 169 |
* MethodMock constructor. |
| 170 |
* @param {Object} scope The scope of the method to be mocked out. |
| 171 |
* @param {string} functionName The name of the function we're going to mock. |
| 172 |
* @param {number=} opt_strictness One of goog.testing.Mock.LOOSE or |
| 173 |
* goog.testing.Mock.STRICT. The default is STRICT. |
| 174 |
* @return {!goog.testing.MockInterface} The mocked method. |
| 175 |
*/ |
| 176 |
goog.testing.MockControl.prototype.createMethodMock = function( |
| 177 |
scope, functionName, opt_strictness) { |
| 178 |
var m = goog.testing.createMethodMock(scope, functionName, opt_strictness); |
| 179 |
this.addMock(m); |
| 180 |
return m; |
| 181 |
}; |
| 182 |
|
| 183 |
|
| 184 |
/** |
| 185 |
* Creates a controlled MethodMock for a constructor. Passes its arguments |
| 186 |
* through to the MethodMock constructor. See |
| 187 |
* {@link goog.testing.createConstructorMock} for details. |
| 188 |
* @param {Object} scope The scope of the constructor to be mocked out. |
| 189 |
* @param {string} constructorName The name of the function we're going to mock. |
| 190 |
* @param {number=} opt_strictness One of goog.testing.Mock.LOOSE or |
| 191 |
* goog.testing.Mock.STRICT. The default is STRICT. |
| 192 |
* @return {!goog.testing.MockInterface} The mocked method. |
| 193 |
*/ |
| 194 |
goog.testing.MockControl.prototype.createConstructorMock = function( |
| 195 |
scope, constructorName, opt_strictness) { |
| 196 |
var m = goog.testing.createConstructorMock( |
| 197 |
scope, constructorName, opt_strictness); |
| 198 |
this.addMock(m); |
| 199 |
return m; |
| 200 |
}; |
| 201 |
|
| 202 |
|
| 203 |
/** |
| 204 |
* Creates a controlled GlobalFunctionMock. Passes its arguments through to the |
| 205 |
* GlobalFunctionMock constructor. |
| 206 |
* @param {string} functionName The name of the function we're going to mock. |
| 207 |
* @param {number=} opt_strictness One of goog.testing.Mock.LOOSE or |
| 208 |
* goog.testing.Mock.STRICT. The default is STRICT. |
| 209 |
* @return {!goog.testing.MockInterface} The mocked function. |
| 210 |
*/ |
| 211 |
goog.testing.MockControl.prototype.createGlobalFunctionMock = function( |
| 212 |
functionName, opt_strictness) { |
| 213 |
var m = goog.testing.createGlobalFunctionMock(functionName, opt_strictness); |
| 214 |
this.addMock(m); |
| 215 |
return m; |
| 216 |
}; |