mockcontrol.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright 2010 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 wrapper for MockControl that provides mocks and assertions
  16. * for testing asynchronous code. All assertions will only be verified when
  17. * $verifyAll is called on the wrapped MockControl.
  18. *
  19. * This class is meant primarily for testing code that exposes asynchronous APIs
  20. * without being truly asynchronous (using asynchronous primitives like browser
  21. * events or timeouts). This is often the case when true asynchronous
  22. * depedencies have been mocked out. This means that it doesn't rely on
  23. * AsyncTestCase or DeferredTestCase, although it can be used with those as
  24. * well.
  25. *
  26. * Example usage:
  27. *
  28. * <pre>
  29. * var mockControl = new goog.testing.MockControl();
  30. * var asyncMockControl = new goog.testing.async.MockControl(mockControl);
  31. *
  32. * myAsyncObject.onSuccess(asyncMockControl.asyncAssertEquals(
  33. * 'callback should run and pass the correct value',
  34. * 'http://someurl.com');
  35. * asyncMockControl.assertDeferredEquals(
  36. * 'deferred object should be resolved with the correct value',
  37. * 'http://someurl.com',
  38. * myAsyncObject.getDeferredUrl());
  39. * asyncMockControl.run();
  40. * mockControl.$verifyAll();
  41. * </pre>
  42. *
  43. */
  44. goog.setTestOnly('goog.testing.async.MockControl');
  45. goog.provide('goog.testing.async.MockControl');
  46. goog.require('goog.asserts');
  47. goog.require('goog.async.Deferred');
  48. goog.require('goog.debug');
  49. goog.require('goog.testing.asserts');
  50. goog.require('goog.testing.mockmatchers.IgnoreArgument');
  51. /**
  52. * Provides asynchronous mocks and assertions controlled by a parent
  53. * MockControl.
  54. *
  55. * @param {goog.testing.MockControl} mockControl The parent MockControl.
  56. * @constructor
  57. * @final
  58. */
  59. goog.testing.async.MockControl = function(mockControl) {
  60. /**
  61. * The parent MockControl.
  62. * @type {goog.testing.MockControl}
  63. * @private
  64. */
  65. this.mockControl_ = mockControl;
  66. };
  67. /**
  68. * Returns a function that will assert that it will be called, and run the given
  69. * callback when it is.
  70. *
  71. * @param {string} name The name of the callback mock.
  72. * @param {function(...*) : *} callback The wrapped callback. This will be
  73. * called when the returned function is called.
  74. * @param {Object=} opt_selfObj The object which this should point to when the
  75. * callback is run.
  76. * @return {!Function} The mock callback.
  77. * @suppress {missingProperties} Mocks do not fit in the type system well.
  78. */
  79. goog.testing.async.MockControl.prototype.createCallbackMock = function(
  80. name, callback, opt_selfObj) {
  81. goog.asserts.assert(
  82. goog.isString(name),
  83. 'name parameter ' + goog.debug.deepExpose(name) + ' should be a string');
  84. var ignored = new goog.testing.mockmatchers.IgnoreArgument();
  85. // Use everyone's favorite "double-cast" trick to subvert the type system.
  86. var obj = /** @type {Object} */ (this.mockControl_.createFunctionMock(name));
  87. var fn = /** @type {Function} */ (obj);
  88. fn(ignored).$does(function(args) {
  89. if (opt_selfObj) {
  90. callback = goog.bind(callback, opt_selfObj);
  91. }
  92. return callback.apply(this, args);
  93. });
  94. fn.$replay();
  95. return function() { return fn(arguments); };
  96. };
  97. /**
  98. * Returns a function that will assert that its arguments are equal to the
  99. * arguments given to asyncAssertEquals. In addition, the function also asserts
  100. * that it will be called.
  101. *
  102. * @param {string} message A message to print if the arguments are wrong.
  103. * @param {...*} var_args The arguments to assert.
  104. * @return {function(...*) : void} The mock callback.
  105. */
  106. goog.testing.async.MockControl.prototype.asyncAssertEquals = function(
  107. message, var_args) {
  108. var expectedArgs = Array.prototype.slice.call(arguments, 1);
  109. return this.createCallbackMock('asyncAssertEquals', function() {
  110. assertObjectEquals(
  111. message, expectedArgs, Array.prototype.slice.call(arguments));
  112. });
  113. };
  114. /**
  115. * Asserts that a deferred object will have an error and call its errback
  116. * function.
  117. * @param {goog.async.Deferred} deferred The deferred object.
  118. * @param {function() : void} fn A function wrapping the code in which the error
  119. * will occur.
  120. */
  121. goog.testing.async.MockControl.prototype.assertDeferredError = function(
  122. deferred, fn) {
  123. deferred.addErrback(
  124. this.createCallbackMock('assertDeferredError', function() {}));
  125. goog.testing.asserts.callWithoutLogging(fn);
  126. };
  127. /**
  128. * Asserts that a deferred object will call its callback with the given value.
  129. *
  130. * @param {string} message A message to print if the arguments are wrong.
  131. * @param {goog.async.Deferred|*} expected The expected value. If this is a
  132. * deferred object, then the expected value is the deferred value.
  133. * @param {goog.async.Deferred|*} actual The actual value. If this is a deferred
  134. * object, then the actual value is the deferred value. Either this or
  135. * 'expected' must be deferred.
  136. */
  137. goog.testing.async.MockControl.prototype.assertDeferredEquals = function(
  138. message, expected, actual) {
  139. if (expected instanceof goog.async.Deferred &&
  140. actual instanceof goog.async.Deferred) {
  141. // Assert that the first deferred is resolved.
  142. expected.addCallback(
  143. this.createCallbackMock('assertDeferredEquals', function(exp) {
  144. // Assert that the second deferred is resolved, and that the value is
  145. // as expected.
  146. actual.addCallback(this.asyncAssertEquals(message, exp));
  147. }, this));
  148. } else if (expected instanceof goog.async.Deferred) {
  149. expected.addCallback(
  150. this.createCallbackMock('assertDeferredEquals', function(exp) {
  151. assertObjectEquals(message, exp, actual);
  152. }));
  153. } else if (actual instanceof goog.async.Deferred) {
  154. actual.addCallback(this.asyncAssertEquals(message, expected));
  155. } else {
  156. throw Error('Either expected or actual must be deferred');
  157. }
  158. };