2

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.

  1. 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.
  2. 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.
Aamer Rasheed
  • 115
  • 3
  • 14

1 Answers1

3

From the sinon.match docs:

Requires the value to be not null or undefined and have at least the same properties as expectation.


From the sinon.assert.match docs:

Uses sinon.match to 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
})
Brian Adams
  • 43,011
  • 9
  • 113
  • 111