2016-11-22 11 views
0

trackPushNotificationが私のnotifier.send関数内で呼び出されていると主張したいと思います。 trackPushNotificationとsend関数の両方が同じファイル内にあります。
callCountプロパティを追跡できるようにするために、Sinonを使用してtrackPushNotificationをスタブする必要があると仮定しました。私のテストを実行すると、trackPushNotificationは全くスタブされていないようです。私はいくつかのものを検索しましたが、明らかに、ES6のインポート/エクスポートを使用している方法と関係があります。私は答えを見つけることができませんでしたので、誰かがこの問題で私を助けてくれることを願っています。Sinonでインポートされたファイルで関数呼び出しをスタブできません

notifier.send関数は次のようになります。

export const send = (users, notification) => { 
    // some other logic 

    users.forEach(user => trackPushNotification(notification, user)); 
}; 

notifier.trackPushNotification関数は次のようになります。

export const trackPushNotification = (notification, user) => { 
    return Analytics.track({ name: 'Push Notification Sent', data: notification.data }, user); 
}; 

私のテストケースは次のようになります。

it('should track the push notifications',() => { 
    sinon.stub(notifier, 'trackPushNotification'); 

    const notificationStub = { 
    text: 'Foo notification text', 
    data: { id: '1', type: 'foo', track_id: 'foo_track_id' }, 
    }; 

    const users = [{ 
    username: '[email protected]', 
    }, { 
    username: '[email protected]', 
    }]; 

    notifier.send(users, notificationStub); 

    assert.equal(notifier.trackPushNotification.callCount, 2); 
}); 

も迅速をしましたテスト:

// implementation.js 
export const baz = (num) => { 
    console.log(`blabla-${num}`); 
}; 

export const foo = (array) => { 
    return array.forEach(baz); 
}; 

// test.js 
it('test',() => { 
    sinon.stub(notifier, 'baz').returns(() => console.log('hoi')); 

    notifier.foo([1, 2, 3]); // outputs the blabla console logs 

    assert.equal(notifier.baz.callCount, 3); 
}); 

答えて

0

これはテストする方法の1つです。

モジュールtrackPushNotification
を呼び出すときにこの文を必要とすることに注意してください:

class notificationSender { 
    send(users, notification){   

     users.forEach(user => this.trackPushNotification(notification, user));   
    } 

    trackPushNotification(notification, user){ 
     console.log("some"); 
    } 

} 


export default notificationSender; 

インポートテストに

import notificationSender from './yourModuleName';<br/> 

テスト

it('notifications', function(){ 
     let n = new notificationSender();   
     sinon.spy(n,'trackPushNotification'); 

     n.send(['u1','u2'],'n1'); 
     expect(n.trackPushNotification.called).to.be.true; 

    }); 
関連する問題