📜  Node.js 断言 tracker.verify()函数

📅  最后修改于: 2022-05-13 01:56:26.372000             🧑  作者: Mango

Node.js 断言 tracker.verify()函数

tracker.verify()方法用于验证与预期调用次数相比实际调用函数的次数。如果实际调用次数与预期调用次数不同,此函数将引发错误。

句法:

tracker.verify()

参数:此函数不接受任何参数。

返回值:返回void。

下面的示例说明了 nodejs 中的 assert tracker.verify()函数。

示例 1:

Javascript
const assert = require('assert');
  
// Creates call tracker.
const tracker = new assert.CallTracker();
  
function func() {}
  
// Returns a function that wraps func() that must be called exact times
// before tracker.verify().
const callsfunc = tracker.calls(func, 2);
  
// called func through callsfunc only 1 time
callsfunc();
  
console.log(tracker.verify());


Javascript
const assert = require('assert');
  
// Creates call tracker.
const tracker = new assert.CallTracker();
  
function func() {}
  
// Returns a function that wraps func() that must be called exact times
// before tracker.verify().
const callsfunc = tracker.calls(func, 2);
  
// called func through callsfunc only 2 times
callsfunc();
callsfunc();
  
console.log(tracker.verify());


输出:

示例 2:

Javascript

const assert = require('assert');
  
// Creates call tracker.
const tracker = new assert.CallTracker();
  
function func() {}
  
// Returns a function that wraps func() that must be called exact times
// before tracker.verify().
const callsfunc = tracker.calls(func, 2);
  
// called func through callsfunc only 2 times
callsfunc();
callsfunc();
  
console.log(tracker.verify());

输出:

参考: https://nodejs.org/api/assert.html#assert_tracker_verify