Javascript ReactTestUtils.Simulate.click上的Jasmine spyOn测试失败
尝试测试反应组件,使用Karma+Jasmine, 我试图检查是否调用了Javascript ReactTestUtils.Simulate.click上的Jasmine spyOn测试失败,javascript,testing,reactjs,jasmine,karma-jasmine,Javascript,Testing,Reactjs,Jasmine,Karma Jasmine,尝试测试反应组件,使用Karma+Jasmine, 我试图检查是否调用了onClick处理程序中的所有函数,但测试返回错误结果: `Expected spy reportLoginWithEmail to have been called.` 以下是我的组件: <a className="sign-in-with-email" onClick={this.signInWithEmail}> Or Sign in with your Email </a> 测试:
onClick
处理程序中的所有函数,但测试返回错误结果:
`Expected spy reportLoginWithEmail to have been called.`
以下是我的组件:
<a className="sign-in-with-email" onClick={this.signInWithEmail}>
Or Sign in with your Email
</a>
测试:
我遗漏了什么,有什么建议吗?好的,经过几个小时的研究,我发现了问题:
require
组件的顺序非常重要,这是我的问题
在测试的顶部,导入了SignIn
组件:
import SignIn from '../components/SignIn;
只有在我在SignIn
组件中已初始化的之前mock
reportLoginWithEmail
中的reportlogin之后,才会在SignIn
组件调用非mock
ed函数时检查mock
是否调用了ed函数
因此,该问题已通过require
的变更单解决,并删除测试顶部SignIn
组件的import
,工作代码:
beforeEach(() => {
biActions = require('../../../actions/BIActions');
spyOn(biActions, 'reportLoginWithEmail');
SignIn = require('../../../components/LoginPage/SignIn');
LoginByEmail = require('../../../components/LoginPage/LoginByEmail');
component = TestUtils.renderIntoDocument(<SignIn/>);
});
beforeach(()=>{
biActions=require('../../../actions/biActions');
spyOn(biActions,“reportLoginWithEmail”);
SignIn=require('../../../components/LoginPage/SignIn');
LoginByMail=require('../../../components/LoginPage/LoginByMail');
component=TestUtils.renderIntoDocument();
});
在这种情况下,使用mock
'edreportlogin with email
功能初始化SignIn
组件
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);
});
import SignIn from '../components/SignIn;
beforeEach(() => {
biActions = require('../../../actions/BIActions');
spyOn(biActions, 'reportLoginWithEmail');
SignIn = require('../../../components/LoginPage/SignIn');
LoginByEmail = require('../../../components/LoginPage/LoginByEmail');
component = TestUtils.renderIntoDocument(<SignIn/>);
});