• Node.js Video Tutorials

Node.js - assert.report() 函数



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)

注意 − 有时在线编译器可能无法提供预期的结果,因此我们会在本地执行上述代码。

当我们编译并运行代码时,函数 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
   }
]

示例

在示例中,

  • 我们执行的操作与上述程序相同。

  • 但是在此代码中,我们调用了包装函数 3 次,这与我们传递给 tracker.calls() 函数的 exact 参数不匹配。

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)

注意 − 有时在线编译器可能无法提供预期的结果,因此我们会在本地执行上述代码。

如果我们编译并运行代码,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
   }
]

示例

在下面的示例中,我们按预期调用包装函数,次数与我们传递给 tracker.calls() 函数的 exact 参数相同。

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)

注意 − 有时在线编译器可能无法提供预期的结果,因此我们会在本地执行上述代码。

因此,如果我们编译并运行代码,tracker.report() 函数将返回一个空的对象数组。

[]
nodejs_assert_module.htm
广告

© . All rights reserved.