Skip to content Skip to sidebar Skip to footer

Has Been Called With Object Assertion

Function I'm spying on, receives object as an argument. I need to assert that the function been called with certain properties of the object. e.g: my SUT has: function kaboom() {

Solution 1:

Assuming you're 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'
}));

Solution 2:

To achieve called.with partial object check, you can also use chai-spies-augment package (https://www.npmjs.com/package/chai-spies-augment) :

Usage (please, notice to .default after importing):

const chaiSpies = require('chai-spies');
const chaiSpiesAugment = require('chai-spies-augment').default;

chai.use(chaiSpies);
chai.use(chaiSpiesAugment);

usage :

const myStub = { myFunc: () => true };
const spy1 = chai.spy.on(myStub, 'myFunc');
myStub.myFunc({ a: 1, b: 2 });

expect(spy1).to.have.been.called.with.objectContaining({ a: 1 });

Post a Comment for "Has Been Called With Object Assertion"