【发布时间】:2016-04-19 20:50:25
【问题描述】:
尝试使用 Karma+Jasmine 测试 React 组件,
我正在尝试检查是否调用了 onClick 处理程序中的所有函数,但测试返回错误结果:
`Expected spy reportLoginWithEmail to have been called.`
这是我的组件:
<a className="sign-in-with-email" onClick={this.signInWithEmail}>
Or Sign in with your Email
</a>
signInWithEmail 处理程序:
signInWithEmail = (event) => {
event.preventDefault();
this.setState({
isEmailSignIn: true
});
biActions.reportLoginWithEmail();
};
测试:
describe('SignIn', () => {
let component, biActions;
beforeEach(() => {
component = TestUtils.renderIntoDocument(<SignIn/>);
biActions = require('../../../actions/BIActions');
spyOn(biActions, 'reportLoginWithEmail');
});
it('test clicking on login by email call function', () => {
let signInEmail = TestUtils.findRenderedDOMComponentWithClass(component, 'sign-in-with-email');
TestUtils.Simulate.click(signInEmail);
expect(biActions.reportLoginWithEmail).toHaveBeenCalled();
});
});
另一边测试state改回true:
it('test clicking on login by email change state', () => {
let signInEmail = TestUtils.findRenderedDOMComponentWithClass(component, 'sign-in-with-email');
TestUtils.Simulate.click(signInEmail);
expect(component.state.isEmailSignIn).toBe(true);
});
我错过了什么,有什么建议吗?
【问题讨论】:
标签: javascript testing reactjs jasmine karma-jasmine