繁体   English   中英

React 测试库:TypeError:无法读取未定义的属性“文章”

[英]React Testing library: TypeError: Cannot read property 'articles' of undefined

我正在尝试使用 react-testing-library 测试我的容器。

const middlewares = [thunk.withExtraArgument({})];
const mockStore = configureMockStore(middlewares);

const storeState = {
 articles: 
    [
        {
            id: "9b565b11-7311-5b5e-a699-97873dffb364",
            title: "jsdhahd",
            body: "jsahdjadshajhd",
            link: "https://www.google.com",
            media: "media.jpg"
        },
    ],
loading: false,
error: ''
};

describe('<Homepage />', () => {
  let store;

  beforeEach(() => {
    store = mockStore(storeState);
});

afterEach(() => {
    store.clearActions();
});

it('should render correctly', () => {
    const container = render(<Provider store={store}>{<Homepage />}</Provider>);

    expect(container).toMatchSnapshot();
});

我得到这个错误:错误

任何人都可以帮忙吗? 嘲笑 redux 商店似乎是一个问题,但我不知道如何解决它。 我在减速器中有文章、加载和错误的初始状态。 在我的 App.js 文件中,我用<Provider store={store}>包装了<App/> <Provider store={store}>

export const store = createStore(
  rootReducer,
  applyMiddleware(thunk)
)

ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>, document.getElementById('root')
);

错误发生在我创建storeState对象的过程中。

正确做法:

const storeState = {
   reducer: {
      articles: 
        [
         {
            id: "9b565b11-7311-5b5e-a699-97873dffb364",
            title: "jsdhahd",
            body: "jsahdjadshajhd",
            link: "https://www.google.com",
            media: "media.jpg"
        },
       ],
     loading: false,
     error: ''
};

我认为它应该是state.articles而不是state.reducer.articles中的mapStateToProps

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM