Loggable基本クラス ( ILoggableを実装する)をインターセプトするインターセプターの単体テストを作成したいと考えています。
Loggable基本クラスには呼び出すメソッドがなく、ロギング機能によって初期化されるためにのみ使用されます。
私の理解では、次のことを行う必要があります。
- ILoggableとILoggerのモック
- ロギング機能の初期化
- インターセプターを登録する
- モックされた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
}