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'
我使用Chai和chai-immutable
.
我打电话给:
expect(nextState).to.deep.include("a");
为什么这不起作用?
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'
我使用Chai和chai-immutable
.
我打电话给:
expect(nextState).to.deep.include("a");
为什么这不起作用?
请注意,即使.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]