首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我的酶测试不及格?

为什么我的酶测试不及格?
EN

Stack Overflow用户
提问于 2017-07-31 21:02:52
回答 2查看 479关注 0票数 0

我有一个简单的测试,我试图通过酶/摩卡/茶。下面是错误:

代码语言:javascript
复制
  1) <PostList /> should have a container for holding posts:
     TypeError: Cannot read property 'length' of undefined
      at PostList.render (D:/mydocs/webdev/gitprojs/ReactBlogFinal/views/PostList/PostList.jsx:13:9)
      at node_modules\react-test-renderer\lib\shallow\ReactCompositeComponent.js:795:21
      at measureLifeCyclePerf (node_modules\react-test-renderer\lib\shallow\ReactCompositeComponent.js:75:12)
      at ShallowComponentWrapper._renderValidatedComponentWithoutOwnerOrContext (node_modules\react-test-renderer\lib\shallow\ReactCompositeComponent.js:794:25)
      at ShallowComponentWrapper.performInitialMount (node_modules\react-test-renderer\lib\shallow\ReactCompositeComponent.js:361:30)
      at ShallowComponentWrapper.mountComponent (node_modules\react-test-renderer\lib\shallow\ReactCompositeComponent.js:257:21)
      at Object.mountComponent (node_modules\react-test-renderer\lib\shallow\ReactReconciler.js:45:35)
      at ReactShallowRenderer._render (node_modules\react-test-renderer\lib\shallow\ReactShallowRenderer.js:138:23)
      at _batchedRender (node_modules\react-test-renderer\lib\shallow\ReactShallowRenderer.js:85:12)
      at Object.batchedUpdates (node_modules\react-test-renderer\lib\shallow\ReactDefaultBatchingStrategy.js:60:14)
      at Object.batchedUpdates (node_modules\react-test-renderer\lib\shallow\ReactUpdates.js:97:27)
      at ReactShallowRenderer.render (node_modules\react-test-renderer\lib\shallow\ReactShallowRenderer.js:112:18)
      at ReactShallowRenderer.render (node_modules\enzyme\build\react-compat.js:171:37)
      at node_modules\enzyme\build\ShallowWrapper.js:128:26
      at ReactDefaultBatchingStrategyTransaction.perform (node_modules\react-test-renderer\lib\shallow\Transaction.js:143:20)
      at Object.batchedUpdates (node_modules\react-test-renderer\lib\shallow\ReactDefaultBatchingStrategy.js:62:26)
      at Object.batchedUpdates (node_modules\react-test-renderer\lib\shallow\ReactUpdates.js:97:27)
      at ReactShallowRenderer.unstable_batchedUpdates (node_modules\react-test-renderer\lib\shallow\ReactShallowRenderer.js:130

下面是测试结果。我只是测试一下是否存在div:

代码语言:javascript
复制
import React from 'react';
import { mount, shallow, render } from 'enzyme';
import { expect } from 'chai';

import PostList from './PostList';


describe('<PostList />', () => {

  it('should have a container for holding posts', () => {
    const wrapper = shallow(<PostList />);

    expect(wrapper.find('div')).to.have.length(1);
  });
});

这是组件。这个组件将是博客文章的容器,if语句测试post对象的长度,如果通过,我的post div将开始生成。所以我知道这些帖子一开始是不确定的。在React中,在render方法中使用这样的逻辑是不是一种糟糕的做法?或者这是酶方面的问题?

代码语言:javascript
复制
import React from 'react';

export default class PostList extends React.Component {
  render() {
    const posts = this.props.posts;
    let postDivs;

    if (posts.length !== 0) {
      postDivs = posts.map(post => (
        <div className="post-item" key={post._id}>
          <h2 className="post-title">{post.title}</h2>
          <h3 className="post-date">{post.date}</h3>
          <h3 className="author">{post.author}</h3>
          <p className="body">{post.body}</p>
        </div>
      ));
      console.log(postDivs);
    }


    return (
      <div className="post-container">
        {postDivs}
      </div>
    );
  }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-07-31 21:25:39

this.props.postsundefinedundefined没有长度-只有字符串和数组有(或一些具有自定义getters的对象)

您需要PostList.defaultProps = { posts: [] },或者在propTypes中将其设置为isRequired,或者将tour test编写为<PostList posts={[]} />,或者将render语句设置为if (posts !== undefined && posts.length !== 0) {const { posts = [] } = this.props -随您的选择。

票数 1
EN

Stack Overflow用户

发布于 2017-07-31 21:13:30

您正在渲染PostList,但没有定义posts (在道具上)。

代码语言:javascript
复制
const wrapper = shallow(<PostList />)

因此,this.props.posts是未定义的,并且您正在尝试从未定义的变量中读取属性(长度

代码语言:javascript
复制
 1) <PostList /> should have a container for holding posts:
 TypeError: Cannot read property 'length' of undefined

这将导致异常,并且您的测试将失败

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45416716

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档