Explorar o código

If function is called without callback, apply with original args

Shane Kilkelly %!s(int64=9) %!d(string=hai) anos
pai
achega
1f77cc0fd3

+ 9 - 2
libraries/metrics/test/unit/coffee/timeAsyncMethodTests.coffee

@@ -90,8 +90,8 @@ describe 'timeAsyncMethod', ->
 
 	describe 'when the wrapped function is not using a callback', ->
 		beforeEach ->
-			@testObject.nextNumber = (n) ->
-				return n+1
+			@realMethod =  sinon.stub().returns(42)
+			@testObject.nextNumber = @realMethod
 
 		it 'should not throw an error', ->
 			@timeAsyncMethod @testObject, 'nextNumber', 'test.nextNumber'
@@ -99,4 +99,11 @@ describe 'timeAsyncMethod', ->
 				@testObject.nextNumber 2
 			expect(badCall).to.not.throw(Error)
 
+		it 'should call the underlying method', ->
+			@timeAsyncMethod @testObject, 'nextNumber', 'test.nextNumber'
+			result = @testObject.nextNumber(12)
+			expect(@realMethod.callCount).to.equal 1
+			expect(@realMethod.calledWith(12)).to.equal true
+			expect(result).to.equal 42
+
 

+ 3 - 1
libraries/metrics/timeAsyncMethod.coffee

@@ -13,7 +13,9 @@ module.exports = (obj, methodName, prefix, logger) ->
 		[firstArgs..., callback] = originalArgs
 
 		if !callback? || typeof callback != 'function'
-			logger.log "[Metrics] expected wrapped method '#{methodName}' to be invoked with a callback"
+			if logger?
+				logger.log "[Metrics] expected wrapped method '#{methodName}' to be invoked with a callback"
+			return realMethod.apply this, originalArgs
 
 		timer = new metrics.Timer(key)