title: Stubbing push notifications with sinon
body: <p>I would like to test a function that sends a push notification. I would like to stub the notification send. I use mocha and sinon. I tried to stub with several syntax, but it's not working. One stub I tried:</p>
  
  <pre><code>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;
  }
  </code></pre>
  
  <p>The code to test:</p>
  
  <pre><code>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 &gt; 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;
  }
  </code></pre>
  
  <p><strong>EDIT</strong>: The test</p>
  
  <pre><code>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();
          });
      });
  </code></pre>