Reputation: 147
How can I test that a stubbed function is called with an object in a certain shape as the argument?
For example, I was trying to do something like
cy.get('@submitStub').should('have.been.calledWithMatch', {
dateRange: {
startDate: `The actual value here doesn't matter`,
endDate: '',
}
});
Of course the above code doesn't work as expected. Can anyone help? Thank you!
Upvotes: 3
Views: 2242
Reputation: 7284
You can do:
describe('test', () => {
it('test', () => {
const obj = {
method () {}
};
cy.stub(obj, 'method').as('stubbed')
obj.method({
dateRange: {
startDate: `The actual value here doesn't matter`,
endDate: '',
}
});
const isNotUndefined = Cypress.sinon.match( val => {
return val !== undefined;
});
cy.get('@stubbed').should('have.been.calledWithMatch', {
dateRange: {
startDate: isNotUndefined,
endDate: isNotUndefined,
}
});
// or
cy.get('@stubbed').should('have.been.calledWithMatch', {
dateRange: Cypress.sinon.match( obj => {
return obj &&
'startDate' in obj &&
'endDate' in obj
})
});
// or
cy.get('@stubbed').should('have.been.calledWithMatch', {
dateRange: Cypress.sinon.match.has("startDate")
.and(Cypress.sinon.match.has("endDate"))
});
// or
cy.get('@stubbed').should('have.been.calledWithMatch', arg1 => {
// do whatever comparisons you want on the arg1, and return `true` if
// it matches
return true;
});
});
});
Upvotes: 6