匹配Chai断言中的部分对象? [英] Match partial objects in Chai assertions?

查看:127
本文介绍了匹配Chai断言中的部分对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找匹配以下内容的最佳方法:

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

以上操作无效,因为C0存在于实际中,而不是预期中。简而言之,我希望通过PASS,但是我不确定如何在不编写大量自定义代码的情况下完成此操作...

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...

推荐答案

chai-subset chai-fuzzy 可能还会执行您要查找的内容。

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

Chai-subset应该像这样工作:

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

这篇关于匹配Chai断言中的部分对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆