Node.js assert断言原理与用法分析
本文实例讲述了node.js assert断言原理与用法。分享给大家供大家参考,具体如下:
node.js官方api中文版
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程序设计有所帮助。