Node.js断言Assert

Node.js断言Assert 首页 / Node.js入门教程 / Node.js断言Assert

Node.js声明是编写测试的最基本方法。除非测试失败,否则在运行测试时它不会提供任何反馈。断言模块提供了一组简单的断言测试,可用于测试不变式。该模块供Node.js内部使用,但可以通过require('assert')在应用程序代码中使用。

断言示例

让无涯教程看看Node.js assert的一个简单示例。

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/nodejs/nodejs-assertion-testing.html

来源:LearnFk无涯教程网

文件:assert_example1.js.

var assert = require('assert');
function add (a, b) {
  return a + b;
}
var expected = add(1,2);
assert( expected === 3, 'one plus two is three');

因为大小写正确,所以它不会提供任何输出。如果要查看输出,则需要使测试失败。

Node.js assert example 1

文件:assert_example2.js.

var assert = require('assert');
function add (a, b) {
  return a + b;
}
var expected = add(1,2);
assert( expected === 4, 'one plus two is three');

现在,您将看到AssertionError。

Node.js assert example 2

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

教程推荐

说透低代码 -〔陈旭〕

说透5G -〔杨四昌〕

Spring编程常见错误50例 -〔傅健〕

深入浅出云计算 -〔何恺铎〕

后端技术面试 38 讲 -〔李智慧〕

即时消息技术剖析与实战 -〔袁武林〕

Python核心技术与实战 -〔景霄〕

玩转Spring全家桶 -〔丁雪丰〕

朱赟的技术管理课 -〔朱赟〕

好记忆不如烂笔头。留下您的足迹吧 :)