不可变的柴断言错误虽然预期等于结果

我一直在使用节点/ redux,我有以下testing与柴:

AssertionError: expected 'Map { "winos": List [ Map { "id": 1, "x": 1, "y": 1, "movable": false }, Map { "id": 2, "x": 2, "y": 2, "movable": false }, Map { "id": 5, "x": 5, "y": 5, "movable": false } ] }' to equal 'Map { "winos": List [ Map { "id": 1, "x": 1, "y": 1, "movable": false }, Map { "id": 2, "x": 2, "y": 2, "movable": false }, Map { "id": 5, "x": 5, "y": 5, "movable": false } ] }' 

我已经看到,这是一个已知的错误: https : //github.com/astorije/chai-immutable/issues/24 。 那里的人设法解决这个问题,使树中的所有东西都是不变的,但是我想我已经拥有了一切不变的东西。

我的代码如下:

 import {List, Map} from 'immutable'; import {expect} from 'chai'; export function addWino(state, wino) { return state.updateIn(['winos'], arr => arr.push(wino)); } describe('setWinos', () => { describe('addWino', () => { it('adds a Wino', () => { const wino = Map({ id: 5, x:5, y:5, movable: false }); const nextState = addWino(state, wino); expect(nextState).to.equal(Map({ winos: List.of([ Map({ id: 1, x:1, y:1, movable: false }) ], [ Map({ id: 2, x:2, y:2, movable: false }) ], [ Map({ id: 5, x:5, y:5, movable: false }) ]) })); }); }); } 

我也已经尝试过.eql().to.deep.equal() 。 谢谢你的帮助。

我find了原因,而不是:

 winos: List.of([ Map({ id: 1, x:1, y:1, movable: false }) ], [ Map({ id: 5, x:5, y:5, movable: false }) ]) })); 

我应该 :

 winos: List.of( Map({ id: 1, x:1, y:1, movable: false }), Map({ id: 5, x:5, y:5, movable: false }) ) })); 

[]不需要的地方,创build一个额外的列表。

我认为你的断言是错误的types。 assert.equal(和类似的)通常testing两个事物是否“相同”。 在对象的情况下,如果不是完全相同的对象,则不成立。 即使内容是相同的。 为断言框架寻找“deepEqual”之类的东西

看到这里: https : //tonicdev.com/lipp/deep-equal

 var assert = require('assert') var x = {a: 123} var y = x assert.equal(x, y) var u = {a: 123} assert.deepEqual(x, u, 'this is ok') assert.equal(x, u, 'this fails')