Chai测试对象数组以“包含类似”的内容。对象子匹配 [英] Chai test array of objects to "contain something like" an object submatch
问题描述
好的。我在这里尝试阅读其他问题,但仍然找不到简单的答案。
Ok. I've tried to read other questions here but still didn't find a straightforward answer.
如何使用chai在数组中声明部分对象匹配?
类似以下内容:
How can I assert a partial object match in an array using chai? Something like the following:
var expect = require('chai').expect;
var data = [ { name: 'test', value: 'bananas' } ];
expect(data).to.be.an('array').that.contains.somethig.like({name: 'test'});
为了澄清,我的意图是尽可能地接近所提供的示例。
Just to clarify, my intention is to get as close to the example provided as possible.
- 链接到
.be.an('array')
和 - 仅提供部分对象作为参数(与 chai-子集)。
- to chain after the
.be.an('array')
and - to provide only the partial object as a parameter (unlike chai-subset).
我真的认为 expect(data).to.be.an('array')。that .deep.contains({name:'test'});
可以工作,但是由于不是部分匹配而失败,我有点儿被搞砸了。
I really thought that expect(data).to.be.an('array').that.deep.contains({name: 'test'});
would work, but it fails on not being a partial match and I'm kinda screwed there.
推荐答案
由于 chai-like@0.2.14
可以使用以下方法:
Since chai-like@0.2.14
the following approch will work:
var chai = require('chai'),
expect = chai.expect;
chai.use(require('chai-like'));
chai.use(require('chai-things')); // Don't swap these two
expect(data).to.be.an('array').that.contains.something.like({name: 'test'});
这篇关于Chai测试对象数组以“包含类似”的内容。对象子匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!