Node.js assert断言原理与用法分析

所属分类: 网络编程 / JavaScript 阅读数: 1017
收藏 0 赞 0 分享

本文实例讲述了Node.js assert断言原理与用法。分享给大家供大家参考,具体如下:

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本身。

测试如下:

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程序设计有所帮助。

更多精彩内容其他人还在看

jQuery LigerUI 使用教程表格篇(1)

ligerGrid是ligerui系列插件的核心控件,用户可以快速地创建一个美观,而且功能强大的表格,支持排序、分页、多表头、固定列等等
收藏 0 赞 0 分享

JavaScript中常用的运算符小结

JavaScript中常用的运算符小结,需要的朋友可以参考下。
收藏 0 赞 0 分享

深入理解JavaScript系列(13) This? Yes,this!

在这篇文章里,我们将讨论跟执行上下文直接相关的更多细节。讨论的主题就是this关键字。实践证明,这个主题很难,在不同执行上下文中this的确定经常会发生问题
收藏 0 赞 0 分享

javascript (用setTimeout而非setInterval)

javascript (用setTimeout而非setInterval)如果用setInterval 可能出现 下次调用会在前一次调用前调用
收藏 0 赞 0 分享

JavaScript中两个感叹号的作用说明

用两个感叹号的作用就在于,如果明确设置了o中flag的值(非null/undefined/0""/等值),自然test就会取跟o.flag一样的值;如果没有设置,test就会默认为false,而不是null或undefined
收藏 0 赞 0 分享

javascript写的简单的计算器,内容很多,方法实用,推荐

最近用javascript写了一个简单的计算器,自己测试感觉还好,代码都给了注释,非常不错,推荐大家学习。
收藏 0 赞 0 分享

js的表单操作 简单计算器

javascript写的简单的加减乘除计算器,里面涉及到一些方法还是很实用的哦,新手不要错过
收藏 0 赞 0 分享

Jquery中删除元素的实现代码

empty用来删除指定元素的子元素,remove用来删除元素,或者设定细化条件执行删除
收藏 0 赞 0 分享

javaScript 利用闭包模拟对象的私有属性

JavaScript缺少块级作用域,没有private修饰符,但它具有函数作用域。作用域的好处是内部函数可以访问它们的外部函数的参数和变量(除了this和argument
收藏 0 赞 0 分享

为JavaScript类型增加方法的实现代码(增加功能)

大家在js开发过程中有些功能已经满足不了我们的需求,或没有我们需要的功能,那么我们就可以自己扩展下,个性化js
收藏 0 赞 0 分享
查看更多