0
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'

我使用Chaichai-immutable.

我打电话给:

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

为什么这不起作用?

4

1 回答 1

0

请注意,即使.include()被 覆盖chai-immutable,它的行为就像 Chai 在本机数组上所做的那样:

考虑:

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

这失败了:

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

为了解决这个问题,我看到至少有两种方法(据我所知)单独使用 Chai。可能还有Chai 插件可以帮助您解决这个问题。

使用.flatten

Immutable.jsList附带.flatten()它会将您的 s 矩阵扁平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");

失败将输出如下:

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

使用.satisfy

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")));

这避免了扁平化被测试的值,但是因为chai-immutable没有覆盖.satisfy(),所以在失败的情况下输出不是太漂亮:

AssertionError: expected { Object (size, _origin, ...) } to satisfy [Function]
于 2016-12-17T07:06:41.313 回答