Skip to content
Advertisement

How to return based on actual arguments being sent?

Let’s say we have a function with two arguments. It’s being called many times with different arguments on every call. So, its impossible to stub it with withArgs option.

JavaScript

I want to write a stub function which will check the actual arguments being passed (args1, args2) and return a static response with a switch case. Something on the following lines:

JavaScript

So, how to return based on actual arguments?

Advertisement

Answer

You can use stub(obj, 'meth').callsFake(fn) to dynamically check what passed in and response to it.

An example would be:

JavaScript

Note that for an earlier version of Sinon < 3.0.0 You should use var stub = sinon.stub(object, "method", fn); instead.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement