Unit testing React.js和Jasmine间谍
使用基本测试utils和jasmine进行单元测试 如何监视react组件中的函数 test.js:Unit testing React.js和Jasmine间谍,unit-testing,reactjs,jasmine,reactjs-testutils,Unit Testing,Reactjs,Jasmine,Reactjs Testutils,使用基本测试utils和jasmine进行单元测试 如何监视react组件中的函数 test.js: class Test extends React.Component { handleClick() { // Do something } render() { return ( <div className="test-class" onClick={this.handleClick}>Test</div> );
class Test extends React.Component {
handleClick() {
// Do something
}
render() {
return (
<div className="test-class" onClick={this.handleClick}>Test</div>
);
}
}
const React = require('react-with-addons');
const RequireJsTest = require('requirejs-test');
const Utils = React.addons.TestUtils;
const Test = require('./test');
describe('test', () => {
it('should test', function() {
const test = Utils.renderIntoDocument(<Test/>);
const el = Utils.findRenderedDOMComponentWithClass(test, 'test-class');
spyOn(test, 'handleClick');
Utils.Simulate.click(el);
expect(test.handleClick).toHaveBeenCalled();
});
});
有什么想法吗?谢谢 老实说,我还没有测试过react应用程序,但请尝试一下(描述块中的最后一个测试),我刚刚在自述文档中找到了它 我认为您应该在呈现组件之前监视组件类原型方法:
spyOn(Test.prototype, 'handleClick');
// and then
expect(Test.prototype.handleClick).toHaveBeenCalled();
spyOn(Test.prototype, 'handleClick');
// and then
expect(Test.prototype.handleClick).toHaveBeenCalled();