2016-11-22 2 views
0

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

Antwort

0

Dies ist eine Möglichkeit, die Sie testen können. Bitte beachten Sie, dass Sie die diese Anweisung benötigen, wenn trackPushNotification

Modul Aufruf:

class notificationSender { 
    send(users, notification){   

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

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

} 


export default notificationSender; 

Import auf Test

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

Test

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

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

    });