2016-04-19 11 views
2

カルマ+ジャスミン、 私はonClickハンドラに、すべての機能が呼び出されていることを確認しようとしているが、テストが偽の結果を返す使用して、コンポーネントに反応をテストしようとすると:ReactTestUtils.Simulate.clickテストのジャスミンspyOnが失敗した

をここで
`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); 
    }); 

私は、任意の提案を何をしないのですか?

答えて

1

OK]をクリックして、研究の数時間後、私は問題を発見:あなたのコンポーネントの

require順序が非常に重要であり、それは私の問題でした。

import SignIn from '../components/SignIn; 

、しばらくSignInmock「ED関数が呼び出された場合のみ、そのIすでにSignInコンポーネントに初期化されたbeforeEachmockreportLoginWithEmail後、itブロックがチェック:テストSignInコンポーネントの上に

がインポートされましたmock ed関数に呼び出されたコンポーネント、

requireの変更順序で問題が解決され、012を削除しました

beforeEach(() => { 
    biActions = require('../../../actions/BIActions'); 

    spyOn(biActions, 'reportLoginWithEmail'); 

    SignIn = require('../../../components/LoginPage/SignIn'); 
    LoginByEmail = require('../../../components/LoginPage/LoginByEmail'); 

    component = TestUtils.renderIntoDocument(<SignIn/>); 
    }); 

その場合mock「ED reportLoginWithEmail機能

で初期化 SignIn成分: SignIn試験の上にコンポーネント、作業コードの
関連する問題