calledWith object key/value matching

5,503 views
Skip to first unread message

Tyler Clemens

unread,
Apr 17, 2015, 8:48:04 PM4/17/15
to sin...@googlegroups.com
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
 
});
});

Travis Kaufman

unread,
Apr 17, 2015, 8:53:58 PM4/17/15
to sin...@googlegroups.com
Try using sinon.match http://sinonjs.org/docs/#matchers
--
You received this message because you are subscribed to the Google Groups "Sinon.JS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sinonjs+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tyler Clemens

unread,
Apr 17, 2015, 8:58:05 PM4/17/15
to sin...@googlegroups.com
Perfect! Thanks, I had a hard time trying to come up with what to call this to google search it. 
Reply all
Reply to author
Forward
0 new messages