断言:学java的时候学过断言,但一直用的很少,node中也有
var assert = require(‘assert‘) console.info(‘.......start app.......‘); var actual = 1; var expect = ‘1‘ var a; //判断是否为真 assert(a, ‘is not valid‘); assert.ok(0,‘is not effective‘); //arg[0]为真实值,arg[1]期望值,arg[2]可选错误信息 //equal和notEqual是通过==和!=,比较是会类型转换,比如1==‘1‘ 为true //strictEqual和notStrictEqual是通过===和!==,比如1===‘1‘ 为false assert.equal(actual, actual, ‘excption message:value !==...‘); assert.strictEqual(actual, expect, ‘excption message:value !===‘); assert.notEqual(actual, expect, ‘excption message:value ==...‘) assert.notStrictEqual(actual, expect, ‘excption message:value===...‘) console.info(‘.......end app.......‘);
还有几个没有测试过,用到再说。
assert.throws(block, [error], [message])
本文出自 “天空海阔” 博客,请务必保留此出处http://ether007.blog.51cto.com/8912105/1411274
原文:http://ether007.blog.51cto.com/8912105/1411274