Javascript ReactTestUtils.Simulate.click上的Jasmine spyOn测试失败

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> 测试:

尝试测试反应组件,使用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>
测试:


我遗漏了什么,有什么建议吗?

好的,经过几个小时的研究,我发现了问题:

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
'ed
reportlogin 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/>);
  });