【发布时间】: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