Nodejs断言测试

var assert = require('assert');/*node中,我们可以使用assert模块来测试代码。equal()和notEqual()分别作相等性和不等性的判断,第一个参数是期望值,第二个参数是真实值,第三个参数是异常信息*/// assert.equal(1,2,[console.log('ok')]);//期望值和真实值无论相等还是不相等,输出信息,为什么?// assert.equal(1,2,['not equal']);//期望值和真实值不相等,抛出异常信息/*ok()方法是比较真值的简洁方法,相当于是用==比较当前值是否为true。*/// assert.ok('','w');//空格为false// assert.ok('This is a string', 'Strings that are not empty are truthy'); //true assert.ok(0, 'Zero is not truthy');//0为false// assert.fale(1,2,'not equal',console.log('error'));/*node提供了对object对象的比较方法deepEqual() 和 notDeepEqual(),他们采用下面的步骤比较对象,有一个步骤不匹配就抛出异常:1.采用===比较;2.比较他们是否是Buffers,如果是则比较长度,接下来每字节每字节的比较;3.是用==比较;4.最后如果参数是object对象,则比较他们得属性长度和属性值。可以看的出来,这两个方法性能上可能要差些,所以只有在需要的时候才使用他们。*/// assert.deepEqual(1,2,['not equal']);/*assert.throws(block[, error][, message])声明一个 block 用来抛出错误(error), error可以是构造函数,正则表达式或其他验证器。*/// assert.throws(function(){//   throw new Error('message is wrong');// });// assert.doesNotThrow(function() {//   throw new Error("I lived in the ocean way before Nemo");// });