Javascript TypeError:无法读取属性';路径名';react/redux测试中未定义的
我正在测试一些react组件,这是一个基本的测试套件,只是为了知道组件是否正在渲染及其子组件 我正在使用Javascript TypeError:无法读取属性';路径名';react/redux测试中未定义的,javascript,unit-testing,reactjs,redux,Javascript,Unit Testing,Reactjs,Redux,我正在测试一些react组件,这是一个基本的测试套件,只是为了知道组件是否正在渲染及其子组件 我正在使用redux mock store创建存储,并使用{mount}在提供程序中装载容器,但即使模拟正确的存储,也始终会触发此错误: TypeError:无法读取未定义的属性“路径名” 这是我最基本的测试: import React from 'react'; import { mount } from 'enzyme'; import configureStore from 'redux-mock
redux mock store
创建存储,并使用{mount}
在提供程序中装载容器,但即使模拟正确的存储,也始终会触发此错误:
TypeError:无法读取未定义的属性“路径名”
这是我最基本的测试:
import React from 'react';
import { mount } from 'enzyme';
import configureStore from 'redux-mock-store';
import { Provider } from 'react-redux';
import App from '../containers/App.container';
describe('App', () => {
let wrapper;
const mockStore = configureStore([]);
const store = mockStore({
router: {
location: { pathname: '/home', query: {}, search: '' },
params: {}
}
});
console.log(store.getState());
beforeEach(() => {
wrapper = mount(
<Provider store={store}>
<App />
</Provider>
);
});
it('Should render app and container elements', () => {
expect(wrapper.find('.app').exists()).toBeTruthy();
expect(wrapper.find('.container').exists()).toBeTruthy();
});
it('Should render the navbar', () => {
expect(wrapper.find('nav').exists()).toBeTruthy();
});
});
我无法找出测试的问题,mockStore的格式正确:
有什么想法吗
更新: 考虑到这一点,我在rootReducer中没有位置的reducer/prop,但是,我只想通过子组件传递位置对象属性,
react redux router
在ownProps
参数中提供这些属性
奇怪的事实:在应用程序中记录location
属性会返回正确的对象
在测试中,始终未定义。。。(如错误所示)
这是我的简历:
import { combineReducers } from 'redux';
import { reducer as formReducer } from 'redux-form';
import { routerReducer } from 'react-router-redux';
import authReducer from './auth.reducer';
import analysisReportsReducer from './AnalysisReports.reducer';
import titleAnalysisReducer from './TitleAnalysis.reducer';
import postsReportsReducer from './PostsReports.reducer';
const rootReducer = combineReducers({
form: formReducer,
routing: routerReducer,
auth: authReducer,
analysis: titleAnalysisReducer,
analysis_reports: analysisReportsReducer,
posts: postsReportsReducer
});
export default rootReducer;
看起来您的位置对象的作用域位于路由器下方 您的测试可能正在获取window.location属性,假设测试是cli而不是在浏览器中,您的测试套件可能不会复制该属性 或许可以尝试:
<NavBar location={this.props.router.location.pathname} onLogoutClick={logout}/>
不,它不起作用。但是这是一个模拟,我只是使用locatios作为字符串来验证一些渲染…这是一个看起来很时髦的设置。我会在渲染方法返回之前设置一个断点,并探索范围以查看数据可能隐藏的位置。添加了一个更新,可能对所有这些都更有意义。。。我还是不明白:/
import { combineReducers } from 'redux';
import { reducer as formReducer } from 'redux-form';
import { routerReducer } from 'react-router-redux';
import authReducer from './auth.reducer';
import analysisReportsReducer from './AnalysisReports.reducer';
import titleAnalysisReducer from './TitleAnalysis.reducer';
import postsReportsReducer from './PostsReports.reducer';
const rootReducer = combineReducers({
form: formReducer,
routing: routerReducer,
auth: authReducer,
analysis: titleAnalysisReducer,
analysis_reports: analysisReportsReducer,
posts: postsReportsReducer
});
export default rootReducer;
<NavBar location={this.props.router.location.pathname} onLogoutClick={logout}/>