如何在 Chai 中使用 include?

How to use include in Chai?

我正在尝试弄清楚如何传递对象并使用 chai

中的 that.includesto.include 检查对象是否在响应中

我写了一个简单的fiddle来检查我的问题:

https://jsfiddle.net/balexandre/4Loupnjk/2/

https://jsfiddle.net/balexandre/4Loupnjk/5/ 带有 .deep 标志

var e = {
  "results":  {
    "total_rejected_recipients": 0,
    "total_accepted_recipients": 1,
    "id":"102618457586465882"
  }
};

根据我的理解,e 对象实际上应该包含较小的对象...或者我遗漏了什么?

expect(e).to.include({
  "results":  {
    "total_rejected_recipients": 0,
    "total_accepted_recipients": 1
    }
});

但我收到错误消息:

assertionError: expected { Object (results) } to have property 'results' of { Object (total_rejected_recipients, total_accepted_recipients) }, but got { Object (total_rejected_recipients, total_accepted_recipients, ...) }
at Context.<anonymous> (:73:18)

第一次使用这个框架,可能是问题:)

首先,你应该使用 deep.include 断言,因为那里有一个深层对象。

无论如何,这看起来像是一个错误。实施此操作的 github 票证位于 here and the relevant commit here.

此断言的测试覆盖率在这里:

expect({foo: obj1, bar: obj2}).to.deep.include({foo: {a: 1}});
expect({foo: obj1, bar: obj2}).to.deep.include({foo: {a: 1}, bar: {b: 2}});
expect({foo: obj1, bar: obj2}).to.not.deep.include({foo: {a: 9}});
expect({foo: obj1, bar: obj2}).to.not.deep.include({foo: {z: 1}});
expect({foo: obj1, bar: obj2}).to.not.deep.include({baz: {a: 1}});
expect({foo: obj1, bar: obj2}).to.not.deep.include({foo: {a: 1}, bar: {b: 9}});

但是,它会在以下情况下中断:

expect({ foo: obj1, bar: obj2 }).to.deep.include({ foo: { a: 1 }, bar: { } });

最好在 chai repository, and temporary use the chai-subset 包中打开问题。