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);
});