typemoq icon indicating copy to clipboard operation
typemoq copied to clipboard

It.isObjectWith partial deep comparison behavior is unexpected

Open VinGarcia opened this issue 6 years ago • 3 comments

I was creating a test and I found that the isObjectWith function would be perfect for me since I did not want to test all attributes of my object, only the ones that concerned that specific test.

However, this object also had a sub-object with optional attributes (some of which I wanted to verify its presence, others I didn't), and this is what behaved differently than what I expected: It seems it will ignore attributes on the root of the object if they are not present, but won't ignore attributes of nested objects if they are not present.

I have created a minimal code example to ilustrate the problem:

import { It, Mock, Times } from 'typemoq';

function myFunc(object: {}) {
  console.log('object', object);
}

const myFuncMock = Mock.ofInstance(myFunc);
myFuncMock
  .setup((m) => m(It.isAny()))
  .returns(() => Promise.resolve());

myFuncMock.object({ x:0, a: { b: 0, c: 0 } });

// Works
myFuncMock.verify((m) => m(
  It.isObjectWith({ x:0 }),
), Times.once());

// Works
myFuncMock.verify((m) => m(
  It.isObjectWith({ a: { b: 0, c: 0 } }),
), Times.once());

// Does not work:
myFuncMock.verify((m) => m(
  It.isObjectWith({ a: { b: 0 } }),
), Times.once());

Is this the expected behavior?

Also, thanks in advance for your attention and for the great library =]

VinGarcia avatar Sep 04 '18 17:09 VinGarcia

Facing the same issue here (using latest version)

sescotti avatar Sep 21 '18 12:09 sescotti

Did anybody come up with a solution yet?

Kampfmoehre avatar Jun 19 '19 06:06 Kampfmoehre

you can test out my solution with @parisholley/[email protected]

parisholley avatar Aug 26 '19 16:08 parisholley