【问题标题】:immutable Chai Assertion error while expected equals result不可变的 Chai 断言错误,而预期等于结果
【发布时间】:2016-04-13 11:50:21
【问题描述】:

我一直在使用 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()。 感谢您的帮助。

【问题讨论】:

    标签: javascript node.js unit-testing chai chai-immutable


    【解决方案1】:

    我找到了原因,而不是:

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

    [] 不需要并创建一个附加列表。

    【讨论】:

    • 您应该将此答案标记为解决您的问题,因为这确实是问题:)
    【解决方案2】:

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

    【讨论】:

    • 我已经尝试过了,但我得到了相同的输出。我将其添加到主帖中。无论如何,谢谢。
    • 对不起,这对您没有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-16
    • 1970-01-01
    相关资源
    最近更新 更多