Просмотр исходного кода

Add tests for methods producing errors, and logger

Shane Kilkelly 9 лет назад
Родитель
Сommit
40b238271d

+ 30 - 0
libraries/metrics/test/unit/coffee/timeAsyncMethodTests.coffee

@@ -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()

+ 44 - 1
libraries/metrics/test/unit/js/timeAsyncMethodTests.js

@@ -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);