Node.js assert断言原理与用法分析
本文实例讲述了Node.jsassert断言原理与用法。分享给大家供大家参考,具体如下:
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本身。
测试如下:
vartest=functionok(){
console.log('testok');
}
//输出undefined
test.ok=test;
//输出{[Function:ok]ok:[Circular]}
test.fail=functionfail(){
console.log('testfail');
}
//输出[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:1undefined1 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])
Testsstrictequality,asdeterminedbythestrictequalityoperator(===)
严格相等,和全等符号(===)的判断结果相同。
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])
Expectsthefunctionblocktothrowanerror.
Ifspecified,errorcanbeaconstructor,RegExp,orvalidationfunction.
Ifspecified,messagewillbethemessageprovidedbytheAssertionErroriftheblockfailstothrow.
assert.throws(
()=>{},
Error
);
//输出AssertionError:Missingexpectedexception(Error)..
assert.throws(
()=>{thrownewError('Wrongvalue');},
Error
);
//输出undefined
assert.throws(
()=>{thrownewError('Wrongvalue');},
/Wrong/
);
//输出undefined
assert.throws(
()=>{thrownewError('Wrongvalue');},
/wrong/
);
//输出Error:Wrongvalue
assert.throws(
()=>{thrownewError('Wrongvalue');},
(err)=>{
if((errinstanceofError)&&/value/.test(err)){returntrue;
}
},
'unexpectederror'
);
//输出undefined
Notethaterrorcannotbeastring.Ifastringisprovidedasthesecondargument,thenerrorisassumedtobeomittedandthestringwillbeusedformessageinstead.Thiscanleadtoeasy-to-missmistakes:
注意:错误信息不能是一个字符串。如果字符串被作为第二个参数,那么错误就会被假定为省略,并且字符串将会被用作提示信息,这样很容易导致错误。
assert.throws(()=>{thrownewError('Wrongvalue');},'Wrong','didnotthrowwithexpectedmessage');
//输出undefined
assert.throws(()=>{},'Wrong','didnotthrowwithexpectedmessage');
//输出AssertionError:Missingexpectedexception.Wrong
assert.throws(()=>{},/Wrong/,'didnotthrowwithexpectedmessage');
//输出AssertionError:Missingexpectedexception.didnotwithexpectedmessage.
assert.ifError(value)
Throwsvalueifvalueistruthy.Thisisusefulwhentestingtheerrorargumentincallbacks.
当值为真时,抛出AssertionError错误。该方法在测试回调函数的参数时非常有用。
assert.ifError(0);
//输出undefined
assert.ifError(1);
//输出1
assert.ifError('error');
//输出error
assert.ifError(newError('theremaybewrong'));
//输出Error:theremaybewrong
希望本文所述对大家nodejs程序设计有所帮助。