javascript Chai.js:对象包含/包含

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15144952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 23:41:02  来源:igfitidea点击:

Chai.js: Object contains/includes

javascriptarraysobjecttddchai

提问by David McMullin

Chaihas an includemethod. I want to test to see if an object contains another object. For example:

include办法。我想测试一个对象是否包含另一个对象。例如:

var origin = {
  name: "John",
  otherObj: {
    title: "Example"
  }
}

I want to use Chai to test if this object contains the following (which it does)

我想用 Chai 来测试这个对象是否包含以下内容(它确实包含)

var match = {
  otherObj: {
    title: "Example"
  }
}

Doing this does not appear to work:

这样做似乎不起作用:

origin.should.include(match)

回答by eagleeye

Hei, just published chai-subset. Check this out: https://www.npmjs.org/package/chai-subsetThis should work for you)

嘿,刚刚发布了柴子集。看看这个:https: //www.npmjs.org/package/chai-subset这应该适合你)

 var chai = require('chai');
 var chaiSubset = require('chai-subset');
 chai.use(chaiSubset);

 var obj = {
     a: 'b',
     c: 'd',
     e: {
         foo: 'bar',
         baz: {
             qux: 'quux'
         }
     }
 };

 expect(obj).to.containSubset({
     e: {
         foo: 'bar',
         baz: {
             qux: 'quux'
         }
     }
 });

回答by David McMullin

The include and contain assertions can be used as eitherproperty based language chains oras methods to assert the inclusion of an object in an arrayor a substring in a string. When used as language chains, they toggle the contain flag for the keys assertion. [emphasis mine]

在包括和包含断言可以用作任一基于属性的语言链作为方法来断言的对象的在包含阵列中一个或一个子字符串。当用作语言链时,它们会切换键断言的包含标志。[强调我的]

So if you're invoking include on an object (not an array or a string), then it only serves to toggle the contain flag for the keys assertion. By the looks of your example, testing for deep equality would make more sense, possibly checking for the key first.

因此,如果您在对象(不是数组或字符串)上调用包含,则它仅用于切换键断言的包含标志。根据您的示例,测试深度相等会更有意义,可能首先检查密钥。

origins.should.include.keys("otherObj");
origins.otherObj.should.deep.equal(match.otherObj);

Actually, now I browse the other examples, you would probably be happiest with this :

实际上,现在我浏览其他示例,您可能会对此感到最满意:

origins.should.have.deep.property("otherObj", match.otherObj)

回答by jc1

In chai 4.2.0, for example you can use deep include

例如,在 chai 4.2.0 中,您可以使用 deep include

chaijs doc examples:

chaijs 文档示例:

// Target array deeply (but not strictly) includes `{a: 1}`
expect([{a: 1}]).to.deep.include({a: 1});
expect([{a: 1}]).to.not.include({a: 1});

// Target object deeply (but not strictly) includes `x: {a: 1}`
expect({x: {a: 1}}).to.deep.include({x: {a: 1}});
expect({x: {a: 1}}).to.not.include({x: {a: 1}});

回答by Sebastien Horin

If you know the level of the subobject you can simply use:

如果您知道子对象的级别,您可以简单地使用:

expect(origin.otherObj).to.include(match.otherObj);

https://www.chaijs.com/api/bdd/

https://www.chaijs.com/api/bdd/

回答by machnicki

In Chai 1.5.0 you will find handy method

在 Chai 1.5.0 中你会发现方便的方法

includeDeepMembers

http://chaijs.com/releases/

http://chaijs.com/releases/