- Node.js教程
- Node.js - 教程
- Node.js - 简介
- Node.js - 环境设置
- Node.js - 首次申请
- Node.js - REPL 终端
- Node.js - 命令行选项
- Node.js - 包管理器 (NPM)
- Node.js - 回调概念
- Node.js - 上传文件
- Node.js - 发送电子邮件
- Node.js - 活动
- Node.js - 事件循环
- Node.js - 事件发射器
- Node.js - 调试器
- Node.js - 全局对象
- Node.js - 控制台
- Node.js - 流程
- Node.js - 扩展应用程序
- Node.js - 包装
- Node.js - Express 框架
- Node.js - RESTful API
- Node.js - 缓冲器
- Node.js - Streams
- Node.js - 文件系统
- Node.js MySQL
- Node.js - MySQL 快速入门
- Node.js - MySQL创建数据库
- Node.js - MySQL创建表
- Node.js - MySQL Insert Into
- Node.js - MySQL Select From
- Node.js - MySQL Where 子句
- Node.js - MySQL Order By
- Node.js - MySQL Delete
- Node.js - MySQL Update
- Node.js - MySQL Join
- Node.js MongoDB
- Node.js - MongoDB 快速入门
- Node.js - MongoDB 创建数据库
- Node.js - MongoDB 创建集合
- Node.js - MongoDB Insert
- Node.js - MongoDB Find
- Node.js - MongoDB 查询
- Node.js - MongoDB 排序
- Node.js - MongoDB Delete
- Node.js - MongoDB Update
- Node.js - MongoDB Limit
- Node.js - MongoDB Join
- Node.js模块
- Node.js - 模块
- Node.js - 内置模块
- Node.js - utility 模块
- Node.js - Web 模块
Node.js - assert.report() 函数
assert 模块提供了一组用于验证不变量的 assert 函数。tracker.report() 函数是 Node.js 的 assert 模块的内置函数。
当我们想要了解有关未被调用预期次数的函数的预期调用次数和实际调用次数的信息时,Node.js tracker.report() 函数非常有用。
此函数返回一个对象数组,其中包含有关 tracker.calls() 函数返回的包装函数的所有信息。
语法
以下是 Node.js tracker.report() 函数的示例 -
tracker.report();
参数
此函数不接受任何参数。
返回值
函数 tracker.report() 返回一个对象数组,其中包含有关 tracker.calls() 函数返回的包装函数的信息。
信息对象持有如下所述 -
- message<string> - 消息将由函数自动分配。
- actual<number> - 这显示了调用函数的实际次数。
- expected<number> - 这显示了函数预期调用的次数。
- operator<operator> 这将让我们知道被包装器函数包装的函数的名称。
- stack<Object> - 函数的堆栈跟踪。
在以下示例中,
- 我们正在创建一个呼叫跟踪器对象。
- 然后,我们正在创建一个包装函数。
- 然后我们将函数 func 作为参数传递给 tracker.calls() 函数,我们将函数 func 包装到包装函数 funccall。
- 然后我们调用 tracker.report() 函数。
const assert = require('assert');
const tracker = new assert.CallTracker();
function func() {};
const callsfunc = tracker.calls(func, 5);
console.log(tracker.report());
输出
/home/cg/root/63a002c52763b/main.js:1
(function (exports, require, module, __filename, __dirname) { aconst assert = require('assert');
^^^^^^
SyntaxError: Unexpected identifier
at new Script (vm.js:74:7)
at createScript (vm.js:246:10)
at Object.runInThisContext (vm.js:298:10)
at Module._compile (internal/modules/cjs/loader.js:670:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
at Module.load (internal/modules/cjs/loader.js:612:32)
at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
at Function.Module._load (internal/modules/cjs/loader.js:543:3)at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
at startup (internal/bootstrap/node.js:238:19)
(function (exports, require, module, __filename, __dirname) { aconst assert = require('assert');
^^^^^^
SyntaxError: Unexpected identifier
at new Script (vm.js:74:7)
at createScript (vm.js:246:10)
at Object.runInThisContext (vm.js:298:10)
at Module._compile (internal/modules/cjs/loader.js:670:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
at Module.load (internal/modules/cjs/loader.js:612:32)
at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
at Function.Module._load (internal/modules/cjs/loader.js:543:3)at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
at startup (internal/bootstrap/node.js:238:19)
注意 : 有时在线编译器可能无法给我们预期的结果,因此我们在本地执行上述代码。
当我们编译并运行代码时,函数 tracker.report() 将在输出中显示一个对象数组。
[
{
message: 'Expected the func function to be executed 5 time(s) but was executed 0 time(s).',
actual: 0,
expected: 5,
operator: 'func',
stack: Error
at CallTracker.calls (node:internal/assert/calltracker:44:19)
at Object.<anonymous> (C:\Users\Lenovo\Desktop\JavaScript\nodefile.js:7:27)
at Module._compile (node:internal/modules/cjs/loader:1126:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
at Module.load (node:internal/modules/cjs/loader:1004:32)
at Function.Module._load (node:internal/modules/cjs/loader:839:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:17:47
}
]
{
message: 'Expected the func function to be executed 5 time(s) but was executed 0 time(s).',
actual: 0,
expected: 5,
operator: 'func',
stack: Error
at CallTracker.calls (node:internal/assert/calltracker:44:19)
at Object.<anonymous> (C:\Users\Lenovo\Desktop\JavaScript\nodefile.js:7:27)
at Module._compile (node:internal/modules/cjs/loader:1126:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
at Module.load (node:internal/modules/cjs/loader:1004:32)
at Function.Module._load (node:internal/modules/cjs/loader:839:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:17:47
}
]
例
在此示例中,
- 我们正在做与上述程序相同的一切。
- 但是在这段代码中,我们调用了包装函数 3 次,这与我们传递给 tracker.calls() 函数的确切参数的数字不匹配。
const assert = require('assert');
const tracker = new assert.CallTracker();
function func() {};
const callsfunc = tracker.calls(func, 5);
callsfunc();
callsfunc();
callsfunc();
console.log(tracker.report());
输出
/home/cg/root/63a002c52763b/main.js:3
const tracker = new assert.CallTracker();
^
TypeError: assert.CallTracker is not a constructor
at Object.<anonymous> (/home/cg/root/63a002c52763b/main.js:3:17)at Module._compile (internal/modules/cjs/loader.js:702:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
at Module.load (internal/modules/cjs/loader.js:612:32)
at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
at Function.Module._load (internal/modules/cjs/loader.js:543:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
at startup (internal/bootstrap/node.js:238:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)
const tracker = new assert.CallTracker();
^
TypeError: assert.CallTracker is not a constructor
at Object.<anonymous> (/home/cg/root/63a002c52763b/main.js:3:17)at Module._compile (internal/modules/cjs/loader.js:702:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
at Module.load (internal/modules/cjs/loader.js:612:32)
at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
at Function.Module._load (internal/modules/cjs/loader.js:543:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
at startup (internal/bootstrap/node.js:238:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)
注意: 有时在线编译器可能无法给我们预期的结果,因此我们在本地执行上述代码。
如果我们编译并运行代码,tracker.reports() 函数将在输出中显示一个对象数组。
[
{
message: 'Expected the func function to be executed 5 time(s) but was executed 3 time(s).',
actual: 3,
expected: 5,
operator: 'func',
stack: Error
at CallTracker.calls (node:internal/assert/calltracker:44:19)
at Object.<anonymous> (C:\Users\Lenovo\Desktop\JavaScript\nodefile.js:7:27)
at Module._compile (node:internal/modules/cjs/loader:1126:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
at Module.load (node:internal/modules/cjs/loader:1004:32)
at Function.Module._load (node:internal/modules/cjs/loader:839:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:17:47
}
]
{
message: 'Expected the func function to be executed 5 time(s) but was executed 3 time(s).',
actual: 3,
expected: 5,
operator: 'func',
stack: Error
at CallTracker.calls (node:internal/assert/calltracker:44:19)
at Object.<anonymous> (C:\Users\Lenovo\Desktop\JavaScript\nodefile.js:7:27)
at Module._compile (node:internal/modules/cjs/loader:1126:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
at Module.load (node:internal/modules/cjs/loader:1004:32)
at Function.Module._load (node:internal/modules/cjs/loader:839:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:17:47
}
]
例
在下面的以下示例中,我们按照预期调用包装函数,以输入我们传入 tracker.calls() 函数的确切参数的数字。
const assert = require('assert');
const tracker = new assert.CallTracker();
function func() {};
const callsfunc = tracker.calls(func, 5);
callsfunc();
callsfunc();
callsfunc();
callsfunc();
callsfunc();
console.log(tracker.report());
输出
/home/cg/root/63a002c52763b/main.js:3
const tracker = new assert.CallTracker();
^
TypeError: assert.CallTracker is not a constructor
at Object.<anonymous> (/home/cg/root/63a002c52763b/main.js:3:17)
at Module._compile (internal/modules/cjs/loader.js:702:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
at Module.load (internal/modules/cjs/loader.js:612:32)
at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
at Function.Module._load (internal/modules/cjs/loader.js:543:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
at startup (internal/bootstrap/node.js:238:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)
const tracker = new assert.CallTracker();
^
TypeError: assert.CallTracker is not a constructor
at Object.<anonymous> (/home/cg/root/63a002c52763b/main.js:3:17)
at Module._compile (internal/modules/cjs/loader.js:702:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
at Module.load (internal/modules/cjs/loader.js:612:32)
at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
at Function.Module._load (internal/modules/cjs/loader.js:543:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
at startup (internal/bootstrap/node.js:238:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)
注意: 有时在线编译器可能无法给我们预期的结果,因此我们在本地执行上述代码。
因此,如果我们编译并运行代码,tracker.report() 函数将返回一个空的对象数组。
[]