Can sinon stub withArgs match some but not all arguments

deitch picture deitch · Oct 18, 2014 · Viewed 39.2k times · Source

I have a function I am stubbing that gets called with multiple arguments. I want to check just the first argument. The rest are callback function, so I want to leave them alone. Thus, I might have the following 2 calls, using ajax as an example:

method.get = sinon.stub();
method.get(25,function(){/* success callback */},function(){/* error callback */});         
method.get(10,function(){/* success callback */},function(){/* error callback */});

I cannot use method.get.calls... because then it doesn't differentiate between the first one get(25) and the second get(10). But if I use method.get.withArgs(25).calls... then it does not match either, because withArgs() matches all arguments, which this does not (and never could, with callbacks like that).

How do I get sinon stubs to check and set responses based on just the 1st arg?

Answer

Andrew Radford picture Andrew Radford · Nov 3, 2014

https://sinonjs.org/releases/latest/matchers/#sinonmatchany

You can use sinon.match.any:

method.get.withArgs(25, sinon.match.any, sinon.match.any);