节点js

时间:2018-05-24 10:08:58

标签: javascript unit-testing node.js-tape

我正在使用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');})
    });

我收到错误:不好1计划!=计数

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

感谢

1 个答案:

答案 0 :(得分: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);
});