简体   繁体   English

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

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

I'm trying to test my Container using react-testing-library.我正在尝试使用 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();
});

And I get this error: error我得到这个错误:错误

Can anyone help?任何人都可以帮忙吗? It seems a problem with mocking the redux store, but I don't know exactly how to solve it.嘲笑 redux 商店似乎是一个问题,但我不知道如何解决它。 I have in the reducer a initial state with the articles, loading, and error.我在减速器中有文章、加载和错误的初始状态。 And in my App.js file I'm wrapping the <App/> with the <Provider store={store}>在我的 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')
);

The error was on the way I was creating the storeState object.错误发生在我创建storeState对象的过程中。

Correct way:正确做法:

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.

相关问题 将 react-testing-library 添加到项目时遇到问题 - TypeError: Cannot read property 'then' of undefined - Trouble adding react-testing-library to a project - TypeError: Cannot read property 'then' of undefined 类型错误:无法在反应/redux 测试中读取未定义的属性“路径名” - TypeError: Cannot read property 'pathname' of undefined in react/redux testing 反应单元测试:TypeError:无法读取未定义的属性“ test” - React Unit Testing: TypeError: Cannot read property 'test' of undefined React - sinon 测试 - 获取 TypeError:无法读取未定义的属性“子字符串” - React - sinon testing - getting TypeError: Cannot read property 'substring' of undefined React - 使用 Jest 进行测试 - TypeError:无法读取未定义的属性“销毁” - React - Testing w/ Jest - TypeError: Cannot read property 'destroy' of undefined TypeError:无法读取 react-testing-library 中 null 的属性“已禁用” - TypeError: Cannot read property 'disabled' of null in react-testing-library 类型错误:无法读取 React 中未定义的属性“map” - TypeError: Cannot read property 'map' of undefined in React TypeError:无法使用React读取undefined的属性 - TypeError: Cannot read property of undefined using React 反应-未捕获的TypeError:无法读取未定义的属性“ 1” - React - Uncaught TypeError: Cannot read property '1' of undefined React Express TypeError:无法读取未定义的属性“ then” - React Express TypeError: Cannot read property 'then' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM