【问题标题】:Jasmine test for React gives error and doesn't run testsReact 的 Jasmine 测试给出错误并且不运行测试
【发布时间】:2015-07-15 19:41:55
【问题描述】:

我刚刚开始使用 React 进行编码并且我已经制作了我的第一页,现在我想为它添加一个测试。我正在使用 Jasmine 和 karma 进行测试。当我尝试运行测试时,出现错误:

TypeError: this.props.data is undefined

我已经在网上搜索过,但找不到任何东西,所以非常感谢任何帮助。您可以在我的 github repo github.com/mrbgit/short-stories/tree/react-tests 上查看我的完整项目,谢谢!

我的测试文件如下所示:

var React = require('react/addons');
var Story = require('../../app/js/components/story.jsx');
var TestUtils = React.addons.TestUtils;
var testUtilsAdditions = require('react-testutils-additions');
describe('Story component', function () {
    var component;
    afterEach(function () {
        // if (component && testUtilsAdditions.isCompositeComponent(component) && component.isMounted()) {
        //   React.unmountComponentAtNode(component.getDOMNode().parent);
        // }
    });
    beforeEach(function () {
        component = TestUtils.renderIntoDocument(React.createElement(Story));
        component.props.data.storyTitle = 'front end test title';
        component.props.data.author = 'front end author';
        component.props.data.storyText = 'front end story text';
        console.log('LLLLLLLLL', component);
    });
    it('should display a story', function () {
        expect(component.props.data).toBeDefined();
        expect(component.props.data.storyTitle).toBeDefined();
        expect(component.props.data.storyTitle).toBe('front end test title');
        expect(component.props.data.author).toBe('front end author');
        expect(component.props.data.storyText).toBe('front end story text')
    });
});

【问题讨论】:

    标签: javascript testing jasmine reactjs karma-jasmine


    【解决方案1】:

    试试这个

    beforeEach(function () {
        var element = React.createElement(Story);
        element.props.data = {
            storyTitle: 'front end test title',
            author : 'front end author',
            storyText : 'front end story text'
        };
        component = TestUtils.renderIntoDocument(element);
        console.log('LLLLLLLLL', component);
    });
    

    【讨论】:

    • 感谢您这么快回复我。我尝试了您的建议,但现在出现错误:TypeError: element.props.data is undefined
    • @CascadiaJS 也许data 应该明确定义,如在更新的答案中
    • 感谢 Kirill Slatin 修复它!
    【解决方案2】:

    在使用 react 测试工具将其渲染到 DOM 时,您需要将 data 对象传递到您的组件中。这就是我通常测试我的组件的方式:

    var React = require('react');
    var ReactAddons = require('react/addons').addons;
    var TestUtils = ReactAddons.TestUtils;
    var Story = require('../../app/js/components/story');
    var expect = require('chai').expect;
    
    describe('<Story />', function () {
      before(function () {
    
        // Mock data.
        this.storyData = {
          author: 'Test Author',
          storyTitle: 'Test title.',
          storyText: 'This is a test story.'
        };
    
        // Render component with mock data.
        this.component = TestUtils.renderIntoDocument(
          <Story data={ this.storyData } />
        );
      });
    
      it('receives story data', function () {
        var componentData = this.component.props.data;
    
        expect(componentData).to.exist;
    
        expect(componentData.storyTitle).exist;
        expect(componentData.storyTitle).to.equal(this.storyData.storyTitle);
    
        expect(componentData.author).to.exist;
        expect(componentData.author).to.equal(this.storyData.author);
    
        expect(componentData.storyText).to.exist;
        expect(componentData.storyText)to.equal(this.storyData.storyText);
      });
    });
    

    在我的示例中,我使用chaijs 进行断言。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-14
      • 1970-01-01
      • 1970-01-01
      • 2016-06-14
      • 1970-01-01
      • 1970-01-01
      • 2016-06-13
      • 1970-01-01
      相关资源
      最近更新 更多