NodeJs-assert断言例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class : assert
- assert.fail(actual, expected, message, operator)
- assert(value, message), assert.ok(value, [message])
- assert.equal(actual, expected, [message])
- assert.notEqual(actual, expected, [message])
- assert.deepEqual(actual, expected, [message])
- assert.notDeepEqual(actual, expected, [message])
- assert.strictEqual(actual, expected, [message])
- assert.notStrictEqual(actual, expected, [message])
- assert.throws(block, [error], [message])
- assert.doesNotThrow(block, [message])
- assert.ifError(value)

console.log(assert);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*

输出如下

{ [Function: ok]
AssertionError:
{ [Function: AssertionError]
super_:
{ [Function: Error]
captureStackTrace: [Function: captureStackTrace],
stackTraceLimit: 10 } },
fail: [Function: fail],
ok: [Circular],
equal: [Function: equal],
notEqual: [Function: notEqual],
deepEqual: [Function: deepEqual],
notDeepEqual: [Function: notDeepEqual],
strictEqual: [Function: strictEqual],
notStrictEqual: [Function: notStrictEqual],
throws: [Function],
doesNotThrow: [Function],
ifError: [Function] }
*/

assert是个函数,函数名为ok。javascript中函数是Function类的实例,也就是对象,所以可为其添加fail和equal等属性。注意输出结果第9行 ok:[Circular] 这个表述,这是指针循环的意思,即ok属性指向了本身,所以调用assert.ok就相当于调用了assert本身。

  • 版权声明: 本博客所有文章,未经许可,任何单位及个人不得做营利性使用!转载请标明出处!如有侵权请联系作者。
  • Copyrights © 2015-2020 翟天野

请我喝杯咖啡吧~