我在单元测试中使用Moles将调用重定向到日志应用程序块(EntLib的包装版本),并且它适用于某些方法但不是全部。
这是代理人正在设置的测试初始化方法......
[TestInitialize()]
public void TestInit()
{
Common.Logging.Moles.MExceptionEvent.LogExceptionStringStringStringString = delegate(Exception ex, string a, string b, string c, string d)
{
Debug.WriteLine(String.Format("Exception occurred in test context '{0}' : {1} ", TestContext.TestName, ex.ToString()));
};
Common.Logging.Moles.MCriticalEvent.LogStringStringTraceEventTypeStringString = delegate(string a, string b, TraceEventType tet, string c, string d)
{
Debug.WriteLine(String.Format("Critical Event occurred in test context '{0}' : {1} ", TestContext.TestName, a));
};
Common.Logging.Moles.MDebugEvent.LogStringStringTraceEventTypeStringString = delegate(string a, string b, TraceEventType tet, string c, string d)
{
Debug.WriteLine(String.Format("Debug Event occurred in test context '{0}' : {1} ", TestContext.TestName, a));
};
}
这是重定向的方法签名(来自对象资源管理器)。
Public Shared Sub Log(exc As System.Exception, Optional sessionId As String = "", Optional msg As String = "", Optional encoreNamespace As String = "", Optional methodName As String = "")
Member of Common.Logging.ExceptionEvent
Public Shared Sub Log(msg As String, Optional sessionId As String = "", Optional severity As System.Diagnostics.TraceEventType = Information, Optional encoreNamespace As String = "", Optional methodName As String = "")
Member of Common.Logging.CriticalEvent
Public Shared Sub Log(msg As String, Optional sessionId As String = "", Optional severity As System.Diagnostics.TraceEventType = Information, Optional encoreNamespace As String = "", Optional methodName As String = "")
Member of Common.Logging.DebugEvent
ExceptionEvent和CriticalEvent能够正确登录到重定向的输出位置,但DebugEvent不能。 DebugEvent调用会抛出配置异常,因为它尝试从配置文件加载日志记录配置。
是否有一些我想念的简单的东西,或者这应该是我写的吗?
答案 0 :(得分:0)
原来这不是上述代码的直接问题。
我有一个[ClassInitialize]正在调用日志记录调用,并且总是在[TestInitialize]之前触发。
将[TestInitialize]的内容移动到[ClassInitialize]的开头修复了问题。