aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/labs/mock/mock_test.html
blob: 93a39843b2815a5ffc5a5c9e6506f693c53f1682 (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
<!DOCTYPE html>
<html>
<!--
Copyright 2012 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>
<title>Closure Unit Tests - goog.labs.mock</title>
<script src="../../base.js"></script>
<script>

goog.require('goog.labs.mock');
goog.require('goog.testing.jsunit');

</script>
</head>
<body>
<script>

var ParentClass = function() {};
ParentClass.prototype.method1 = function() {};
ParentClass.prototype.x = 1;

var ChildClass = function() {};
goog.inherits(ChildClass, ParentClass);
ChildClass.prototype.method2 = function() {};
ChildClass.prototype.y = 2;

function testParentClass() {
  var parentMock = goog.labs.mock(ParentClass);

  assertNotUndefined(parentMock.method1);
  assertUndefined(parentMock.method1());
  assertUndefined(parentMock.method2);
  assertUndefined(parentMock.x);
  assertUndefined(parentMock.y);
}

function testChildClass() {
  var childMock = goog.labs.mock(ChildClass);

  assertNotUndefined(childMock.method1);
  assertUndefined(childMock.method1());
  assertNotUndefined(childMock.method2);
  assertUndefined(childMock.method2());
  assertUndefined(childMock.x);
  assertUndefined(childMock.y);
}

function testParentClassInstance() {
  var parentMock = goog.labs.mock(new ParentClass());

  assertNotUndefined(parentMock.method1);
  assertUndefined(parentMock.method1());
  assertUndefined(parentMock.method2);
  assertUndefined(parentMock.x);
  assertUndefined(parentMock.y);
}

function testChildClassInstance() {
  var childMock = goog.labs.mock(new ChildClass());

  assertNotUndefined(childMock.method1);
  assertUndefined(childMock.method1());
  assertNotUndefined(childMock.method2);
  assertUndefined(childMock.method2());
  assertUndefined(childMock.x);
  assertUndefined(childMock.y);
}

function testNonEnumerableProperties() {
  var mockObject = goog.labs.mock({});
  assertNotUndefined(mockObject.toString);
  goog.labs.mock.when(mockObject).toString().then(function() {
    return 'toString';
  });
  assertEquals('toString', mockObject.toString());
}

function testBasicStubbing() {
  var obj = {
    method1: function(i) {
      return 2 * i;
    },
    method2: function(i, str) {
      return str;
    },
    method3: function(x) {
      return x;
    }
  };

  var mockObj = goog.labs.mock(obj);
  goog.labs.mock.when(mockObj).method1(2).then(function(i) {return i;});

  assertEquals(4, obj.method1(2));
  assertEquals(2, mockObj.method1(2));
  assertUndefined(mockObj.method1(4));

  goog.labs.mock.when(mockObj).method2(1, 'hi').then(function(i) {return 'oh'});
  assertEquals('hi', obj.method2(1, 'hi'));
  assertEquals('oh', mockObj.method2(1, 'hi'));
  assertUndefined(mockObj.method2(3, 'foo'));

  goog.labs.mock.when(mockObj).method3(4).thenReturn(10);
  assertEquals(4, obj.method3(4));
  assertEquals(10, mockObj.method3(4));
  assertUndefined(mockObj.method3(5));
}

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