如何对拦截器进行单元测试?

the*_*row 4 unit-testing castle-dynamicproxy mocking

我想为截取Loggable基类(实现ILoggable)的拦截器编写一些单元测试.
可记录的基类有没有方法来调用,它仅用于通过登录基础设施进行初始化.
根据我的理解,我应该:

  1. 模拟一个ILoggable和一个ILogger
  2. 初始化日志记录工具
  3. 在它上面注册我的拦截器
  4. 调用模拟的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)

Krz*_*mic 6

如果它只是Logger在你的类上使用Property 的拦截器而不是为什么在那里?你也可以在拦截器上拥有它.(就像Ayende在他的帖子中解释的那样).

除此之外 - 拦截器只是一个与界面交互的类 - 一切都是高度可测试的.