不可变的 Chai 断言错误,而预期等于结果

immutable Chai Assertion error while expected equals result

我一直在使用 node/redux,在使用 chai 进行测试时我有以下结果:

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()。 感谢您的帮助。

我认为您的断言类型错误。 assert.equal(和类似的)通常测试两个东西是否是 "the same"。在对象的情况下,如果它不是完全相同的对象,则不成立。即使内容相同。为您的断言框架寻找类似 "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')

我找到原因了,而不是:

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

[] 在不需要的地方创建一个附加列表。