~t7-vla7-lz/psiphon/psiphon

« back to all changes in this revision

Viewing changes to trunk/testing/selenium_scripts/selenium/unittest/jsmock/mock-tests.html

  • Committer: Eugene Fryntov
  • Date: 2016-11-15 22:13:59 UTC
  • mfrom: (373.1.1 psiphon)
  • Revision ID: e.fryntov@psiphon.ca-20161115221359-f6s56ue1a54n4ijj
merged lp:~t7-vla7-lz/psiphon/psiphon @ 374

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
2
 
        "http://www.w3.org/TR/html4/loose.dtd">
3
 
 
4
 
<!--
5
 
Copyright 2004 ThoughtWorks, Inc
6
 
 
7
 
 Licensed under the Apache License, Version 2.0 (the "License");
8
 
 you may not use this file except in compliance with the License.
9
 
 You may obtain a copy of the License at
10
 
 
11
 
     http://www.apache.org/licenses/LICENSE-2.0
12
 
 
13
 
 Unless required by applicable law or agreed to in writing, software
14
 
 distributed under the License is distributed on an "AS IS" BASIS,
15
 
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 
 See the License for the specific language governing permissions and
17
 
 limitations under the License.
18
 
-->
19
 
<html>
20
 
<head>
21
 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
22
 
<title>JsMock Tests</title>
23
 
<link rel="stylesheet" type="text/css" href="../../jsunit/css/jsUnitStyle.css">
24
 
<script language="JavaScript" type="text/javascript" src="../../jsunit/app/jsUnitCore.js"></script>
25
 
<script language="JavaScript" type="text/javascript" src="mock.js"></script>
26
 
<script language="JavaScript" type="text/javascript">
27
 
 
28
 
function testCallingExpectedFunctionShouldPass() {
29
 
    var myMock = new Mock()
30
 
    myMock.expects("aslak")
31
 
    myMock.aslak()
32
 
    myMock.verify()
33
 
}
34
 
 
35
 
function testAccessingExpectedPropertyShouldPass() {
36
 
    var myMock = new Mock()
37
 
    myMock.expectsProperty("hello").returns("world")
38
 
    assertEquals("world", myMock.hello)
39
 
}
40
 
 
41
 
function testAccessingExpectedPropertyWithObjectShouldPass() {
42
 
    var myMock = new Mock()
43
 
    ob = [1,2]
44
 
    myMock.expectsProperty("hello").returns(ob)
45
 
    assertEquals(ob, myMock.hello)
46
 
}
47
 
 
48
 
function testCallingUnexpectedFunctionShouldFail() {
49
 
    var myMock = new Mock();
50
 
    try {
51
 
        myMock.someMethod();
52
 
        fail("Should fail because someMethod wasn't expected!");
53
 
    } catch(expected) {
54
 
        // pass
55
 
    }
56
 
}
57
 
 
58
 
function testNotCallingExpectedFunctionShouldFail() {
59
 
    var myMock = new Mock()
60
 
    myMock.expects("someMethod")
61
 
    try {
62
 
        myMock.verify()
63
 
        fail("Should fail because someMethod wasn't called!")
64
 
    } catch(expected) {
65
 
        // pass
66
 
    }
67
 
}
68
 
 
69
 
function testCallingTooManyExpectedFunctionShouldFail() {
70
 
    var myMock = new Mock()
71
 
    myMock.expects("someMethod")
72
 
    try {
73
 
        myMock.someMethod();
74
 
        myMock.someMethod();
75
 
        myMock.verify()
76
 
        fail("Should fail because someMethod was called more than once!")
77
 
    } catch(expected) {
78
 
        // pass
79
 
    }
80
 
}
81
 
 
82
 
function testCallingExpectedFunctionWithBadArgumentsShouldFail() {
83
 
    var myMock = new Mock()
84
 
    myMock.expects("someMethod", "foo")
85
 
    try {
86
 
        myMock.someMethod("bar")
87
 
    } catch(expected) {
88
 
        return
89
 
    }
90
 
    fail("Should fail because bar wasn't the expected arg!")
91
 
}
92
 
 
93
 
function testCallingExpectedFunctionWithExpectedArgumentsShouldPass() {
94
 
    var myMock = new Mock()
95
 
    myMock.expects("someMethod", "foo")
96
 
    myMock.expects("anotherMethod", "bar", "zap")
97
 
    assertUndefined(myMock.someMethod("foo"))
98
 
    assertUndefined(myMock.anotherMethod("bar", "zap"))
99
 
}
100
 
 
101
 
function testCallingExpectedFunctionWithTooFewArgumentsShouldFail() {
102
 
    var myMock = new Mock()
103
 
    myMock.expects("someMethod", "foo", "bar")
104
 
    try {
105
 
        myMock.someMethod("foo")
106
 
    } catch(expected) {
107
 
        return
108
 
    }
109
 
    fail("Should fail because too few arguments were passed!")
110
 
}
111
 
 
112
 
function testCallingExpectedFunctionWithTooManyArgumentsShouldFail() {
113
 
    var myMock = new Mock()
114
 
    myMock.expects("someMethod", "foo")
115
 
    try {
116
 
        myMock.someMethod("foo", "bar")
117
 
    } catch(expected) {
118
 
        return
119
 
    }
120
 
    fail("Should fail because too many arguments were passed!")
121
 
}
122
 
 
123
 
function testShouldCreateMockInstancesWithoutSideffects() {
124
 
    var foo = new Mock()
125
 
    var bar = new Mock()
126
 
 
127
 
    foo.expects("foo")
128
 
    bar.expects("bar")
129
 
 
130
 
    try {
131
 
        bar.foo()
132
 
    } catch(expected) {
133
 
        return
134
 
    }
135
 
    fail("Should fail because an unexpected bar was called!")
136
 
}
137
 
 
138
 
function testCallingExpectedFunctionWithReturnShouldReturnValue() {
139
 
    var myMock = new Mock()
140
 
    myMock.expects("someMethod", "bar").returns("foo")
141
 
    myMock.expects("theOtherMethod", "zap", "ping", "pong").returns("bang")
142
 
    assertEquals("foo", myMock.someMethod("bar"))
143
 
    assertEquals("bang", myMock.theOtherMethod("zap", "ping", "pong"))
144
 
    myMock.verify()
145
 
}
146
 
 
147
 
function testCallingExpectedFunctionWithThrowsShouldThrowError() {
148
 
    var myMock = new Mock();
149
 
    myMock.expects("someMethod", "bar").andThrows("failure")
150
 
    try {
151
 
        myMock.someMethod("bar")
152
 
    } catch (e) {
153
 
        assertEquals("failure", e.message);
154
 
        return
155
 
    }
156
 
    fail("Mock did not throw exception when required");
157
 
}
158
 
 
159
 
function testSettingExpectedPropertyShouldPass() {
160
 
    var myMock = new Mock()
161
 
    myMock.expectsProperty("foo", "bar")
162
 
    myMock.foo = "bar"
163
 
    myMock.verify()
164
 
}
165
 
 
166
 
function testShouldAllowExpectationOfSameFunctionWithDifferentArguments() {
167
 
    var myMock = new Mock()
168
 
    myMock.expects("aslak", "hello").returns("world")
169
 
    myMock.expects("aslak", "bonjour").returns("monde")
170
 
    assertEquals("world", myMock.aslak("hello"))
171
 
    assertEquals("monde", myMock.aslak("bonjour"))
172
 
    myMock.verify()
173
 
}
174
 
 
175
 
function TODO_testSettingUnexpectedPropertyShouldFail() {
176
 
    var myMock = new Mock()
177
 
    myMock.foo = "bar"
178
 
 
179
 
    try {
180
 
        myMock.verify()
181
 
    } catch(expected) {
182
 
        return
183
 
    }
184
 
    fail("Should fail because an unexpected property was set!")
185
 
}
186
 
 
187
 
function TODO_testNotSettingExpectedPropertyShouldFail() {
188
 
    var myMock = new Mock()
189
 
    myMock.expectsProperty("foo", "bar")
190
 
    try {
191
 
        myMock.verify()
192
 
    } catch(expected) {
193
 
        return
194
 
    }
195
 
    fail("Should fail because an expected property was not set!")
196
 
}
197
 
 
198
 
function TODO_testSettingExpectedPropertyWithUnexpectedValueShouldFail() {
199
 
    var myMock = new Mock()
200
 
    myMock.expectsProperty("foo", "bar")
201
 
    myMock.foo = "zap"
202
 
    try {
203
 
        myMock.verify()
204
 
    } catch(expected) {
205
 
        return
206
 
    }
207
 
    fail("Should fail because an expected property was set with unexpected value!")
208
 
}
209
 
 
210
 
</script>
211
 
</head>
212
 
 
213
 
<body>
214
 
<h1>JsMock Tests</h1>
215
 
 
216
 
<p>This page contains tests for JsMock. To see them, take a look at the source. To run them, load this file via JsUnit's
217
 
    testRunner.html</p>
218
 
</body>
219
 
</html>