1

I can explain my question in one image.

In unit test how can I compare original object with scope copy and avoid angular $$hashKey

In my unit test after some manipulations I want to check that $scope.data (first log message) is equal to original data object (second log message). I use mocha + chai and

expect(innerScope.data).to.deep.equal(data);

It is used in directive, so angular adds $$hashKey's to each object and of course my expectation is wrong. How can I test it in other way?

Boris Zagoruiko
  • 12,705
  • 15
  • 47
  • 79

2 Answers2

3

I know this is not a new question but the jasmine.objectContaining method would help here Jasmine

Connor Wyatt
  • 166
  • 11
0

There's only 2 ways I can think of:

1: Delete the $$hashKey property from the innerScope.data objects before the comparison:

innerScope.data.forEach(function (o) { delete o.$$hashKey });
expect(innerScope.data).to.deep.equal(data);

2: Loop through the objects and test that each object in innerScope.data contains the corresponding object in the original data, this will ignore any extra properties on the innerScope.data objects:

for (var i = 0; i < innerScope.data.length; i++) {
    expect(innerScope.data[i]).to.contain(data[i]);
}
Jason Watmore
  • 4,521
  • 2
  • 32
  • 36