Chai deep contains assertion on nested objects

纵然是瞬间 提交于 2019-12-23 09:44:08

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/29368318/chai-deep-contains-assertion-on-nested-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!