Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript TypeError:routingState.equals不是函数| Jest测试用例_Javascript_Reactjs_React Redux_Jestjs_Enzyme - Fatal编程技术网

Javascript TypeError:routingState.equals不是函数| Jest测试用例

Javascript TypeError:routingState.equals不是函数| Jest测试用例,javascript,reactjs,react-redux,jestjs,enzyme,Javascript,Reactjs,React Redux,Jestjs,Enzyme,这是我的selector.js文件:- const makeSelectLocationState = () => { let prevRoutingState; let prevRoutingStateJS; return (state) => { const routingState = state.get('route'); // or state.route if (!routingState.equals(prevRoutingState))

这是我的selector.js文件:-

const makeSelectLocationState = () => {
  let prevRoutingState;
  let prevRoutingStateJS;

  return (state) => {
    const routingState = state.get('route'); // or state.route

    if (!routingState.equals(prevRoutingState)) {
      prevRoutingState = routingState;
      prevRoutingStateJS = routingState.toJS();
    }

    return prevRoutingStateJS;
  };
};
下面是我编写的测试用例:-

it('makeSelectLocationState function testing', () => {
    const selectLocationState = makeSelectLocationState();
    const mockedState = fromJS({
      route: '',
    });
    expect(selectLocationState(mockedState)).toEqual('');
  });
使用jest运行测试用例时,我遇到以下错误:-

TypeError: routingState.equals is not a function
问题在于:

const routingState = state.get('route'); 
感觉您没有获得routingState,可能未定义,因此您将收到上述错误消息。因此请检查routingState的日期。

在测试文件中,将mockedState传递给selectLocationState

selectLocationState(mockedState)
但是,makeSelectLocationState函数没有属性。可能更改为:

const makeSelectLocationState = (state) => {}
看起来
route
是一个字符串(空字符串)。字符串没有
equals
方法,因此
routingState.equals()
不存在

也许您可以将
mockedState.route
设置为具有
equals
功能的对象:

const mockedState = fromJS({
  route: { equals: item => item === item },
});

TypeError:无法读取未定义的属性“equals”。在做了更改之后,我得到了上面的错误@root@Shwetank请在makeSelectLocationState中提供console.log(routingState)的输出。console.log(routingState)的输出为映射{size:1、\u root:ArrayMapNode、\uuuuOwnerId:undefined、\uuuuuuHash:undefined、\uuuuuuuu Changed:false}@root@Shwetank虽然routingState的类型是Map,和Map实际上没有equals函数。您可以使用它:routingState.toString()===prevRoutingState.toString()或使用forEach检查routingState的每个项目。
const mockedState = fromJS({
  route: { equals: item => item === item },
});