不可变 - List.of()

时间:2017-09-21 23:20:16

标签: redux mocha immutable.js

我正在关注这个使用Immutable的redux教程 - http://teropa.info/blog/2015/09/10/full-stack-redux-tutorial.html,尝试制作我自己的应用版本以更好地理解Immutable和Redux,我不明白为什么我的测试失败了。 Mocha的输出显示预期值相同,但我认为订单可能导致测试失败。但是,Mocha的输出不显示List对象的实际顺序。 List.of()是对象数组的正确方法吗?

core_spec.js

import {List, Map} from 'immutable';
import {expect} from 'chai';
import {initAlbums, next, rate} from '../src/core';

describe('application logic', () => {

  describe('initAlbums', () => {

    it('adds the albums to the state', () => {
      const state = Map();
      const albums = List.of({'Album 1': null}, {'Album 2': null}, {'Album 3': null});
      const nextState = initAlbums(state, albums);
      expect(nextState).to.deep.equal(Map({
        albums: List.of({'Album 1': null}, {'Album 2': null}, {'Album 3': null})
      }));
    });

  });

}); 

core.js

import {List, Map} from 'immutable';

export function initAlbums(state, albums) {
  return state.set('albums', List(albums));
}

Mocha输出

  1 failing

  1) application logic initAlbums adds the albums to the state:

      AssertionError: expected 'Map { "albums": List [ [object Object], [object Object], [object Object] ] }' to equal 'Map { "albums": List [ [object Object], [object Object], [object Object] ] }'
      + expected - actual

1 个答案:

答案 0 :(得分:0)

请检查您的测试:

chkValue = chkValue.replace(/[^\w]/g, "");

你期望平等。 关注http://chaijs.com/api/bdd/#method_equal

  

相等的断言是目标严格(===)等于   给予val。

以下:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators

  

如果操作数引用相同的对象,则只有比较对象的表达式才会生效。

这就是你的测试失败的原因。您应该使用 .deep http://chaijs.com/api/bdd/#method_deep

相关问题