【问题标题】:Immutable - List.of()不可变 - List.of()
【发布时间】:2017-09-21 23:20:15
【问题描述】:

我正在关注这个使用 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));
}

摩卡输出

  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

【问题讨论】:

    标签: redux mocha.js immutable.js


    【解决方案1】:

    请检查您的测试:

    expect(nextState).to.equal(Map({
      albums: List.of({'Album 1': null}, {'Album 2': null}, {'Album 3': null})
    }));
    

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

    equal 断言目标严格 (===) 等于 给定值。

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

    仅当操作数引用相同的对象时,比较对象的表达式才为真。

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

    【讨论】:

      猜你喜欢
      • 2019-07-24
      • 2020-01-15
      • 1970-01-01
      • 1970-01-01
      • 2020-12-11
      • 2019-09-05
      • 2021-12-27
      • 2020-08-15
      • 1970-01-01
      相关资源
      最近更新 更多