Match partial objects in Chai assertions?

99封情书 提交于 2019-12-19 05:07:02

问题


I am looking for the best way to match the following:

expect([
    {
        C1: 'xxx',
        C0: 'this causes it not to match.'
    }
]).to.deep.include.members([
    {
        C1: 'xxx'
    }
]);

The above doesn't work because C0 exists in the actual, but not the expected. In short, I want this expect to PASS, but I'm not sure how to do it without writing a bunch of custom code...


回答1:


chai-subset or chai-fuzzy might also perform what you're looking for.

Chai-subset should work like this:

expect([
  {
    C1: 'xxx',
    C0: 'this causes it not to match.'
  }
]).to.containSubset([{C1: 'xxx'}]);

Personally if I don't want to include another plugin I will use the property or keys matchers that chai includes:

([
  {
    C1: 'xxx',
    C0: 'this causes it not to match.'
  }
]).forEach(obj => {
  expect(obj).to.have.key('C1'); // or...
  expect(obj).to.have.property('C1', 'xxx');
});



回答2:


There are a few different chai plugins which all solve this problem. I am a fan of shallow-deep-equal. You'd use it like this:

expect([
    {
      C1: 'xxx',
      C0: 'this causes it not to match.'
    }
  ]).to.shallowDeepEqual([
    {
      C1: 'xxx'
    }
  ]);



回答3:


without plugins: http://chaijs.com/api/bdd/#method_property

  expect([
    {
      C1: 'xxx',
      C0: 'this causes it not to match.'
    }
  ]).to.have.deep.property('[0].C1', 'xxx');



回答4:


You can use pick and omit underscore functions to select/reject properties to test:

const { pick, omit } = require('underscore');

const obj = {
  C1: 'xxx',
  C0: 'this causes it not to match.',
};

it('tests sparse object with pick', () => {
  expect(pick(obj, 'C1')).to.eql({ C1: 'xxx' });
});

it('tests sparse object with omit', () => {
  expect(omit(obj, 'C0')).to.eql({ C1: 'xxx' });
});



回答5:


I believe the simplest (and certainly easiest) way would be to:

var actual=[
  {
    C1:'xxx',
    C0:'yyy'
  }
];

actual.forEach(function(obj){
  expect(obj).to.have.property('C1','xxx');
});



回答6:


I wrote chai-match-pattern and lodash-match-pattern to handle partial matching (and many more) deep matching scenarios.

var chai = require('chai');
var chaiMatchPattern = require('chai-match-pattern');
chai.use(chaiMatchPattern);

// Using JDON pattern in expectation
chai.expect([
  {
    C1: 'xxx',
    C0: 'this causes it not to match.'
  }
]).to.matchPattern([
  {
    C1: 'xxx',
    '...': ''
  }
]);

// Using the slightly cleaner string pattern notation in expectation
chai.expect([
  {
    C1: 'xxx',
    C0: 'this causes it not to match.'
  }
]).to.matchPattern(`
  [
    {
      C1: 'xxx',
      ...
    }
  ]
  `
);



回答7:


Slightly updated version of #RobRaisch because for empty comparison giving error because '' not equal ""

let expected = {
            dateOfBirth: '',
            admissionDate: '',
            dischargeDate: '',
            incidentLocation: null
        };
        Object.keys(expected).forEach(function(key) {
            expect(actual[key]).to.equal(expected[key]);
        });


来源:https://stackoverflow.com/questions/29532981/match-partial-objects-in-chai-assertions

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