How to Match Some Values of Object Using Sinon Stub
I've Been Writing Test Using Sinon. During the Same I Wrote Stub Where Some Input Parameters Are Passed and an Object Is Returned. This Object Returns Some...
I've been writing test using sinon. During the same I wrote stub where some input parameters are passed and an object is returned. This object returns some values and a random date value generated by system at the time of execution. So need guidance on following.
- How can I handle the same as the matching arguments are static in nature and I don't know the possible value of the date generated by actual code.
- How can we skip certain key values of an object using sinon. i.e. say object has following values.
const object = {name: "abc", employeeNumber : "123"}I only want to check if name is "abc" and don't need to match employeeNumber.
1 Answer
From the sinon.match docs:
Requires the value to be not
nullorundefinedand have at least the same properties asexpectation.
From the sinon.assert.match docs:
Uses
sinon.matchto test if the arguments can be considered a match.
Example:
test('partial object match', () => {
const object = { name: "abc", employeeNumber : "123" };
sinon.assert.match(object, { name: 'abc' }); // SUCCESS
})