简体   繁体   中英

sinon.js stub - How to stub async.map

I want to test following function.

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

Here I am using async.map, what I want is to stub. async.map takes 3 parameters, first array and second and third callback. I want to stub second callback as well and call third callback with test values. How to do it?

I tried:

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

But this executes second function and not third function, I tried using callsArg , but that also did not help, not sure that is relevant here or not.

See in Sinon docs

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

In your context it should be

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM