如何使用包括在柴?

时间:2017-05-31 15:15:19

标签: javascript mocha chai

我试图计算如何传递一个对象,然后使用来自chai的that.includesto.include检查对象是否在响应中

我写了一个简单的小提琴来检查我的问题:

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

带有.deep标志的

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

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)

第一次在这个框架上,可能是问题:)

1 个答案:

答案 0 :(得分:3)

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

无论如何,看起来这是一个错误。实现此功能的github票证位于here和相关提交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存储库中打开问题,并临时使用chai-subset包。