The then () call handler will not be called during registration - only during the next event loop, which is outside the current test stack.
You will need to perform a check inside the completion handler and notify mocha that your asynchronous code has completed. See Also http://visionmedia.imtqy.com/mocha/#asynchronous-code
It should look something like this:
it('should execute promise\ success callback', function(done) { mySpies.executeQuery = sinon.stub(databaseConnection, 'execute').returns(q.resolve('[{"id":2}]')); databaseConnection.execute('SELECT 2 as id FROM Users ORDER BY RAND() LIMIT 1').then(function(result){ chai.expect(result).to.be.equal('[{"id":2}]'); databaseConnection.execute.restore(); done(); }, function(err) { done(err); }); });
Changes in the source code:
- made test function parameter
- validation and cleaning in the then () handler
Edit: Also, to be honest, this test does not test anything regarding your code, it only checks the functionality of the promise, since only a bit of your code (databaseConnection) breaks.
source share