javascript - React 的 Jasmine 测试给出错误并且不运行测试

标签 javascript testing jasmine reactjs karma-jasmine

我刚刚开始使用 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')
    });
});

最佳答案

试试这个

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

关于javascript - React 的 Jasmine 测试给出错误并且不运行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31439619/

相关文章:

unit-testing - vstest.executionengine.x86.exe 未关闭

jasmine - Jasmine headless (headless)Webkit中更好的故障报告

angular - submitEl.triggerEventHandler 不是 Angular 5 中的函数

javascript - 如何在不启动任何 Jasmine 测试的情况下运行 Karma 服务器

使用 AppendChild 的 Javascript For 循环

c# - 如何在我的错误日志中使用步骤的名称? NUnit、Specflow、C#

javascript - 有没有办法触发 JavaScript 中另一个元素后面的任何元素的点击?

php - 使用来自同一行的 id 单击带有 XPath 的按钮

javascript - fs.readFileSync() 在 express js 应用程序中失败

计算按钮点击次数的Javascript代码