mockcontrol.js 7.2 KB

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