【问题标题】:Why does this test on a selector (reselect library) fail?为什么对选择器(重新选择库)的测试会失败?
【发布时间】:2017-03-11 01:48:33
【问题描述】:

根据问题的标题。

.../selector.js

const makeSelectUserData = () => createSelector(
  selectProfile,
  (profileState) => profileState.get('userData')
);

.../tests/selectors.test.js

it('should select the current user', () => {
    const mockedState = fromJS({
      profile: {
        userData: {
          firstName: "X",
          lastName: "Y",
          gender: "Male",
          email: "a@b.cd",
        },
      },
    });
    console.log(userDataSelector(mockedState));
    expect(userDataSelector(mockedState)).toEqual(user);
  });

console.log ...打印:

Map { "firstName": "X", "lastName": "Y", "gender": "Male", "email": "a@b.cd" }

这就是为什么,但我不明白为什么其他选择器其中用户是一个字符串(例如)可以正常工作,而选择器返回一个字符串。

奇怪的是,选择器实际上表现正确,但测试却没有。

【问题讨论】:

    标签: automated-tests jestjs reselect


    【解决方案1】:

    我找到了解决办法。

    所需要的只是偶然期望(...

    expect(userDataSelector(mockedState).toJS()).toEqual(user);
    

    因为 fromJS() 将对象转换为不可变对象,选择器仍然返回该对象

    其他具有原生(原始)值的测试仍然有效,因为不可变不会改变它们

    【讨论】:

      猜你喜欢
      • 2013-02-23
      • 2014-04-25
      • 2019-02-17
      • 1970-01-01
      • 1970-01-01
      • 2012-07-05
      • 2019-04-02
      • 1970-01-01
      • 2017-05-14
      相关资源
      最近更新 更多