我刚刚开始使用 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/