2016-04-13 42 views
1

我已經使用節點/終極版,我已經同時用柴測試以下:不可改變柴斷言錯誤而預期的equals導致

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()。 感謝您的幫助。

回答

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

[]哪裏不需要,並創建一個附加列表。

+0

您應該將此答案標記爲解決您的問題,因爲這確實是問題:) – astorije

0

我認爲你的斷言是錯誤的類型。 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') 
+0

我已經試過了,但我得到了相同的輸出。我將它添加到主帖子中。不管怎麼說,多謝拉。 – Flotos

+0

對不起,這並沒有幫助你。 – lipp

相關問題