Chai test array of objects to “contain something like” an object submatch

微笑、不失礼 提交于 2019-12-31 01:39:09

问题


Ok. I've tried to read other questions here but still didn't find a straightforward answer.

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.

  • to chain after the .be.an('array') and
  • to provide only the partial object as a parameter (unlike chai-subset).

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.


回答1:


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'});



回答2:


Not sure why you dismissed chai-subset as this seems to work:

expect(data).to.be.an("array").to.containSubset([{ name: "test" }]);


来源:https://stackoverflow.com/questions/44595658/chai-test-array-of-objects-to-contain-something-like-an-object-submatch

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