vendredi 2 septembre 2016

Sinon.js calledWithExactly() dynamic number of arguments

Let's consider the following function:

function MyClass(...);
MyClass.prototype.funcToTest = function() {
    var args = arguments;
    ...
}

I wish to be able to test with what arguments this function was called with. In my test I do the following:

var obj = new MyClass();
sinon.spy(obj, 'funcToTest');
var args = [5, 3, 8];
MyClass.prototype.funcToTest.apply(obj, args);
sinon.assert.calledWithExactly(obj.funcToTest, args); // Here's the problem

The problem is that this assert fails and I'm not sure why, but I've checked obj.funcToTest.args and it equals an an array of the array passed, so for the above example it was equal:
[[5, 3, 8]]
So I've tried to change the assert to:

sinon.assert.calledWithExactly(obj.funcToTest, [args]);

but it fails as well. Anyone got an idea on how to deal with dynamic number of arguments being passed to a function and checking on what arguments the function was called with?

Aucun commentaire:

Enregistrer un commentaire