2016-05-01 16 views
0

Ich möchte eine Funktion testen, die eine Push-Benachrichtigung sendet. Ich möchte die Benachrichtigung absenden. Ich benutze Mokka und Sinon. Ich habe versucht, mit mehreren Syntax stumpf, aber es funktioniert nicht. Ein Stub ich versucht:Stubbing Push-Benachrichtigungen mit Sinon

var sender = sinon.createStubInstance(gcm.Sender); 
var gcmReturn = { 
    multicast_id: 1, 
    success: 1, 
    failure: 1, 
    canonical_ids: 1, 
    results: [{ 
     registration_id: 'xxxx', 
     message_id: 'yyy' 
    }, { 
     error: 'InvalidRegistration' 
    }] 
} 
sender.send = function() { 
    return gcmReturn; 
} 

Der Code zum Test:

var gcm = require('node-gcm'); 

// Function to test 
NotificationService.prototype.sendSpecificNotification = function (data) { 
    var self = this; 
    // Process data to create the users, title and text parameters 
    return self.send(users, title, text) 
     .then(function(pushResults) { 
      // expect ... 
     }) 
}; 

NotificationService.prototype.send = function (users, title, text) { 
    var registrationIds = []; 
    users.forEach(function (user) { 
     var push = user.preferences != null ? user.preferences.push : false; 
     if (push) { 
      var regId = user.preferences.pushRegId; 
      registrationIds.push(regId); 
     } 
    }); 
    var deferred = Q.defer(); 
    if (registrationIds.length > 0) { 
     var message = new gcm.Message(); 
     message.addData('message', text); 
     message.addData('title', title); 
     message.delayWhileIdle = false; 
     message.timeToLive = 24 * 60 * 60; 
     var currentDate = new Date(); 
     var notId = currentDate.getHours() * 60 * 60 + currentDate.getMinutes() * 60 + currentDate.getSeconds(); 
     message.addData('notId', notId); 
     var sender = new gcm.Sender(config.gcm.senderId); 
     // I want to stub that call 
     sender.send(message, registrationIds, function (err, result) { 
      if (err) { 
       deferred.reject(new Error(error)); 
      } else { 
       deferred.resolve(result); 
      } 
     }); 
    } else { 
     deferred.resolve(''); 
    } 
    return deferred.promise; 
} 

EDIT: Der Test

it('Subscription Push', function() { 
    var sender = sinon.createStubInstance(gcm.Sender); 
    var gcmReturn = { 
     multicast_id: 1, 
     success: 1, 
     failure: 1, 
     canonical_ids: 1, 
     results: [{ 
      registration_id: 'xxxx', 
      message_id: 'yyy' 
     }, { 
      error: 'InvalidRegistration' 
     }] 
    } 
    sender.send.returns(Q.resolve(gcmReturn)); 
    return fixtureService.clearDatabase() 
     .then(function() { 
      return fixtureService.load('./test/subscription-push.json'); 
     }) 
     .then(function() { 
      return notificationService.sendSpecificNotification(); 
     }); 
    }); 

Antwort

0

Haben Sie versucht, die Sinon Stub-Methode verwenden? Beispiel:

it('Subscription Push', function() { 
    var sender = sinon.createStubInstance(gcm.Sender); 
    var gcmReturn = { 
     multicast_id: 1, 
     success: 1, 
     failure: 1, 
     canonical_ids: 1, 
     results: [{ 
      registration_id: 'xxxx', 
      message_id: 'yyy' 
     }, { 
      error: 'InvalidRegistration' 
     }] 
    } 
    sender.send.returns(Q.resolve(gcmReturn)); 
    return fixtureService.clearDatabase() 
     .then(function() { 
      return fixtureService.load('./test/subscription-push.json'); 
     }) 
     .then(function() { 
      return notificationService.sendSpecificNotification(); 
     }); 
}); 

UPDATE

Sie sind nicht die Stub-Methoden überhaupt verwenden, gibt es keine Magie hinter den Kulissen geht hier vor, die das Verfahren automatisch auf dem notificationService Stummel. Sie müssen den von Ihnen erstellten Absender verwenden oder die Methode direkt auf dem notificationService ausführen.

it('Subscription Push', function() { 
    var sender = sinon.createStubInstance(gcm.Sender); 
    var gcmReturn = { 
     multicast_id: 1, 
     success: 1, 
     failure: 1, 
     canonical_ids: 1, 
     results: [{ 
      registration_id: 'xxxx', 
      message_id: 'yyy' 
     }, { 
      error: 'InvalidRegistration' 
     }] 
    } 
    sender.send.returns(Q.resolve(gcmReturn)); 
    return fixtureService.clearDatabase() 
     .then(function() { 
      return fixtureService.load('./test/subscription-push.json'); 
     }) 
     .then(function() { 
      // Here you should be using sender.sendSpecificNotification(); 
      // Alternatively, you could simply stub out the method 
      // on the notificationService directoy and avoid using the sender at all. 
      // To stub out the method on the notificationService do this: 
      // sinon.stub(notificationService, 'send', function() { return Q.resolve(gcmReturn); }); 
      return notificationService.sendSpecificNotification(); 
     }); 
}); 
+0

ich 'Typeerror: Versuchte senden zu wickeln die bereits wrapped' – Sydney

+0

Versuchen Sie, die letzte Zeile zu sender.send.returns (gcmReturn) zu ändern; – grimurd

+0

Ich habe den Fehler nicht mehr, aber der Stub wird nicht in der 'sendSpecificNotification' Methode verwendet. Grundsätzlich die Testzeit. – Sydney