javascript - sinon.js stub - 如何 stub async.map

标签 javascript node.js unit-testing asynchronous sinon

我想测试以下功能。

var myFun = function (a, b, callback) {
   async.map(a, function (b, mapCallback) {
      //Do something with b => code I don't want to execute 
      mapCallback(null, res) 
   }, 
   function (err, output) {
        if (err) {
            Logger.error(err);
            return callback(err, null);
        }
        return callback(null,  output.filter(function(n){ return n != null }));
    });
}

这里我使用的是async.map,我想要的是 stub 。 async.map 采用 3 个参数,第一个数组以及第二个和第三个回调。我还想 stub 第二个回调并使用测试值调用第三个回调。怎么做?

我尝试过:

var mockAsync = sinon.stub(async, "map")
mockAsync.yields("Some error", null);

但这执行第二个函数而不是第三个函数,我尝试使用callsArg,但这也没有帮助,不确定这里是否相关。

最佳答案

参见Sinon docs

stub.callArg(argNum)
stub.callArgWith(argNum, [arg1, arg2, ...])

根据您的上下文,它应该是

mockAsync.callArgWith(1, "Some error", null)

关于javascript - sinon.js stub - 如何 stub async.map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38629948/

相关文章:

javascript - JavaScript/NodeJS是否会自动引发错误?

unit-testing - 如何使用有效负载获取模拟发布请求

c++ - 单元测试 ActiveX 控件 (C++)

javascript - 如何选择某些其他标签内的标签

node.js - "Error: Cannot find module ' 少 '"Node.js 模块加载首选项/顺序/缓存?

javascript - Bootstrap 从模式弹出窗口返回结果到每一行?

javascript - 添加代码智能到nodejs中的rest api链接

c# - 我怎样才能防止我的 NUnit 测试有时并行运行

javascript - JS : Reorganize objects inside an array based on value of a particular key (reduce?)

javascript - 堆叠条形图上的单击事件 - ChartJs