Hello everyone,
I'm trying to use calledWith on a spy to check if it was called with an object, that contained a key with a specific value. Something like the following:
sinon.assert.calledWith(mySpy, { foo: 'bar' });
But mySpy is actually being called with something like this:
mySpy({ foo: 'bar', foo2: 'bar2'})
Is there a way to do "partial" matching on a object that is being passed as a parameter, or do I have to check against the entire object?
Here's a complete example:
var myObj = {
myFunction: function(params) {
return params;
}
}
describe('myObj myFunction', function () {
beforeEach(function () {
sinon.spy(myObj, 'myFunction');
});
afterEach(function () {
myObj.myFunction.restore();
});
it('calls myFunction with a single property', function () {
sinon.assert.notCalled(myObj.myFunction)
myObj.myFunction({foo: 'bar'})
sinon.assert.called(myObj.myFunction)
});
it('calls myFunction with two properties', function () {
myObj.myFunction({foo: 'bar', foo2: 'bar2'})
sinon.assert.called(myObj.myFunction)
sinon.assert.calledWith(myObj.myFunction, {foo: 'bar', foo2: 'bar2'}) // works
sinon.assert.calledWith(myObj.myFunction, {foo: 'bar'}) // fails
});
});