本文介绍了Chai deep包含嵌套对象的断言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图断言一个对象包含另一个(ei深度等于不能使用),但似乎严格检查嵌套的对象。
代码示例:
describe('Meta',function(){
it('对象应包含克隆副本',function(){
var obj = {a:1,b:'2',c:{a:2,b:'2'}};
return expect(obj).deep .contains(JSON.parse(JSON.stringify(obj)));
});
});
错误讯息:
AssertionError:期望{a:1,b:'2',c:{a:2,b:'2'}}以使属性'c'为{a:2,b:' 2'},但得到{a:2,b:'2'}
有没有办法使用深度相等功能进行包含?
解决方案
尝试使用eql:
expect(obj).to.deep.eql(JSON.parse(JSON.stringify(obj)));
eql比较对象中的值。
I'm trying to assert that a object contains another one(e.i. deep equal cannot be use), but it seems that the nested ones are checked strictly.
Code example:
describe('Meta', function () {
it('object should contains a cloned copy', function () {
var obj = {a: 1, b: '2', c: {a: 2, b: '2'}};
return expect(obj).deep.contains(JSON.parse(JSON.stringify(obj)));
});
});
Error message:
AssertionError: expected { a: 1, b: '2', c: { a: 2, b: '2' } } to have a property 'c' of { a: 2, b: '2' }, but got { a: 2, b: '2' }
Is there any way to do a "contains" with "deep equal" functionality?
解决方案
Instead of using contains, try using eql:
expect(obj).to.deep.eql(JSON.parse(JSON.stringify(obj)));
eql compares the values in the object.
That should do the trick.
这篇关于Chai deep包含嵌套对象的断言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!