Skip to content

Instantly share code, notes, and snippets.

@joebalancio
Last active December 13, 2015 17:28
Show Gist options
  • Save joebalancio/4947713 to your computer and use it in GitHub Desktop.
Save joebalancio/4947713 to your computer and use it in GitHub Desktop.
I'm having trouble testing 2 modules that have methods which return promises. When I chain the calls together and test if the methods have been called, only 1 out of the 2 were called. The second nested method is never called according to Q. However, both resolved values are logged and inspecting the method within the callback reveals that it ha…
Q = require('q');
sinon = require('sinon')
sinonChai = require('sinon-chai');
chai.use(sinonChai);
sinon.should()
describe('testing promises', function() {
var aDeferred, bDeferred, moduleA, moduleB;
aDeferred = Q.defer();
bDeferred = Q.defer();
moduleA = {
asyncMethodA: function() {
return aDeferred.promise;
}
};
moduleB = {
asyncMethodB: function() {
return bDeferred.promise;
}
};
it('should pass', function() {
sinon.spy(moduleA, 'asyncMethodA');
sinon.spy(moduleB, 'asyncMethodB');
aDeferred.resolve('fooResolve');
bDeferred.resolve('barResolve');
moduleA.asyncMethodA('foo').then(function(value) {
var promise;
console.log(value);
promise = moduleB.asyncMethodB('bar');
moduleB.asyncMethodB.should.have.been.called;
return promise;
}).then(function(value) {
return console.log(value);
}).fail(function(err) {
return console.log(err);
});
moduleA.asyncMethodA.should.have.been.called;
moduleB.asyncMethodB.should.have.been.called;
// FAIL: expected asyncMethodB to have been called at least once, but it was never called
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment