title: How can I test through a mocked promise with Mocha?
body: <p>My node.js code is:</p>
  
  <pre><code>function getPatientNotificationNumbers(patientId) {
      patientId = patientId &amp;&amp; patientId.toString();
  
      var sql = "SELECT * FROM [notification_phone_number] ";
      sql += "WHERE patient_id = " + escapeSql(patientId);
      return sqlServer.query(sql).then(function(results) {
          var phoneNumbers = _.map(results[0], function (result) {
              var obj = {
                  id: result.id,
                  phoneNumber: result.phone_number,
                  isPrimary: result.is_primary,
                  isVerified: result.is_verified,
                  patientId: result.patient_id
              }
              return obj;
          });
  
          return phoneNumbers;
      });
  }
  </code></pre>
  
  <p>Pretty simple and straight forward. What I want to test is that the return of this function, properly resolved, is an array of <code>phoneNumbers</code> that match that format.</p>
  
  <p><code>sqlServer</code> is <code>require</code>'d above and I have a ton of things <code>require</code>'d in this file. To stub them out, I am using <code>mockery</code>, which seems to be pretty great.</p>
  
  <p>Here is my test, so far:</p>
  
  <pre><code>before(function() {
      deferred = Q.defer();
      mockery.enable();
      moduleConfig.init();
      mockery.registerSubstitute('moment', moment);
      mockery.registerAllowable('../../util');
  
      mockStubs.sqlServer = {
          query: sinon.stub().returns(deferred.promise)
      }
  
      mockery.registerMock('../../db/sqlserver', mockStubs.sqlServer);
  
      methods = require('../../../rpc/patient/methods');
  });
  
  beforeEach(function() {
      deferred = Q.defer();
  })
  
  it('should get the patient notification numbers', function(done) {
      // sinon.spy(sqlServer, 'query').and.returnValue(deferred.promise);
      deferred.resolve('here we go');
      methods.getPatientNotificationNumbers(1).then(function(result) {
          console.log(result);
          done();
      });
  
  
  });
  </code></pre>
  
  <p>However, it never gets past <code>sqlServer.query</code> in my code. So the <code>results</code> are pointless. I also tried something like:</p>
  
  <p><code>response = methods.getPatientNotificationNumbers(1)</code></p>
  
  <p>but when I <code>console.log(response)</code>, it's basically <code>{state: 'pending'}</code>, which I guess is an unresolved promise.</p>
  
  <p>So I'm all over the place and I'm open to using whatever libraries make things easy. I am not married to <code>mockery</code>, <code>sinon</code> or whatever else. Any suggestions would help.</p>
  
  <p>Thanks!</p>