# lib/goog/testing/mockcontrol.js

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