2017-08-05 17 views
1

を使用してfirebaseへの呼び出しをテストしようとしています。 Firebaseは、Googleが提供するオンラインデータベースサービスです。私は唯一の私は、現時点でテストする必要がある機能をからかっています モックされたfirebase関数が呼び出されて失敗する前にjestテストが終了する

'use strict'; 

const firebase = jest.genMockFromModule('firebase'); 

const ref = jest.fn(() => { 
    return { 
    child: jest.fn(() => { 
     return ref 
    }), 
    update: jest.fn(() => { 
     console.log('Called update') 
     return Promise.resolve() 
    }) 
    } 
}) 

firebase.initializeApp = jest.fn() 
firebase.database = jest.fn(() => { 
    return { 
    ref: ref 
    } 
}) 

module.exports = firebase 

下回るよう firebaseモジュールをからかっています。以下は私のテストケースです。

it('+++ actionCreator addAlarm',() => { 
    const store = mockStore(initialState) 
    store.dispatch(ActionCreators.addAlarm(alarm)); 
    // Make sure that the scheduleNotifications API is called 
    expect(scheduleNotifications).toHaveBeenCalled(); 
    expect(firebase.initializeApp).toHaveBeenCalled(); 
    expect(firebase.database().ref().update).toHaveBeenCalled(); 
}); 

テストケースの最後の行は、私は嘲笑されfirebaseupdate関数を呼び出していていることを確認しようとしています。

は、以下の試験ケースは、私はupdate関数が呼び出されたことを確認行に失敗しているコンソール出力

abcs-MBP-2:GalarmApp abc$ npm test 

> [email protected] test /Users/abc/Projects/GalarmApp 
> jest 

FAIL __tests__/actionsSpecs.js 
    ● >>>A C T I O N --- Test galarm actions: › +++ actionCreator addAlarm 

    expect(jest.fn()).toHaveBeenCalled() 

    Expected mock function to have been called. 

     at Object.<anonymous> (__tests__/actionsSpecs.js:58:52) 
     at tryCallTwo (node_modules/promise/lib/core.js:45:5) 
     at doResolve (node_modules/promise/lib/core.js:200:13) 
     at new Promise (node_modules/promise/lib/core.js:66:3) 
     at Promise.resolve.then.el (node_modules/p-map/index.js:42:16) 
     at tryCallOne (node_modules/promise/lib/core.js:37:12) 
     at node_modules/promise/lib/core.js:123:15 

    >>>A C T I O N --- Test galarm actions: 
    ✕ +++ actionCreator addAlarm (8ms) 
    ✓ +++ actionCreator setConnectionStatus (4ms) 

Test Suites: 1 failed, 1 total 
Tests:  1 failed, 1 passed, 2 total 
Snapshots: 1 passed, 1 total 
Time:  1.989s, estimated 2s 
Ran all test suites. 
    console.warn node_modules/rn-host-detect/index.js:45 
    [SECURITY] node-uuid: crypto not usable, falling back to insecure Math.random() 

    console.log __mocks__/firebase.js:11 
    Called update 

あります。コンソールの出力を見ると、Called updateコンソールが存在することがわかります。これは、更新関数が呼び出されたが、テストケースが失敗した後に呼び出されることを意味します。

これはfirebaseupdate関数の呼び出しは、私の知る限り理解して非同期的に起きていないサンクアクション

const addAlarm = (alarm) => (dispatch, getState) => { 
    if(alarm.status) { 
    NotificationManager.scheduleNotifications(alarm); 
    } 

    const alarmObjForFirebase = this.createAlarmObjForFirebase(alarm) 
    firebaseRef.update(alarmObjForFirebase) 
} 

あるaddAlarmアクションです。

この問題を解決する方法についての情報があれば教えてください。

答えて

1

私のコードでこの問題を発見し、他の人の利益のためのソリューションとして投稿しました。問題は、updateモックが定義された方法です。それが定義された方法として、firebase.database().ref().update. Since this is a new instance of the mock function, it wouldn't contain any data about update関数が呼び出されるたびに、私はupdateモック関数の新しいインスタンスを取得していました。

const update = jest.fn(() => { 
    return Promise.resolve() 
}) 

const ref = jest.fn(() => { 
    return { 
    update: update 
    } 
}) 

この方法は、次のように

私は、コードを変更するために必要な、私はupdateモック機能の新しいインスタンスを作成していないと私はそれをテストケース中に呼び出されたことを主張することができました。

関連する問題