aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/testing/mock_test.html
blob: 2e8c6ec9d09832ffd64de930e9c9efc81fe958a0 (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
<!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.Mock</title>
<script src="../base.js"></script>
<script>
  goog.require('goog.array');
  goog.require('goog.object');
  goog.require('goog.testing.Mock');
  goog.require('goog.testing.MockControl');
  goog.require('goog.testing.MockExpectation');
  goog.require('goog.testing.jsunit');
</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 matchers = goog.testing.mockmatchers;
  var mock;

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

  function testMockErrorMessage() {
    var expectation = new goog.testing.MockExpectation('a');
    assertEquals(0, expectation.getErrorMessageCount());
    assertEquals('', expectation.getErrorMessage());

    expectation.addErrorMessage('foo failed');
    assertEquals(1, expectation.getErrorMessageCount());
    assertEquals('foo failed', expectation.getErrorMessage());

    expectation.addErrorMessage('bar failed');
    assertEquals(2, expectation.getErrorMessageCount());
    assertEquals('foo failed\nbar failed', expectation.getErrorMessage());
  }

  function testVerifyArgumentList() {
    var expectation = new goog.testing.MockExpectation('a');
    assertEquals('', expectation.getErrorMessage());

    // test single string arg
    expectation.argumentList = ['foo'];
    assertTrue(mock.$verifyCall(expectation, 'a', ['foo']));

    // single numeric arg
    expectation.argumentList = [2]
    assertTrue(mock.$verifyCall(expectation, 'a', [2]));

    // single object arg (using standard === comparison)
    var obj = {prop1: 'prop1', prop2: 2};
    expectation.argumentList = [obj];
    assertTrue(mock.$verifyCall(expectation, 'a', [obj]));

    // make sure comparison succeeds if args are similar, but not ===
    var obj2 = {prop1: 'prop1', prop2: 2};
    expectation.argumentList = [obj];
    assertTrue(mock.$verifyCall(expectation, 'a', [obj2]));
    assertEquals('', expectation.getErrorMessage());

    // multiple args
    expectation.argumentList = ['foo', 2, obj, obj2];
    assertTrue(mock.$verifyCall(expectation, 'a', ['foo', 2, obj, obj2]));

    // test flexible arg matching.
    expectation.argumentList = ['foo', matchers.isNumber];
    assertTrue(mock.$verifyCall(expectation, 'a', ['foo', 1]));

    expectation.argumentList = [new matchers.InstanceOf(RealObject)];
    assertTrue(mock.$verifyCall(expectation, 'a', [new RealObject()]));
  }

  function testVerifyArgumentListForObjectMethods() {
    var expectation = new goog.testing.MockExpectation('toString');
    expectation.argumentList = []
    assertTrue(mock.$verifyCall(expectation, 'toString', []));
  }

  function testRegisterArgumentListVerifier() {
    var expectationA = new goog.testing.MockExpectation('a');
    var expectationB = new goog.testing.MockExpectation('b');

    // Simple matcher that return true if all args are === equivalent.
    mock.$registerArgumentListVerifier('a', function(expectedArgs, args) {
      return goog.array.equals(expectedArgs, args, function(a, b) {
        return (a === b);
      });
    });

    // test single string arg
    expectationA.argumentList = ['foo'];
    assertTrue(mock.$verifyCall(expectationA, 'a', ['foo']));

    // single numeric arg
    expectationA.argumentList = [2]
    assertTrue(mock.$verifyCall(expectationA, 'a', [2]));

    // single object arg (using standard === comparison)
    var obj = {prop1: 'prop1', prop2: 2};
    expectationA.argumentList = [obj];
    expectationB.argumentList = [obj];
    assertTrue(mock.$verifyCall(expectationA, 'a', [obj]));
    assertTrue(mock.$verifyCall(expectationB, 'b', [obj]));

    // if args are similar, but not ===, then comparison should succeed
    // for method with registered object matcher, and fail for method without
    var obj2 = {prop1: 'prop1', prop2: 2};
    expectationA.argumentList = [obj];
    expectationB.argumentList = [obj];
    assertFalse(mock.$verifyCall(expectationA, 'a', [obj2]))
    assertTrue(mock.$verifyCall(expectationB, 'b', [obj2]));


    // multiple args, should fail for method with registered arg matcher,
    // and succeed for method without.
    expectationA.argumentList = ['foo', 2, obj, obj2];
    expectationB.argumentList = ['foo', 2, obj, obj2];
    assertFalse(mock.$verifyCall(expectationA, 'a', ['foo', 2, obj2, obj]));
    assertTrue(mock.$verifyCall(expectationB, 'b', ['foo', 2, obj2, obj]));
  }


  function testCreateProxy() {
    mock = new goog.testing.Mock(RealObject, false, true);
    assertTrue(mock.$proxy instanceof RealObject);
    assertThrows(function() {
      new goog.testing.Mock(RealObject, true, true);
    });
    assertThrows(function() {
      new goog.testing.Mock(1, false, true);
    });
  }


  function testValidConstructorArgument() {
    var someNamespace = { };
    assertThrows(function() {
        new goog.testing.Mock(someNamespace.RealObjectWithTypo);
    });
  }


  function testArgumentsAsString() {
    assertEquals('()', mock.$argumentsAsString([]));
    assertEquals('(string, number, object, null)',
                 mock.$argumentsAsString(['red', 1, {}, null]));
  }


  function testThrowCallExceptionBadArgs() {
    var msg;
    mock.$throwException = function(m) {
      msg = m;
    };

    mock.$throwCallException(
        'fn1', [ 'b' ],
        { name: 'fn1',
          argumentList: [ 'c' ],
          getErrorMessage: function() { return ''; } });
    assertContains(
        'Bad arguments to fn1().\nActual: (string)\nExpected: (string)', msg);
  }

  function testThrowCallExceptionUnexpected() {
    var msg;
    mock.$throwException = function(m) {
      msg = m;
    };

    mock.$throwCallException('fn1', [ 'b' ]);
    assertEquals('Unexpected call to fn1(string).', msg);
  }

  function testThrowCallExceptionUnexpectedWithNext() {
    var msg;
    mock.$throwException = function(m) {
      msg = m;
    };

    mock.$throwCallException(
        'fn1', [ 'b' ],
        { name: 'fn2',
          argumentList: [ 3 ],
          getErrorMessage: function() { return ''; } });
    assertEquals(
        'Unexpected call to fn1(string).\n' +
        'Next expected call was to fn2(number)', msg);
  }

  // This tests that base Object functions which are not enumerable in IE can
  // be mocked correctly.
  function testBindNonEnumerableFunctions() {
    // Create Foo and override non enumerable functions.
    var Foo = function() {};
    Foo.prototype.constructor = function() {
      fail('real object should never be called');
    };
    Foo.prototype.hasOwnProperty = function() {
      fail('real object should never be called');
    };
    Foo.prototype.isPrototypeOf = function() {
      fail('real object should never be called');
    };
    Foo.prototype.propertyIsEnumerable = function() {
      fail('real object should never be called');
    };
    Foo.prototype.toLocaleString = function() {
      fail('real object should never be called');
    };
    Foo.prototype.toString = function() {
      fail('real object should never be called');
    };
    Foo.prototype.valueOf = function() {
      fail('real object should never be called');
    };

    // Create Mock and set $returns for toString.
    var mockControl = new goog.testing.MockControl();
    var mock = mockControl.createLooseMock(Foo);
    mock.constructor().$returns('constructor');
    mock.hasOwnProperty().$returns('hasOwnProperty');
    mock.isPrototypeOf().$returns('isPrototypeOf');
    mock.propertyIsEnumerable().$returns('propertyIsEnumerable');
    mock.toLocaleString().$returns('toLocaleString');
    mock.toString().$returns('toString');
    mock.valueOf().$returns('valueOf');

    // Execute and assert that the Mock is working correctly.
    mockControl.$replayAll();
    assertEquals('constructor', mock.constructor());
    assertEquals('hasOwnProperty', mock.hasOwnProperty());
    assertEquals('isPrototypeOf', mock.isPrototypeOf());
    assertEquals('propertyIsEnumerable', mock.propertyIsEnumerable());
    assertEquals('toLocaleString', mock.toLocaleString());
    assertEquals('toString', mock.toString());
    assertEquals('valueOf', mock.valueOf());
    mockControl.$verifyAll();
  }

</script>
</body>
</html>