mockclassfactory_test.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. goog.setTestOnly('goog.testing.MockClassFactoryTest');
  15. goog.require('goog.testing');
  16. goog.require('goog.testing.MockClassFactory');
  17. goog.require('goog.testing.jsunit');
  18. goog.provide('fake.BaseClass');
  19. goog.provide('fake.ChildClass');
  20. goog.provide('goog.testing.MockClassFactoryTest');
  21. // Classes that will be mocked. A base class and child class are used to
  22. // test inheritance.
  23. fake.BaseClass = function(a) {
  24. fail('real object should never be called');
  25. };
  26. fake.BaseClass.prototype.foo = function() {
  27. fail('real object should never be called');
  28. };
  29. fake.BaseClass.prototype.toString = function() {
  30. return 'foo';
  31. };
  32. fake.BaseClass.prototype.toLocaleString = function() {
  33. return 'bar';
  34. };
  35. fake.BaseClass.prototype.overridden = function() {
  36. return 42;
  37. };
  38. fake.ChildClass = function(a) {
  39. fail('real object should never be called');
  40. };
  41. goog.inherits(fake.ChildClass, fake.BaseClass);
  42. fake.ChildClass.staticFoo = function() {
  43. fail('real object should never be called');
  44. };
  45. fake.ChildClass.prototype.bar = function() {
  46. fail('real object should never be called');
  47. };
  48. fake.ChildClass.staticProperty = 'staticPropertyOnClass';
  49. function TopLevelBaseClass() {}
  50. fake.ChildClass.prototype.overridden = function() {
  51. var superResult = fake.ChildClass.base(this, 'overridden');
  52. if (superResult != 42) {
  53. fail('super method not invoked or returned wrong value');
  54. }
  55. return superResult + 1;
  56. };
  57. var mockClassFactory = new goog.testing.MockClassFactory();
  58. var matchers = goog.testing.mockmatchers;
  59. function tearDown() {
  60. mockClassFactory.reset();
  61. }
  62. function testGetStrictMockClass() {
  63. var mock1 = mockClassFactory.getStrictMockClass(fake, fake.BaseClass, 1);
  64. mock1.foo();
  65. mock1.$replay();
  66. var mock2 = mockClassFactory.getStrictMockClass(fake, fake.BaseClass, 2);
  67. mock2.foo();
  68. mock2.$replay();
  69. var mock3 = mockClassFactory.getStrictMockClass(fake, fake.ChildClass, 3);
  70. mock3.foo();
  71. mock3.bar();
  72. mock3.$replay();
  73. var instance1 = new fake.BaseClass(1);
  74. instance1.foo();
  75. mock1.$verify();
  76. var instance2 = new fake.BaseClass(2);
  77. instance2.foo();
  78. mock2.$verify();
  79. var instance3 = new fake.ChildClass(3);
  80. instance3.foo();
  81. instance3.bar();
  82. mock3.$verify();
  83. assertThrows(function() { new fake.BaseClass(-1) });
  84. assertTrue(instance1 instanceof fake.BaseClass);
  85. assertTrue(instance2 instanceof fake.BaseClass);
  86. assertTrue(instance3 instanceof fake.ChildClass);
  87. }
  88. function testGetStrictMockClassCreatesAllProxies() {
  89. var mock1 = mockClassFactory.getStrictMockClass(fake, fake.BaseClass, 1);
  90. // toString(), toLocaleString() and others are treaded specially in
  91. // createProxy_().
  92. mock1.toString();
  93. mock1.toLocaleString();
  94. mock1.$replay();
  95. var instance1 = new fake.BaseClass(1);
  96. instance1.toString();
  97. instance1.toLocaleString();
  98. mock1.$verify();
  99. }
  100. function testGetLooseMockClass() {
  101. var mock1 = mockClassFactory.getLooseMockClass(fake, fake.BaseClass, 1);
  102. mock1.foo().$anyTimes().$returns(3);
  103. mock1.$replay();
  104. var mock2 = mockClassFactory.getLooseMockClass(fake, fake.BaseClass, 2);
  105. mock2.foo().$times(3);
  106. mock2.$replay();
  107. var mock3 = mockClassFactory.getLooseMockClass(fake, fake.ChildClass, 3);
  108. mock3.foo().$atLeastOnce().$returns(5);
  109. mock3.bar().$atLeastOnce();
  110. mock3.$replay();
  111. var instance1 = new fake.BaseClass(1);
  112. assertEquals(3, instance1.foo());
  113. assertEquals(3, instance1.foo());
  114. assertEquals(3, instance1.foo());
  115. assertEquals(3, instance1.foo());
  116. assertEquals(3, instance1.foo());
  117. mock1.$verify();
  118. var instance2 = new fake.BaseClass(2);
  119. instance2.foo();
  120. instance2.foo();
  121. instance2.foo();
  122. mock2.$verify();
  123. var instance3 = new fake.ChildClass(3);
  124. assertEquals(5, instance3.foo());
  125. assertEquals(5, instance3.foo());
  126. instance3.bar();
  127. mock3.$verify();
  128. assertThrows(function() { new fake.BaseClass(-1) });
  129. assertTrue(instance1 instanceof fake.BaseClass);
  130. assertTrue(instance2 instanceof fake.BaseClass);
  131. assertTrue(instance3 instanceof fake.ChildClass);
  132. }
  133. function testGetStrictStaticMock() {
  134. var staticMock = mockClassFactory.getStrictStaticMock(fake, fake.ChildClass);
  135. var mock = mockClassFactory.getStrictMockClass(fake, fake.ChildClass, 1);
  136. mock.foo();
  137. mock.bar();
  138. staticMock.staticFoo();
  139. mock.$replay();
  140. staticMock.$replay();
  141. var instance = new fake.ChildClass(1);
  142. instance.foo();
  143. instance.bar();
  144. fake.ChildClass.staticFoo();
  145. mock.$verify();
  146. staticMock.$verify();
  147. assertTrue(instance instanceof fake.BaseClass);
  148. assertTrue(instance instanceof fake.ChildClass);
  149. assertThrows(function() {
  150. mockClassFactory.getLooseStaticMock(fake, fake.ChildClass);
  151. });
  152. }
  153. function testGetStrictStaticMockKeepsStaticProperties() {
  154. var OriginalChildClass = fake.ChildClass;
  155. var staticMock = mockClassFactory.getStrictStaticMock(fake, fake.ChildClass);
  156. assertEquals(
  157. OriginalChildClass.staticProperty, fake.ChildClass.staticProperty);
  158. }
  159. function testGetLooseStaticMockKeepsStaticProperties() {
  160. var OriginalChildClass = fake.ChildClass;
  161. var staticMock = mockClassFactory.getLooseStaticMock(fake, fake.ChildClass);
  162. assertEquals(
  163. OriginalChildClass.staticProperty, fake.ChildClass.staticProperty);
  164. }
  165. function testGetLooseStaticMock() {
  166. var staticMock = mockClassFactory.getLooseStaticMock(fake, fake.ChildClass);
  167. var mock = mockClassFactory.getStrictMockClass(fake, fake.ChildClass, 1);
  168. mock.foo();
  169. mock.bar();
  170. staticMock.staticFoo().$atLeastOnce();
  171. mock.$replay();
  172. staticMock.$replay();
  173. var instance = new fake.ChildClass(1);
  174. instance.foo();
  175. instance.bar();
  176. fake.ChildClass.staticFoo();
  177. fake.ChildClass.staticFoo();
  178. mock.$verify();
  179. staticMock.$verify();
  180. assertTrue(instance instanceof fake.BaseClass);
  181. assertTrue(instance instanceof fake.ChildClass);
  182. assertThrows(function() {
  183. mockClassFactory.getStrictStaticMock(fake, fake.ChildClass);
  184. });
  185. }
  186. function testFlexibleClassMockInstantiation() {
  187. // This mock should be returned for all instances created with a number
  188. // as the first argument.
  189. var mock = mockClassFactory.getStrictMockClass(
  190. fake, fake.ChildClass, matchers.isNumber);
  191. mock.foo(); // Will be called by the first mock instance.
  192. mock.foo(); // Will be called by the second mock instance.
  193. mock.$replay();
  194. var instance1 = new fake.ChildClass(1);
  195. var instance2 = new fake.ChildClass(2);
  196. instance1.foo();
  197. instance2.foo();
  198. assertThrows(function() { new fake.ChildClass('foo'); });
  199. mock.$verify();
  200. }
  201. function testMockTopLevelClass() {
  202. var mock = mockClassFactory.getStrictMockClass(
  203. goog.global, goog.global.TopLevelBaseClass);
  204. }
  205. function testGoogBaseCall() {
  206. var overriddenFn = fake.ChildClass.prototype.overridden;
  207. var mock = mockClassFactory.getLooseMockClass(fake, fake.ChildClass, 1);
  208. var instance1 = new fake.ChildClass(1);
  209. assertTrue(43 == overriddenFn.call(instance1));
  210. }