Ich möchte behaupten, dass die trackPushNotification
innerhalb meiner notifier.send
Funktion aufgerufen wird. Die Funktionen trackPushNotification und send befinden sich in derselben Datei.
Ich nahm an, ich sollte die trackPushNotification
mit Sinon stubeln, um in der Lage zu sein, die callCount
Eigenschaft zu verfolgen. Wenn ich meinen Test ausführe, scheint die trackPushNotification
überhaupt nicht gestempelt zu werden. Ich habe einige Dinge durchsucht und anscheinend hat es etwas damit zu tun, wie ich ES6-Import/-Export verwende. Ich konnte keine Antwort finden, also hoffe ich, dass mir jemand bei diesem Problem helfen kann.Stumm Funktionsaufruf in importierten Datei mit Sinon
Die notifier.send
Funktion sieht wie folgt aus:
export const send = (users, notification) => {
// some other logic
users.forEach(user => trackPushNotification(notification, user));
};
Meine notifier.trackPushNotification
Funktion wie folgt aussieht:
export const trackPushNotification = (notification, user) => {
return Analytics.track({ name: 'Push Notification Sent', data: notification.data }, user);
};
Mein Testfall wie folgt aussieht:
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);
});
hat auch eine schnelle Test:
// 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);
});