mockcontrol_test.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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.MockControlTest');
  15. goog.setTestOnly('goog.testing.MockControlTest');
  16. goog.require('goog.testing.Mock');
  17. goog.require('goog.testing.MockControl');
  18. goog.require('goog.testing.jsunit');
  19. // Emulate the behavior of a mock.
  20. function MockMock() {
  21. this.replayCalled = false;
  22. this.resetCalled = false;
  23. this.verifyCalled = false;
  24. this.tearDownCalled = false;
  25. }
  26. MockMock.prototype.$replay = function() {
  27. this.replayCalled = true;
  28. };
  29. MockMock.prototype.$reset = function() {
  30. this.resetCalled = true;
  31. };
  32. MockMock.prototype.$verify = function() {
  33. this.verifyCalled = true;
  34. };
  35. function setUp() {
  36. var mock = new goog.testing.Mock(MockMock);
  37. }
  38. function testAdd() {
  39. var mockMock = new MockMock();
  40. var control = new goog.testing.MockControl();
  41. assertEquals(mockMock, control.addMock(mockMock));
  42. }
  43. function testReplayAll() {
  44. var mockMock1 = new MockMock();
  45. var mockMock2 = new MockMock();
  46. var mockMockExcluded = new MockMock();
  47. var control = new goog.testing.MockControl();
  48. control.addMock(mockMock1);
  49. control.addMock(mockMock2);
  50. control.$replayAll();
  51. assertTrue(mockMock1.replayCalled);
  52. assertTrue(mockMock2.replayCalled);
  53. assertFalse(mockMockExcluded.replayCalled);
  54. }
  55. function testResetAll() {
  56. var mockMock1 = new MockMock();
  57. var mockMock2 = new MockMock();
  58. var mockMockExcluded = new MockMock();
  59. var control = new goog.testing.MockControl();
  60. control.addMock(mockMock1);
  61. control.addMock(mockMock2);
  62. control.$resetAll();
  63. assertTrue(mockMock1.resetCalled);
  64. assertTrue(mockMock2.resetCalled);
  65. assertFalse(mockMockExcluded.resetCalled);
  66. }
  67. function testVerifyAll() {
  68. var mockMock1 = new MockMock();
  69. var mockMock2 = new MockMock();
  70. var mockMockExcluded = new MockMock();
  71. var control = new goog.testing.MockControl();
  72. control.addMock(mockMock1);
  73. control.addMock(mockMock2);
  74. control.$verifyAll();
  75. assertTrue(mockMock1.verifyCalled);
  76. assertTrue(mockMock2.verifyCalled);
  77. assertFalse(mockMockExcluded.verifyCalled);
  78. }
  79. function testTearDownAll() {
  80. var mockMock1 = new MockMock();
  81. var mockMock2 = new MockMock();
  82. var mockMockExcluded = new MockMock();
  83. // $tearDown is optional.
  84. mockMock2.$tearDown = function() { this.tearDownCalled = true; };
  85. mockMockExcluded.$tearDown = function() { this.tearDownCalled = true; };
  86. var control = new goog.testing.MockControl();
  87. control.addMock(mockMock1);
  88. control.addMock(mockMock2);
  89. control.$tearDown();
  90. // mockMock2 has a tearDown method and is in the control.
  91. assertTrue(mockMock2.tearDownCalled);
  92. assertFalse(mockMock1.tearDownCalled);
  93. assertFalse(mockMockExcluded.tearDownCalled);
  94. }