首页 » 技术分享 » Node.js - 笔记 - 断言测试 - assert(value, message)

Node.js - 笔记 - 断言测试 - assert(value, message)

 

assert(value[, message])

无错误抛出的情况

/*
 * assert(value,message);
 * assert.ok();的别名
 * 返回类型: function
 * 参数类型:value <any> 检查是否为真的输入
 *         message <string>|<Error>
 * 
 * Data types "<any>"
 * The latest ECMAScript standard defines eight data types:
 * Seven data types that are primitives:
 * 	Boolean
 * 	Null
 * 	Undefined
 * 	Number
 * 	BigInt (Integers, Floats, Doubles, or Bignums.)
 * 	String
 * 	Symbol
 * 	Object
 */
const assert = require('assert');//assert为function类型

console.log('===================\n');
assert(true,"Boolean-true");
console.log('==Boolean-true ok!');

assert(100,"Number");
console.log('==Number ok!');

assert(BigInt("0x1fffffffffffff"),"BigInt");
console.log('==BigInt ok!');

assert("Hello!","String");
console.log('==String ok!');

assert(Symbol('foo'),"Symbol");
console.log('==Symbol ok!');

assert({a:1,b:"he"},"Object");
console.log('==Object ok!');

console.log('===================\n');

运行结果:
在这里插入图片描述
########################################################

抛出错误的情况

assert(true,“Boolean-true”);

const assert = require('assert');//assert为function类型

assert(true,"Boolean-true");
console.log('\n==Boolean-true ok!\n');

assert(false,"Boolean-false");
console.log('==Boolean-false ok!');

在这里插入图片描述
####################################################
assert(null,“Null”);

const assert = require('assert');//assert为function类型

assert(true,"Boolean-true");
console.log('\n==Boolean-true ok!\n');

//assert(false,"Boolean-false");
//console.log('==Boolean-false ok!');

assert(null,"Null");
console.log('Null ok!');

在这里插入图片描述
####################################################
assert(undefined,“Undefined”);

const assert = require('assert');//assert为function类型

assert(true,"Boolean-true");
console.log('\n==Boolean-true ok!\n');

//assert(false,"Boolean-false");
//console.log('==Boolean-false ok!');

//assert(null,"Null");
//console.log('Null ok!');

assert(undefined,"Undefined");
console.log('==Undefined ok!');

在这里插入图片描述
####################################################
assert(0,“Number-0”);

const assert = require('assert');//assert为function类型

assert(true,"Boolean-true");
console.log('\n==Boolean-true ok!\n');

//assert(false,"Boolean-false");
//console.log('==Boolean-false ok!');

//assert(null,"Null");
//console.log('Null ok!');

//assert(undefined,"Undefined");
//console.log('==Undefined ok!');

assert(0,"Number-0");
console.log('==Number-0 ok!');

在这里插入图片描述
####################################################

若为value值为假则断言错误,并把信息存储在对象assert.AssertionError

/***********************************************************
 * 参数value值错误的情况
 * 捕获错误信息,这个错误信息存储在对象assert.AssertionError里.
 *************************************************************/
const assert = require('assert');//assert为function类型

tryAndPrintErr(false,"Boolean-false");
tryAndPrintErr(null,"Null");
tryAndPrintErr(undefined,"Undefined");
tryAndPrintErr(0,"Number-0");

//捕获并打印value值为假时导致错误的相关信息
function tryAndPrintErr(value,message){
	try{
		//意思是期望第一个参数结果为真,若为真则正常
		//若为假则断言错误并把信息存储在对象assert.AssertionError
		assert(value,message);
		console.log('一切正常!');
	}catch(err){
		//打印捕获错误的相关信息
		console.log('\nvalue=' + value + ' 存在异常!以下为异常信息:');
		console.log("err instanceof assert.AssertionError: " + (err instanceof assert.AssertionError));
		console.log("err.name: " + err.name);	  
		console.log("err.message: " + err.message);
		console.log("err.generatedMessage: " + err.generatedMessage);
		console.log("err.code: " + err.code);
		console.log("err.actual: " + err.actual);
		console.log("err.expected: " + err.expected);
		console.log("err.operator: " + err.operator);	
	}
}

在这里插入图片描述

转载自原文链接, 如需删除请联系管理员。

原文链接:Node.js - 笔记 - 断言测试 - assert(value, message),转载请注明来源!

0