當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。