Debian Codegraph @

closure-library/closure/goog/testing/mockcontrol_test.js

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