【问题标题】:Unit test in Node jsNode js 中的单元测试
【发布时间】:2018-11-03 12:27:07
【问题描述】:

我正在学习使用 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

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

谢谢

【问题讨论】:

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


    【解决方案1】:

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

    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);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-29
      • 1970-01-01
      • 2017-07-21
      • 2016-02-19
      • 1970-01-01
      • 1970-01-01
      • 2020-05-04
      • 2020-09-02
      相关资源
      最近更新 更多