Called with the approval of the object

The function in which I am tracking takes an object as an argument. I need to claim that the function was called with certain properties of the object.

for example: my SUT has:

function kaboom() { fn({ foo: 'foo', bar: 'bar', zap: function() { ... }, dap: true }); } 

and in my test I can do this:

 fnStub = sinon.stub(); kaboom(); expect(fnStub).to.have.been.called; 

and it works (it's good to know that fn was called). Now I need to make sure that the correct object has been passed to the function. I only care about the foo and bar properties, i.e. I need to match for specific properties of the argument. How?

upd: sinon.match () seems to work for simple objects. Let the bar rise, right?

What if I want to include the zap function in a statement? How to do it?

+7
javascript unit-testing sinon sinon-chai
source share
1 answer

Assuming you are using sinon-chai , you can use calledWith along with sinon.match to achieve this

 expect(fnStub).to.have.been.calledWith(sinon.match({ foo: 'foo', bar: 'bar' })); 
+12
source share

All Articles