the*_*row 4 unit-testing castle-dynamicproxy mocking
我想为截取Loggable基类(实现ILoggable)的拦截器编写一些单元测试.
在可记录的基类有没有方法来调用,它仅用于通过登录基础设施进行初始化.
根据我的理解,我应该:
问题是我的ILoggable接口没有可以调用的方法,因此不会截获任何内容.
在这里采取行动的正确方法是什么?
我应该手动模拟ILoggable并添加一个存根方法来调用吗?
另外,我也应该嘲笑容器吗?
我正在使用Moq和NUnit.
编辑:
这是我的拦截器实现参考:
public class LoggingWithDebugInterceptor : IInterceptor
{
#region IInterceptor Members
public void Intercept(IInvocation invocation)
{
var invocationLogMessage = new InvocationLogMessage(invocation);
ILoggable loggable = invocation.InvocationTarget as ILoggable;
if (loggable == null)
throw new InterceptionFailureException(invocation, string.Format("Class {0} does not implement ILoggable.", invocationLogMessage.InvocationSource));
loggable.Logger.DebugFormat("Method {0} called with arguments {1}", invocationLogMessage.InvokedMethod, invocationLogMessage.Arguments);
Stopwatch stopwatch = new Stopwatch();
try
{
stopwatch.Start();
invocation.Proceed();
stopwatch.Stop();
}
catch (Exception e)
{
loggable.Logger.ErrorFormat(e, "An exception occured in {0} while calling method {1} with arguments {2}", invocationLogMessage.InvocationSource, invocationLogMessage.InvokedMethod, invocationLogMessage.Arguments);
throw;
}
finally
{
loggable.Logger.DebugFormat("Method {0} returned with value {1} and took exactly {2} to run.", invocationLogMessage.InvokedMethod, invocation.ReturnValue, stopwatch.Elapsed);
}
}
#endregion IInterceptor Members
}
Run Code Online (Sandbox Code Playgroud)