本文实例讲述了Node.js assert断言原理与用法。分享给大家供大家参考,具体如下:
node.js官方API中文版 http://nodeapi.ucdok.com/#/api/assert.html
assert 模块主要用于编写程序的单元测试时使用,通过断言可以提早发现和排查出错误。
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); /* 输出如下 { [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本身。
测试如下:
var test = function ok() { console.log('test ok'); } //输出 undefined test.ok = test; //输出 { [Function: ok] ok: [Circular] } test.fail = function fail() { console.log('test fail'); } //输出 [Function: fail] console.log(test); //输出 {[Function: ok] ok: [Circular], fail: [Function: fail] }
比较相等操作符 ‘==' 会根据前面的参数进行类型转换。
true == 1; // true 1 == true; // true true == 2; // false 2 == true; // false '' == false; // true false == ''; // true 1 == '1'; // true
全等操作符 ‘===' 会先比较元素的类型,只有类型和值都一样才算相等。
true === 1; // false 1 === '1'; // false
转回正题:
注意:如果不设置message,就会将value打印出来。
assert.fail(actual, expected, message, operator)
在不检查任何条件的情况下使断言失败。如果有错误信息则输出错误信息,否则输出actual和expected,中间用operator隔开。
assert.fail(1, 1); //输出 AssertionError: 1 undefined 1 assert.fail(1, 1, undefined, '=='); //输出 AssertionError: 1 == 1 assert.fail(1, 2, undefined, '>'); //输出 AssertionError: 1 > 2 assert.fail(1, 2, 'whoops', '>'); //输出 AssertionError: whoops
assert(value, message), assert.ok(value, [message])
assert(true, 'message'); //输出 undefined assert(false, 'message'); //输出 AssertionError: message assert.ok(true, 'message'); //输出 undefined assert.ok(false, 'message'); //输出 AssertionError: message
assert.equal(actual, expected, [message])
和比较操作符(==)的判断结果相同。当两边都是基本变量的时候转化为同一类型的变量再进行比较;如果是引用类型的变量,则直接比较其内存地址。
assert.equal(1, 1, 'message'); //输出 undefined assert.equal(1, '1', 'message'); //输出 AssertionError: message
assert.strictEqual(actual, expected, [message])
Tests strict equality, as determined by the strict equality operator ( === )
严格相等,和全等符号(===)的判断结果相同。
assert.strictEqual(1, 1, 'message'); //输出 undefined assert.strictEqual(1, '1', 'message'); //输出 AssertionError: message assert.strictEqual(1, '1', 'message'); //输出 AssertionError: message
assert.deepEqual(actual, expected, [message])
当比较的双方均为基本类型时,等价于euqal()
。
当比较的双方均为引用类型时,即将引用类型中的每一个属性用equal()
进行比较。
assert.equal(1, '1'); //输出 undefined assert.deepEqual(1, '1'); //输出 undefined assert.strictEqual(1, '1'); //输出 assert.strictEqual(1, '1'); assert.equal({a:1}, {a:'1'}); //输出 AssertionError: { a: 1 } == {a: '1'} assert.deepEqual({a:1}, {a:'1'}); //输出 undefined assert.strictEqual({a:1}, {a:'1'}); //输出 AssertionError: { a: 1 } == {a: '1'}
assert.throws(block, [error], [message])
Expects the function block to throw an error.
If specified, error can be a constructor, RegExp, or validation function.
If specified, message will be the message provided by the AssertionError if the block fails to throw.
assert.throws( () => {}, Error ); //输出 AssertionError: Missing expected exception (Error).. assert.throws( () => {throw new Error('Wrong value');}, Error ); //输出 undefined assert.throws( () => {throw new Error('Wrong value');}, /Wrong/ ); //输出 undefined assert.throws( () => {throw new Error('Wrong value');}, /wrong/ ); //输出 Error: Wrong value assert.throws( () => {throw new Error('Wrong value');}, (err) => { if ((err instanceof Error) && /value/.test(err)) { return true; } }, 'unexpected error' ); //输出 undefined
Note that error can not be a string. If a string is provided as the second argument, then error is assumed to be omitted and the string will be used for message instead. This can lead to easy-to-miss mistakes:
注意:错误信息不能是一个字符串。如果字符串被作为第二个参数,那么错误就会被假定为省略,并且字符串将会被用作提示信息,这样很容易导致错误。
assert.throws(()=>{throw new Error('Wrong value');}, 'Wrong', 'did not throw with expected message'); //输出 undefined assert.throws(()=>{}, 'Wrong', 'did not throw with expected message'); //输出 AssertionError: Missing expected exception. Wrong assert.throws(()=>{}, /Wrong/, 'did not throw with expected message'); //输出 AssertionError: Missing expected exception. did not with expected message.
assert.ifError(value)
Throws value if value is truthy. This is useful when testing the error argument in callbacks.
当值为真时,抛出AssertionError错误。该方法在测试回调函数的参数时非常有用。
assert.ifError(0); //输出 undefined assert.ifError(1); //输出 1 assert.ifError('error'); //输出 error assert.ifError(new Error('there maybe wrong')); //输出 Error: there maybe wrong
希望本文所述对大家nodejs程序设计有所帮助。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。