Node.js声明是编写测试的最基本方法。除非测试失败,否则在运行测试时它不会提供任何反馈。断言模块提供了一组简单的断言测试,可用于测试不变式。该模块供Node.js内部使用,但可以通过require('assert')在应用程序代码中使用。
让无涯教程看看Node.js assert的一个简单示例。
文件: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');
因为大小写正确,所以它不会提供任何输出。如果要查看输出,则需要使测试失败。
文件: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。
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)