aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/testing/loosemock_test.html
blob: bae04f359ddf253831aa9b465749f71da80d2e78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<!DOCTYPE html>
<html>
<!--
Copyright 2008 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.testing.LooseMock</title>
<script src="../base.js"></script>
<script>
  goog.require('goog.testing.LooseMock');
  goog.require('goog.testing.PropertyReplacer');
  goog.require('goog.testing.jsunit');
  goog.require('goog.testing.mockmatchers');
</script>
</head>
<body>
<script>
  // The object that we will be mocking
  var RealObject = function() {
  };

  RealObject.prototype.a = function() {
    fail('real object should never be called');
  };

  RealObject.prototype.b = function() {
    fail('real object should never be called');
  };

  var mock;

  var stubs;

  function setUp() {
    var obj = new RealObject();
    mock = new goog.testing.LooseMock(obj);
    stubs = new goog.testing.PropertyReplacer();
  }

  function tearDown() {
    stubs.reset();
  }

  /*
   * Calling this method evades the logTestFailure in
   * goog.testing.Mock.prototype.$recordAndThrow, so it doesn't look like the
   * test failed.
   */
  function silenceFailureLogging() {
    if (goog.global['G_testRunner']) {
      stubs.set(goog.global['G_testRunner'],
          'logTestFailure', goog.nullFunction);
    }
  }

  function unsilenceFailureLogging() {
    stubs.reset();
  }

  /**
   * Version of assertThrows that doesn't log the exception generated by the
   * mocks under test.
   * TODO: would be nice to check that a particular substring is in the thrown
   * message so we know it's not something dumb like a syntax error
   */
  function assertThrowsQuiet(var_args) {
    silenceFailureLogging();
    assertThrows.apply(null, arguments);
    unsilenceFailureLogging();
  }

  // Most of the basic functionality is tested in strictmock_test; these tests
  // cover the cases where loose mocks are different from strict mocks
  function testSimpleExpectations() {
    mock.a(5);
    mock.b();
    mock.$replay();
    mock.a(5);
    mock.b();
    mock.$verify();

    mock.$reset();

    mock.a();
    mock.b();
    mock.$replay();
    mock.b();
    mock.a();
    mock.$verify();

    mock.$reset();

    mock.a(5).$times(2);
    mock.a(5);
    mock.a(2);
    mock.$replay();
    mock.a(5);
    mock.a(5);
    mock.a(5);
    mock.a(2);
    mock.$verify();
  }


  function testMultipleExpectations() {
    mock.a().$returns(1);
    mock.a().$returns(2);
    mock.$replay();
    assertEquals(1, mock.a());
    assertEquals(2, mock.a());
    mock.$verify();
  }


  function testMultipleExpectationArgs() {
    mock.a('asdf').$anyTimes();
    mock.a('qwer').$anyTimes();
    mock.b().$times(3);
    mock.$replay();
    mock.a('asdf');
    mock.b();
    mock.a('asdf');
    mock.a('qwer');
    mock.b();
    mock.a('qwer');
    mock.b();
    mock.$verify();

    mock.$reset();

    mock.a('asdf').$anyTimes();
    mock.a('qwer').$anyTimes();
    mock.$replay();
    mock.a('asdf');
    mock.a('qwer');
    goog.bind(mock.a, mock, 'asdf');
    goog.bind(mock.$verify, mock);
  }

  function testSameMethodOutOfOrder() {
    mock.a('foo').$returns(1);
    mock.a('bar').$returns(2);
    mock.$replay();
    assertEquals(2, mock.a('bar'));
    assertEquals(1, mock.a('foo'));
  }

  function testSameMethodDifferentReturnValues() {
    mock.a('foo').$returns(1).$times(2);
    mock.a('foo').$returns(3);
    mock.a('bar').$returns(2);
    mock.$replay();
    assertEquals(1, mock.a('foo'));
    assertEquals(2, mock.a('bar'));
    assertEquals(1, mock.a('foo'));
    assertEquals(3, mock.a('foo'));
    assertThrowsQuiet(function() {
      mock.a('foo');
      mock.$verify();
    });
  }

  function testSameMethodBrokenExpectations() {
    // This is a weird corner case.
    // No way to ever make this verify no matter what you call after replaying,
    // because the second expectation of mock.a('foo') will be masked by
    // the first expectation that can be called any number of times, and so we
    // can never satisfy that second expectation.
    mock.a('foo').$returns(1).$anyTimes();
    mock.a('bar').$returns(2);
    mock.a('foo').$returns(3);

    // LooseMock can detect this case and fail on $replay.
    assertThrowsQuiet(goog.bind(mock.$replay, mock));
    mock.$reset();

    // This is a variant of the corner case above, but it's harder to determine
    // that the expectation to mock.a('bar') can never be satisfied. So we don't
    // fail on $replay, but we do fail on $verify.
    mock.a(goog.testing.mockmatchers.isString).$returns(1).$anyTimes();
    mock.a('bar').$returns(2);
    mock.$replay();

    assertEquals(1, mock.a('foo'));
    assertEquals(1, mock.a('bar'));
    assertThrowsQuiet(goog.bind(mock.$verify, mock));
  }

  function testSameMethodMultipleAnyTimes() {
    mock.a('foo').$returns(1).$anyTimes();
    mock.a('foo').$returns(2).$anyTimes();
    mock.$replay();
    assertEquals(1, mock.a('foo'));
    assertEquals(1, mock.a('foo'));
    assertEquals(1, mock.a('foo'));
    // Note we'll never return 2 but that's ok.
    mock.$verify();
  }

  function testFailingFast() {
    mock.a().$anyTimes();
    mock.$replay();
    mock.a();
    mock.a();
    assertThrowsQuiet(goog.bind(mock.b, mock));
    mock.$reset();

    // too many
    mock.a();
    mock.b();
    mock.$replay();
    mock.a();
    mock.b();

    var message;
    silenceFailureLogging();
    try {
      mock.a();
    } catch (e) {
      message = e.message;
    }
    unsilenceFailureLogging();

    assertTrue('No exception thrown on unexpected call', goog.isDef(message));
    assertContains('Too many calls to a', message);
  }

  function testTimes() {
    mock.a().$times(3);
    mock.b().$times(2);
    mock.$replay();
    mock.a();
    mock.b();
    mock.b();
    mock.a();
    mock.a();
    mock.$verify();
  }


  function testFailingSlow() {
    // not enough
    mock.a().$times(3);
    mock.$replay();
    mock.a();
    mock.a();
    assertThrowsQuiet(goog.bind(mock.$verify, mock));

    mock.$reset();

    // not enough, interleaved order
    mock.a().$times(3);
    mock.b().$times(3);
    mock.$replay();
    mock.a();
    mock.b();
    mock.a();
    mock.b();
    assertThrowsQuiet(goog.bind(mock.$verify, mock));

    mock.$reset();
    // bad args
    mock.a('asdf').$anyTimes();
    mock.$replay();
    mock.a('asdf');
    assertThrowsQuiet(goog.bind(mock.a, mock, 'qwert'));
    assertThrowsQuiet(goog.bind(mock.$verify, mock));
  }


  function testArgsAndReturns() {
    mock.a('asdf').$atLeastOnce().$returns(5);
    mock.b('qwer').$times(2).$returns(3);
    mock.$replay();
    assertEquals(5, mock.a('asdf'));
    assertEquals(3, mock.b('qwer'));
    assertEquals(5, mock.a('asdf'));
    assertEquals(5, mock.a('asdf'));
    assertEquals(3, mock.b('qwer'));
    mock.$verify();
  }


  function testThrows() {
    mock.a().$throws('exception!');
    mock.$replay();
    assertThrowsQuiet(goog.bind(mock.a, mock));
    mock.$verify();
  }


  function testDoes() {
    mock.a(1, 2).$does(function(a, b) {return a + b;});
    mock.$replay();
    assertEquals('Mock should call the function', 3, mock.a(1, 2));
    mock.$verify();
  }

  function testIgnoresExtraCalls() {
    mock = new goog.testing.LooseMock(RealObject, true);
    mock.a();
    mock.$replay();
    mock.a();
    mock.b(); // doesn't throw
    mock.$verify();
  }

  function testSkipAnyTimes() {
    mock = new goog.testing.LooseMock(RealObject);
    mock.a(1).$anyTimes();
    mock.a(2).$anyTimes();
    mock.a(3).$anyTimes();
    mock.$replay();
    mock.a(1);
    mock.a(3);
    mock.$verify();
  }

  function testErrorMessageForBadArgs() {
    mock.a();
    mock.$anyTimes();

    mock.$replay();

    var message;
    silenceFailureLogging();
    try {
      mock.a('a');
    } catch (e) {
      message = e.message;
    }
    unsilenceFailureLogging();

    assertTrue('No exception thrown on verify', goog.isDef(message));
    assertContains('Bad arguments to a()', message);
  }
</script>
</body>
</html>