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

后端 未结 2 932
温柔的废话
温柔的废话 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:40

    This is not a classic scenario, since not many methods would call two methods sequentially, and I guess thats why it isn't supported. But, be calm, the solution is easy:

    var subject = { 
        method: function(one, two) {} 
    };
    
    var stub = sinon.stub(subject, 'method', function(one, two) { 
        one(); 
        two(); 
    });
    
    subject.method(
        function() { console.log('callback 1'); }, 
        function() { console.log('callback 2'); });
    

    Side note: This also gives the option for choosing if one or two should be called first.

提交回复
热议问题