简体   繁体   English

Chai期望有更多的人将不与Chai-Immutable合作

[英]Chai Expect a deep array to include not working with Chai-Immutable

AssertionError: expected 'List [ List [ 0, "a", 0, 0 ], List [ 0, 0, 0, 0 ], List [ 0, 0, 0, 0 ], List [ 0, 0, 0, 0 ] ]' to include 'a'

I use Chai and chai-immutable . 我使用Chaichai-immutable

I am calling: 我正在打电话:

expect(nextState).to.deep.include("a");

Why doesn't this work? 为什么不起作用?

Note that even though .include() is overridden by chai-immutable , it behaves just like Chai does on native arrays: 请注意,即使.include()chai-immutable覆盖,它的行为也像Chai在本机数组上一样:

Consider: 考虑:

expect([
  [0, "a", 0, 0],
  [0, 0, 0, 0],
  [0, 0, 0, 0],
  [0, 0, 0, 0],
]).to.deep.include("a");

This fails with: 失败的原因是:

 AssertionError: expected [ Array(4) ] to include 'a'

To solve this, I see at least 2 ways to do so (as far as I know) using Chai alone. 为了解决这个问题,据我所知,我发现至少有两种方法可以单独使用Chai。 There might also be Chai plugins to help you with this. 可能还会有Chai插件来帮助您。

Using .flatten 使用.flatten

Immutable.js List s come with .flatten() which would flatten your matrix of List s into a single List : Immutable.js List带有.flatten() ,它将把List的矩阵展平为单个List

expect(new List([
  new List([0, "a", 0, 0]),
  new List([0, 0, 0, 0]),
  new List([0, 0, 0, 0]),
  new List([0, 0, 0, 0]),
]).flatten()).to.deep.include("a");

A failure would output like this: 失败将输出如下:

AssertionError: expected 'List [ 0, "a", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]' to include 'z'

Using .satisfy 使用.satisfy

Chai has a .satisfy() assertion that allows for more manual control: Chai有一个.satisfy()断言,可以进行更多手动控制:

expect(new List([
  new List([0, "a", 0, 0]),
  new List([0, 0, 0, 0]),
  new List([0, 0, 0, 0]),
  new List([0, 0, 0, 0]),
])).to.satisfy(matrix => matrix.some(row => row.includes("a")));

This avoids flattening the value being tested against, but because chai-immutable does not override .satisfy() , the output in case of failure is not too pretty: 这样可以避免使测试值变平,但是由于chai-immutable不会覆盖.satisfy() ,因此失败时的输出不会太漂亮:

AssertionError: expected { Object (size, _origin, ...) } to satisfy [Function]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM