0

我正在使用 Tape.js 在节点 js 中学习单元测试,到目前为止,我只发现测试一个函数返回的结果很有用,但是如果一个回调被准确地调用了 n 次呢?

我有这个函数调用回调函数 n 次:

    const repeatCallback = (n, cb) => {
        for (let i = 0; i < n; i++) {
            cb();
        }
    }

module.exports = repeatCallback;

和磁带测试:

const repeatCallback = require('./repeatCallback.js');
    const test = require('tape');



    test('repeat callback tests', (t) => {
    t.plan(3);
        repeatCallback(3, () => {console.log('callack called');})
    });

我得到了错误:not ok 1 plan != count

如何在测试中更新计数以匹配已调用的次数?

谢谢

4

1 回答 1

0

只需计算函数被调用的次数:

const repeatCallback = require('./repeatCallback.js');
const test = require('tape');

test('repeat callback tests', (t) => {
  t.plan(1);
  let count = 0;
  repeatCallback(3, () => { count++; });
  t.equal(count, 3);
});
于 2018-05-24T10:13:27.557 回答