Add tests for methods producing errors, and logger

This commit is contained in:
Shane Kilkelly 2017-03-16 09:55:07 +00:00
parent c3b18618bf
commit 40b238271d
2 changed files with 74 additions and 1 deletions

View file

@ -47,3 +47,33 @@ describe 'timeAsyncMethod', ->
expect(@Timer.done.callCount).to.equal 1
done()
describe 'when base method produces an error', ->
beforeEach ->
@testObject.nextNumber = (n, callback=(err, result)->) ->
setTimeout(
() ->
callback(new Error('woops'))
, 100
)
it 'should propagate the error transparently', (done) ->
@timeAsyncMethod @testObject, 'nextNumber', 'test.nextNumber'
@testObject.nextNumber 2, (err, result) =>
expect(err).to.exist
expect(err).to.be.instanceof Error
expect(result).to.not.exist
done()
describe 'when a logger is supplied', ->
beforeEach ->
@logger = {log: sinon.stub()}
it 'should also call logger.log', (done) ->
@timeAsyncMethod @testObject, 'nextNumber', 'test.nextNumber', @logger
@testObject.nextNumber 2, (err, result) =>
expect(err).to.not.exist
expect(result).to.equal 3
expect(@TimerConstructor.callCount).to.equal 1
expect(@Timer.done.callCount).to.equal 1
expect(@logger.log.callCount).to.equal 1
done()

View file

@ -53,7 +53,7 @@
this.timeAsyncMethod(this.testObject, 'nextNumber', 'test.nextNumber');
return done();
});
return it('should work', function(done) {
it('should transparently wrap method invocation in timer', function(done) {
this.timeAsyncMethod(this.testObject, 'nextNumber', 'test.nextNumber');
return this.testObject.nextNumber(2, (function(_this) {
return function(err, result) {
@ -65,6 +65,49 @@
};
})(this));
});
describe('when base method produces an error', function() {
beforeEach(function() {
return this.testObject.nextNumber = function(n, callback) {
if (callback == null) {
callback = function(err, result) {};
}
return setTimeout(function() {
return callback(new Error('woops'));
}, 100);
};
});
return it('should propagate the error transparently', function(done) {
this.timeAsyncMethod(this.testObject, 'nextNumber', 'test.nextNumber');
return this.testObject.nextNumber(2, (function(_this) {
return function(err, result) {
expect(err).to.exist;
expect(err).to.be["instanceof"](Error);
expect(result).to.not.exist;
return done();
};
})(this));
});
});
return describe('when a logger is supplied', function() {
beforeEach(function() {
return this.logger = {
log: sinon.stub()
};
});
return it('should also call logger.log', function(done) {
this.timeAsyncMethod(this.testObject, 'nextNumber', 'test.nextNumber', this.logger);
return this.testObject.nextNumber(2, (function(_this) {
return function(err, result) {
expect(err).to.not.exist;
expect(result).to.equal(3);
expect(_this.TimerConstructor.callCount).to.equal(1);
expect(_this.Timer.done.callCount).to.equal(1);
expect(_this.logger.log.callCount).to.equal(1);
return done();
};
})(this));
});
});
});
}).call(this);