为什么我的酶测试失败了?

时间:2017-07-31 13:02:52

标签: reactjs mocha chai enzyme

我有一个简单的测试我试图通过Enzyme / mocha / chai。这是错误:

  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:

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语句测试posts对象的长度,如果它通过,我的post divs将开始生成。所以我知道这些帖子一开始就没有定义。 React中的错误做法是在渲染方法中使用逻辑吗?或者这是酶方面的问题?:

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>
    );
  }
}

2 个答案:

答案 0 :(得分:3)

您正在渲染PostList而不定义帖子(在道具上)。

const wrapper = shallow(<PostList />)

所以,this.props.posts是未定义的,你试图从未定义的变量中读取属性(长度)

 1) <PostList /> should have a container for holding posts:
 TypeError: Cannot read property 'length' of undefined

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

答案 1 :(得分:1)

undefinedundefinedPostList.defaultProps = { posts: [] }没有长度 - 只有字符串和数组(或带有自定义getter的一些对象)

您需要propTypes或者您需要<PostList posts={[]} />或者将游览测试写为if (posts !== undefined && posts.length !== 0) {或将渲染语句设为const { posts = [] } = this.propsCalendar time = Calendar.getInstance(); time.setTimeInMillis(System.currentTimeMillis()); time.add(Calendar.SECOND, 30); and then using time.getTimeInMillis() - 挑