aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/mozilla/add-ons/sdk/low-level_apis/test_assert/index.html
blob: 5f7537ec42ef7c680e61c3e3af3f0841245596f4 (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
---
title: test/assert
slug: Mozilla/Add-ons/SDK/Low-Level_APIs/test_assert
translation_of: Archive/Add-ons/Add-on_SDK/Low-Level_APIs/test_assert
---
<p>{{LegacyAddonsNotice}}{{AddonSidebar}}</p>

<div class="note">
<p>Unstable</p>
</div>

<p>实现在 <a href="http://wiki.commonjs.org/wiki/Unit_Testing/1.1">CommonJS Unit Testing specification version 1.1</a> 其中定义的断言接口。</p>

<h2 id="Usage">Usage</h2>

<p>To use the <code>assert</code> module, write a set of unit tests following the instructions in the <a href="/en-US/Add-ons/SDK/Tutorials/Unit_testing">unit testing tutorial</a>. Each test will be passed an <code>Assert</code> object when you run the tests using <a href="/en-US/Add-ons/SDK/Tools/jpm"><code>jpm test</code></a>. You can use this object to make assertions about your program's state.</p>

<p>For example:</p>

<pre class="brush: js">var a = 1;

exports["test value of a"] = function(assert) {
  assert.ok(a == 1, "test that a is 1");
}

require("sdk/test").run(exports);</pre>

<h2 id="Globals">Globals</h2>

<h3 id="Constructors">Constructors</h3>

<h4 class="addon-sdk-api-name" id="Assert(logger)"><code>Assert(logger)</code></h4>

<p>Create a new Assert object. This function is only called by the unit test framework, and not by unit tests themselves.</p>

<h5 id="Parameters">Parameters</h5>

<p><strong>logger : object</strong><br>
 Object used to log the results of assertions.</p>

<h2 id="Assert">Assert</h2>

<p>An object used to make assertions about a program's state in order to implement unit tests.</p>

<p>The <code>Assert</code> object's interface is defined by the <a href="http://wiki.commonjs.org/wiki/Unit_Testing/1.1">CommonJS Unit Testing specification, version 1.1</a>.</p>

<h3 id="Methods">Methods</h3>

<h4 class="addon-sdk-api-name" id="ok(guard_message)"><code>ok(guard, message)</code></h4>

<p>Tests whether an expression evaluates to true.</p>

<pre class="brush: js">assert.ok(a == 1, "test that a is equal to one");</pre>

<p>This is equivalent to:</p>

<pre class="brush: js">assert.equal(a == 1, true, "test that a is equal to one");</pre>

<h5 id="Parameters_2">Parameters</h5>

<p><strong>guard : expression</strong><br>
 The expression to evaluate.</p>

<p><strong>message : string</strong><br>
 Optional message to log, providing extra information about the test.</p>

<h4 class="addon-sdk-api-name" id="equal(actual_expected_message)"><code>equal(actual, expected, message)</code></h4>

<p>Tests shallow, coercive equality with <code>==</code>:</p>

<pre class="brush: js">assert.equal(1, 1, "test that one is one");</pre>

<h5 id="Parameters_3">Parameters</h5>

<p><strong>actual : object</strong><br>
 The actual result.</p>

<p><strong>expected : object</strong><br>
 The expected result.</p>

<p><strong>message : string</strong><br>
 Optional message to log, providing extra information about the test.</p>

<h4 class="addon-sdk-api-name" id="notEqual(actual_expected_message)"><code>notEqual(actual, expected, message)</code></h4>

<p>Tests that two objects are not equal, using <code>!=</code>:</p>

<pre class="brush: js">assert.notEqual(1, 2, "test that one is not two");</pre>

<h5 id="Parameters_4">Parameters</h5>

<p><strong>actual : object</strong><br>
 The actual result.</p>

<p><strong>expected : object</strong><br>
 The expected result.</p>

<p><strong>message : string</strong><br>
 Optional message to log, providing extra information about the test.</p>

<h4 class="addon-sdk-api-name" id="deepEqual(actual_expected_message)"><code>deepEqual(actual, expected, message)</code></h4>

<p>Tests that two objects have a deep equality relation. Deep equality is defined in the <a href="http://wiki.commonjs.org/wiki/Unit_Testing/1.0#Assert">CommonJS specification for Assert</a>, item 7, which is quoted here:</p>

<div class="quote">
<ol>
 <li>All identical values are equivalent, as determined by ===.</li>
 <li>If the expected value is a Date object, the actual value is equivalent if it is also a Date object that refers to the same time.</li>
 <li>Other pairs that do not both pass typeof value == "object", equivalence is determined by ==.</li>
 <li>For all other Object pairs, including Array objects, equivalence is determined by having the same number of owned properties (as verified with Object.prototype.hasOwnProperty.call), the same set of keys (although not necessarily the same order), equivalent values for every corresponding key, and an identical "prototype" property. Note: this accounts for both named and indexed properties on Arrays.</li>
</ol>
</div>

<pre class="brush: js">assert.deepEqual({ a: "foo" }, { a: "foo" }, "equivalent objects");</pre>

<h5 id="Parameters_5">Parameters</h5>

<p><strong>actual : object</strong><br>
 The actual result.</p>

<p><strong>expected : object</strong><br>
 The expected result.</p>

<p><strong>message : string</strong><br>
 Optional message to log, providing extra information about the test.</p>

<h4 class="addon-sdk-api-name" id="notDeepEqual(actual_expected_message)"><code>notDeepEqual(actual, expected, message)</code></h4>

<p>Tests that two objects do not have a deep equality relation, using the negation of the test for deep equality:</p>

<pre class="brush: js">assert.notDeepEqual({ a: "foo" }, Object.create({ a: "foo" }),
                    "object's inherit from different prototypes");</pre>

<h5 id="Parameters_6">Parameters</h5>

<p><strong>actual : object</strong><br>
 The actual result.</p>

<p><strong>expected : object</strong><br>
 The expected result.</p>

<p><strong>message : string</strong><br>
 Optional message to log, providing extra information about the test.</p>

<h4 class="addon-sdk-api-name" id="strictEqual(actual_expected_message)"><code>strictEqual(actual, expected, message)</code></h4>

<p>Tests that two objects are equal, using the strict equality operator <code>===</code>:</p>

<pre class="brush: js">// This test will pass, because "==" will perform type conversion
exports["test coercive equality"] = function(assert) {
  assert.equal(1, "1", "test coercive equality between 1 and '1'");
}

// This test will fail, because the types are different
exports["test strict equality"] = function(assert) {
  assert.strictEqual(1, "1", "test strict equality between 1 and '1'");
}</pre>

<h5 id="Parameters_7">Parameters</h5>

<p><strong>actual : object</strong><br>
 The actual result.</p>

<p><strong>expected : object</strong><br>
 The expected result.</p>

<p><strong>message : string</strong><br>
 Optional message to log, providing extra information about the test.</p>

<h4 class="addon-sdk-api-name" id="notStrictEqual(actual_expected_message)"><code>notStrictEqual(actual, expected, message)</code></h4>

<p>Tests that two objects are not equal, using the negation of the strict equality operator <code>===</code>:</p>

<pre class="brush: js">// This test will fail, because "==" will perform type conversion
exports["test coercive equality"] = function(assert) {
  assert.notEqual(1, "1", "test coercive equality between 1 and '1'");
}

// This test will pass, because the types are different
exports["test strict equality"] = function(assert) {
  assert.notStrictEqual(1, "1", "test strict equality between 1 and '1'");
}</pre>

<h5 id="Parameters_8">Parameters</h5>

<p><strong>actual : object</strong><br>
 The actual result.</p>

<p><strong>expected : object</strong><br>
 The expected result.</p>

<p><strong>message : string</strong><br>
 Optional message to log, providing extra information about the test.</p>

<h4 class="addon-sdk-api-name" id="throws(block_error_message)"><code>throws(block, error, message)</code></h4>

<p>Assert that a block of code throws the expected exception.</p>

<p>This method takes an optional <code>Error</code> argument:</p>

<ul>
 <li>
  <p>to check that the exception thrown is of the expected type, pass a constructor function: the exception thrown must be an instance of the object returned by that function.</p>
 </li>
 <li>
  <p>to check that the exception thrown contains a specific message, pass a regular expression here: the <code>message</code> property of the exception thrown must match the regular expression</p>
 </li>
</ul>


<p>For example, suppose we define two different custom exceptions:</p>

<pre class="brush: js">function MyError(message) {
  this.name = "MyError";
  this.message = message || "Default Message";
}

MyError.prototype = new Error();
MyError.prototype.constructor = MyError;

function AnotherError(message) {
  this.name = "AnotherError";
  this.message = message || "Default Message";
    console.log(this.message);
}

AnotherError.prototype = new Error();
AnotherError.prototype.constructor = AnotherError;</pre>

<p>We can check the type of exception by passing a function as the <code>Error</code> argument:</p>

<pre class="brush: js">exports["test exception type 1 expected to pass"] = function(assert) {
  assert.throws(function() {
    throw new MyError("custom message");
  },
  MyError,
  "test throwing a specific exception");
}

exports["test exception type 2 expected to fail"] = function(assert) {
  assert.throws(function() {
    throw new MyError("custom message");
  },
  AnotherError,
  "test throwing a specific exception");
}</pre>

<p>We can check the message by passing a regular expression:</p>

<pre class="brush: js">exports["test exception message 1 expected to pass"] = function(assert) {
  assert.throws(function() {
    throw new MyError("custom message");
  },
  /custom message/,
  "test throwing a specific message");
}

exports["test exception message 2 expected to pass"] = function(assert) {
  assert.throws(function() {
    throw new AnotherError("custom message");
  },
  /custom message/,
  "test throwing a specific exception");
}

exports["test exception message 3 expected to fail"] = function(assert) {
  assert.throws(function() {
    throw new MyError("another message");
  },
  /custom message/,
  "test throwing a specific message");
}</pre>

<h5 id="Parameters_9">Parameters</h5>

<p><strong>block : block</strong><br>
 The block of code to test.</p>

<p><strong>error : function|RegExp</strong><br>
 Either a constructor function returning the type of exception expected, or a regular expression expected to match the exception's <code>message</code> property.</p>

<p><strong>message : string</strong><br>
 Optional message to log, providing extra information about the test.</p>