简体   繁体   English

在单元测试中,如何将原始对象与合并范围副本进行比较,避免出现角度$$ hashKey

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

I can explain my question in one image. 我可以用一张图片解释我的问题。

在单元测试中,如何将原始对象与合并范围副本进行比较,避免出现角度$$ 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). 在进行一些操作后的单元测试中,我想检查$scope.data (第一条日志消息)是否等于原始data对象(第二条日志消息)。 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. 它在指令中使用,因此angular将$$ hashKey添加到每个对象,当然我的期望是错误的。 How can I test it in other way? 如何以其他方式测试它?

我知道这是不是一个新问题,但jasmine.objectContaining方法将帮助这里茉莉花

There's only 2 ways I can think of: 我只能想到2种方式:

1: Delete the $$hashKey property from the innerScope.data objects before the comparison: 1:在比较之前,从innerScope.data对象中删除$$ hashKey属性:

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: 2:循环遍历对象,并测试innerScope.data中的每个对象在原始数据中都包含相应的对象,这将忽略innerScope.data对象上的任何其他属性:

for (var i = 0; i < innerScope.data.length; i++) {
    expect(innerScope.data[i]).to.contain(data[i]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM