public void SunnyDayLoggingCorrectly() { ILog log = (ILog)mocks.CreateMock(typeof(ILog)); IMethodInvocation methodInvocation = (IMethodInvocation)mocks.CreateMock(typeof(IMethodInvocation)); MethodInfo mi = typeof(string).GetMethod("ToString", Type.EmptyTypes); //two additional calls the method are to retrieve the method name on entry/exit... Expect.Call(methodInvocation.Method).Return(mi).Repeat.Any(); Expect.Call(log.IsTraceEnabled).Return(true).Repeat.Any(); log.Trace("Entering ToString"); Expect.Call(methodInvocation.Proceed()).Return(null); log.Trace("Exiting ToString"); mocks.ReplayAll(); TestableSimpleLoggingAdvice loggingAdvice = new TestableSimpleLoggingAdvice(true); loggingAdvice.CallInvokeUnderLog(methodInvocation, log); mocks.VerifyAll(); }
public void ExceptionPathStillLogsCorrectly() { ILog log = (ILog)mocks.CreateMock(typeof(ILog)); IMethodInvocation methodInvocation = (IMethodInvocation)mocks.CreateMock(typeof(IMethodInvocation)); MethodInfo mi = typeof(string).GetMethod("ToString", Type.EmptyTypes); //two additional calls the method are to retrieve the method name on entry/exit... Expect.Call(methodInvocation.Method).Return(mi).Repeat.Any(); Expect.Call(log.IsTraceEnabled).Return(true).Repeat.Any(); log.Trace("Entering..."); LastCall.On(log).IgnoreArguments(); Exception e = new ArgumentException("bad value"); Expect.Call(methodInvocation.Proceed()).Throw(e); log.Trace("Exception...", e); LastCall.On(log).IgnoreArguments(); mocks.ReplayAll(); TestableSimpleLoggingAdvice loggingAdvice = new TestableSimpleLoggingAdvice(true); try { loggingAdvice.CallInvokeUnderLog(methodInvocation, log); Assert.Fail("Must have propagated the IllegalArgumentException."); } catch (ArgumentException) { } mocks.VerifyAll(); }
public void SunnyDayLoggingAllOptionalInformationCorrectly() { ILog log = (ILog)mocks.CreateMock(typeof(ILog)); IMethodInvocation methodInvocation = (IMethodInvocation)mocks.CreateMock(typeof(IMethodInvocation)); MethodInfo mi = typeof(Dog).GetMethod("Bark"); //two additional calls the method are to retrieve the method name on entry/exit... Expect.Call(methodInvocation.Method).Return(mi).Repeat.Any(); int[] luckyNumbers = new int[] { 1, 2, 3 }; object[] args = new object[] { "hello", luckyNumbers }; Expect.Call(methodInvocation.Arguments).Return(args); Expect.Call(log.IsTraceEnabled).Return(true).Repeat.Any(); log.Trace("Entering..."); LastCall.IgnoreArguments(); Expect.Call(methodInvocation.Proceed()).Return(4); log.Trace("Exiting..."); LastCall.IgnoreArguments(); mocks.ReplayAll(); TestableSimpleLoggingAdvice loggingAdvice = new TestableSimpleLoggingAdvice(true); loggingAdvice.LogExecutionTime = true; loggingAdvice.LogMethodArguments = true; loggingAdvice.LogUniqueIdentifier = true; loggingAdvice.CallInvokeUnderLog(methodInvocation, log); mocks.VerifyAll(); }