strictmock_test.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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.provide('goog.testing.StrictMockTest');
  15. goog.setTestOnly('goog.testing.StrictMockTest');
  16. goog.require('goog.testing.StrictMock');
  17. goog.require('goog.testing.jsunit');
  18. // The object that we will be mocking
  19. var RealObject = function() {};
  20. RealObject.prototype.a = function() {
  21. fail('real object should never be called');
  22. };
  23. RealObject.prototype.b = function() {
  24. fail('real object should never be called');
  25. };
  26. RealObject.prototype.c = function() {
  27. fail('real object should never be called');
  28. };
  29. var mock;
  30. function setUp() {
  31. var obj = new RealObject();
  32. mock = new goog.testing.StrictMock(obj);
  33. }
  34. function testMockFunction() {
  35. var mock = new goog.testing.StrictMock(RealObject);
  36. mock.a();
  37. mock.b();
  38. mock.c();
  39. mock.$replay();
  40. mock.a();
  41. mock.b();
  42. mock.c();
  43. mock.$verify();
  44. mock.$reset();
  45. assertThrows(function() { mock.x() });
  46. }
  47. function testSimpleExpectations() {
  48. mock.a();
  49. mock.$replay();
  50. mock.a();
  51. mock.$verify();
  52. mock.$reset();
  53. mock.a();
  54. mock.b();
  55. mock.a();
  56. mock.a();
  57. mock.$replay();
  58. mock.a();
  59. mock.b();
  60. mock.a();
  61. mock.a();
  62. mock.$verify();
  63. }
  64. function testFailToSetExpectation() {
  65. mock.$replay();
  66. assertThrowsJsUnitException(goog.bind(mock.a, mock));
  67. mock.$reset();
  68. mock.$replay();
  69. assertThrowsJsUnitException(goog.bind(mock.b, mock));
  70. }
  71. function testUnexpectedCall() {
  72. mock.a();
  73. mock.$replay();
  74. mock.a();
  75. assertThrowsJsUnitException(goog.bind(mock.a, mock));
  76. mock.$reset();
  77. mock.a();
  78. mock.$replay();
  79. assertThrowsJsUnitException(goog.bind(mock.b, mock));
  80. }
  81. function testNotEnoughCalls() {
  82. mock.a();
  83. mock.$replay();
  84. assertThrowsJsUnitException(goog.bind(mock.$verify, mock));
  85. mock.$reset();
  86. mock.a();
  87. mock.b();
  88. mock.$replay();
  89. mock.a();
  90. assertThrowsJsUnitException(goog.bind(mock.$verify, mock));
  91. }
  92. function testOutOfOrderCalls() {
  93. mock.a();
  94. mock.b();
  95. mock.$replay();
  96. assertThrowsJsUnitException(goog.bind(mock.b, mock));
  97. }
  98. function testVerify() {
  99. mock.a();
  100. mock.$replay();
  101. mock.a();
  102. mock.$verify();
  103. mock.$reset();
  104. mock.a();
  105. mock.$replay();
  106. assertThrowsJsUnitException(goog.bind(mock.$verify, mock));
  107. }
  108. function testArgumentMatching() {
  109. mock.a('foo');
  110. mock.b('bar');
  111. mock.$replay();
  112. mock.a('foo');
  113. assertThrowsJsUnitException(function() { mock.b('foo') });
  114. mock.$reset();
  115. mock.a('foo');
  116. mock.a('bar');
  117. mock.$replay();
  118. mock.a('foo');
  119. mock.a('bar');
  120. mock.$verify();
  121. mock.$reset();
  122. mock.a('foo');
  123. mock.a('bar');
  124. mock.$replay();
  125. assertThrowsJsUnitException(function() { mock.a('bar') });
  126. }
  127. function testReturnValue() {
  128. mock.a().$returns(5);
  129. mock.$replay();
  130. assertEquals('Mock should return the right value', 5, mock.a());
  131. mock.$verify();
  132. }
  133. function testMultipleReturnValues() {
  134. mock.a().$returns(3);
  135. mock.a().$returns(2);
  136. mock.a().$returns(1);
  137. mock.$replay();
  138. assertArrayEquals(
  139. 'Mock should return the right value sequence', [3, 2, 1],
  140. [mock.a(), mock.a(), mock.a()]);
  141. mock.$verify();
  142. }
  143. function testAtMostOnce() {
  144. // Zero times SUCCESS.
  145. mock.a().$atMostOnce();
  146. mock.$replay();
  147. mock.$verify();
  148. mock.$reset();
  149. // One time SUCCESS.
  150. mock.a().$atMostOnce();
  151. mock.$replay();
  152. mock.a();
  153. mock.$verify();
  154. mock.$reset();
  155. // Many times FAIL.
  156. mock.a().$atMostOnce();
  157. mock.$replay();
  158. mock.a();
  159. assertThrowsJsUnitException(goog.bind(mock.a, mock));
  160. mock.$reset();
  161. // atMostOnce only lasts until a new method is called.
  162. mock.a().$atMostOnce();
  163. mock.b();
  164. mock.a();
  165. mock.$replay();
  166. mock.b();
  167. assertThrowsJsUnitException(goog.bind(mock.$verify, mock));
  168. }
  169. function testAtLeastOnce() {
  170. // atLeastOnce does not mean zero times
  171. mock.a().$atLeastOnce();
  172. mock.$replay();
  173. assertThrowsJsUnitException(goog.bind(mock.$verify, mock));
  174. mock.$reset();
  175. // atLeastOnce does mean three times
  176. mock.a().$atLeastOnce();
  177. mock.$replay();
  178. mock.a();
  179. mock.a();
  180. mock.a();
  181. mock.$verify();
  182. mock.$reset();
  183. // atLeastOnce only lasts until a new method is called
  184. mock.a().$atLeastOnce();
  185. mock.b();
  186. mock.a();
  187. mock.$replay();
  188. mock.a();
  189. mock.a();
  190. mock.b();
  191. mock.a();
  192. assertThrowsJsUnitException(goog.bind(mock.a, mock));
  193. }
  194. function testAtLeastOnceWithArgs() {
  195. mock.a('asdf').$atLeastOnce();
  196. mock.a('qwert');
  197. mock.$replay();
  198. mock.a('asdf');
  199. mock.a('asdf');
  200. mock.a('qwert');
  201. mock.$verify();
  202. mock.$reset();
  203. mock.a('asdf').$atLeastOnce();
  204. mock.a('qwert');
  205. mock.$replay();
  206. mock.a('asdf');
  207. mock.a('asdf');
  208. assertThrowsJsUnitException(function() { mock.a('zxcv') });
  209. assertThrowsJsUnitException(goog.bind(mock.$verify, mock));
  210. }
  211. function testAnyTimes() {
  212. mock.a().$anyTimes();
  213. mock.$replay();
  214. mock.$verify();
  215. mock.$reset();
  216. mock.a().$anyTimes();
  217. mock.$replay();
  218. mock.a();
  219. mock.a();
  220. mock.a();
  221. mock.a();
  222. mock.a();
  223. mock.$verify();
  224. }
  225. function testAnyTimesWithArguments() {
  226. mock.a('foo').$anyTimes();
  227. mock.$replay();
  228. mock.$verify();
  229. mock.$reset();
  230. mock.a('foo').$anyTimes();
  231. mock.a('bar').$anyTimes();
  232. mock.$replay();
  233. mock.a('foo');
  234. mock.a('foo');
  235. mock.a('foo');
  236. mock.a('bar');
  237. mock.a('bar');
  238. mock.$verify();
  239. }
  240. function testZeroTimes() {
  241. mock.a().$times(0);
  242. mock.$replay();
  243. mock.$verify();
  244. mock.$reset();
  245. mock.a().$times(0);
  246. mock.$replay();
  247. assertThrowsJsUnitException(function() { mock.a() });
  248. }
  249. function testZeroTimesWithArguments() {
  250. mock.a('foo').$times(0);
  251. mock.$replay();
  252. mock.$verify();
  253. mock.$reset();
  254. mock.a('foo').$times(0);
  255. mock.$replay();
  256. assertThrowsJsUnitException(function() { mock.a('foo') });
  257. }
  258. function testTooManyCalls() {
  259. mock.a().$times(2);
  260. mock.$replay();
  261. mock.a();
  262. mock.a();
  263. assertThrowsJsUnitException(function() { mock.a() });
  264. }
  265. function testTooManyCallsWithArguments() {
  266. mock.a('foo').$times(2);
  267. mock.$replay();
  268. mock.a('foo');
  269. mock.a('foo');
  270. assertThrowsJsUnitException(function() { mock.a('foo') });
  271. }
  272. function testMultipleSkippedAnyTimes() {
  273. mock.a().$anyTimes();
  274. mock.b().$anyTimes();
  275. mock.c().$anyTimes();
  276. mock.$replay();
  277. mock.c();
  278. mock.$verify();
  279. }
  280. function testMultipleSkippedAnyTimesWithArguments() {
  281. mock.a('foo').$anyTimes();
  282. mock.a('bar').$anyTimes();
  283. mock.a('baz').$anyTimes();
  284. mock.$replay();
  285. mock.a('baz');
  286. mock.$verify();
  287. }
  288. function testVerifyThrows() {
  289. mock.a(1);
  290. mock.$replay();
  291. mock.a(1);
  292. try {
  293. mock.a(2);
  294. fail('bad mock, should fail');
  295. } catch (ex) {
  296. // this could be an event handler, for example
  297. }
  298. assertThrowsJsUnitException(goog.bind(mock.$verify, mock));
  299. }
  300. function testThrows() {
  301. mock.a().$throws('exception!');
  302. mock.$replay();
  303. assertThrows(goog.bind(mock.a, mock));
  304. mock.$verify();
  305. }
  306. function testDoes() {
  307. mock.a(1, 2).$does(function(a, b) { return a + b; });
  308. mock.$replay();
  309. assertEquals('Mock should call the function', 3, mock.a(1, 2));
  310. mock.$verify();
  311. }
  312. function testErrorMessageForBadArgs() {
  313. mock.a();
  314. mock.$anyTimes();
  315. mock.$replay();
  316. var message;
  317. try {
  318. mock.a('a');
  319. } catch (e) {
  320. message = e.message;
  321. }
  322. assertTrue('No exception thrown on verify', goog.isDef(message));
  323. assertContains('Bad arguments to a()', message);
  324. }