当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Node.js new assert.CallTracker()用法及代码示例


new assert.CallTracker()

添加于:v14.2.0、v12.19.0

创建一个新的 CallTracker 对象,该对象可用于跟踪函数是否被调用了特定次数。必须调用tracker.verify() 才能进行验证。通常的模式是在 process.on('exit') 处理程序中调用它。

import assert from 'node:assert';
import process from 'node:process';

const tracker = new assert.CallTracker();

function func() {}

// callsfunc() must be called exactly 1 time before tracker.verify().
const callsfunc = tracker.calls(func, 1);

callsfunc();

// Calls tracker.verify() and verifies if all tracker.calls() functions have
// been called exact times.
process.on('exit', () => {
  tracker.verify();
});const assert = require('node:assert');

const tracker = new assert.CallTracker();

function func() {}

// callsfunc() must be called exactly 1 time before tracker.verify().
const callsfunc = tracker.calls(func, 1);

callsfunc();

// Calls tracker.verify() and verifies if all tracker.calls() functions have
// been called exact times.
process.on('exit', () => {
  tracker.verify();
});

相关用法


注:本文由纯净天空筛选整理自nodejs.org大神的英文原创作品 new assert.CallTracker()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。