码迷,mamicode.com
首页 > Web开发 > 详细

Nodejs断言测试

时间:2016-10-14 23:02:17      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:

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");
// });

Nodejs断言测试

标签:

原文地址:http://www.cnblogs.com/xiao-zhang-blogs/p/5962040.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!