sinon.js stub - can you call more than one callback on a single stubbed function?

后端 未结 2 955
温柔的废话
温柔的废话 2021-02-20 06:24

If I have a stub for a function that takes 2 callbacks, how can I wire up sinon.js to call both callbacks when the stubbed function is invoked?

For exam

2条回答
  •  伪装坚强ぢ
    2021-02-20 06:39

    Why don't you skip sinon altogether?

    var obj = { stubMe: function(cb1, cb2) {} };
    var originalMethod = obj.stubMe;
    
    obj.stubMe = function(cv1, cb2) { 
      //Whatever logic you wish
      cb1(); 
      cb2(); 
    }; 
    
    //Do your test
    
    obj.stubMe = originalMethod; //Restore
    

    This way you can even continue to use sinon's APIs, if you wish:

    var stub = sinon.stub();
    obj.stubMe = function(cb1, cb2) { 
      stub.apply(stub, arguments);
      //Rest of whatever logic you wanted here
    };
    
    obj.stubMe();
    expect(stub.calledOnce()).to.be(true); //That would pass
    

    What'd you think?

提交回复
热议问题