4

针对生产代码程序集运行 NUnit 时出现错误(请参阅帖子底部)。生产代码程序集引用了内部使用 log4net 的第三方框架(具体来说,这是 SimplyAccounting 的 SDK)。它使用的 log4net 版本是 1.2.9.0。除了我们通过 dll 文件中的公共 api 使用的第三方框架之外,log4net 不是我们生产代码的一部分。

在进行故障排除时,我从单元测试项目中消除了对生产代码程序集的引用,并添加了对 SimplyAccounting SDK 的引用。当我这样做时,NUnit 会吐出以下错误。如果第三方参考(或生产代码参考)不在测试项目中,那么 Nunit 运行良好。任何具有潜在 log4net 冲突的解决方法都会有所帮助。我正在使用 NUnit 2.5.2。

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at log4net.Layout.LayoutSkeleton.get_Header()
   at log4net.Appender.TextWriterAppender.WriteHeader()
   at log4net.Appender.TextWriterAppender.set_Writer(TextWriter value)
   --- End of inner exception stack trace ---

Server stack trace: 
   at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
   at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index)
   at NUnit.Core.Log4NetCapture.SetAppenderTextWriter(TextWriter writer)
   at NUnit.Core.Log4NetCapture.StartCapture()
   at NUnit.Core.TextCapture.set_Enabled(Boolean value)
   at NUnit.Core.RemoteTestRunner.StartTextCapture(EventListener queue)
   at NUnit.Core.RemoteTestRunner.BeginRun(EventListener listener, ITestFilter filter)
   at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInContext, Object[]& outArgs)
   at System.Runtime.Remoting.Messaging.StackBuilderSink.PrivateProcessMessage(RuntimeMethodHandle md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInContext, Object[]& outArgs)
   at System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg, Int32 methodPtr, Boolean fExecuteInContext)

Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at NUnit.Core.TestRunner.BeginRun(EventListener listener, ITestFilter filter)
   at NUnit.Core.ProxyTestRunner.BeginRun(EventListener listener, ITestFilter filter)
   at NUnit.Util.TestDomain.BeginRun(EventListener listener, ITestFilter filter)
   at NUnit.Util.TestLoader.RunTests(ITestFilter filter)
   at NUnit.UiKit.TestSuiteTreeView.RunTests(ITest[] tests, Boolean ignoreCategories)
   at NUnit.UiKit.TestSuiteTreeView.RunSelectedTests()
   at NUnit.UiKit.TestTree.RunSelectedTests()
   at NUnit.Gui.NUnitForm.runButton_Click(Object sender, EventArgs e)
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
4

1 回答 1

2

一种解决方案(尽管非常具有破坏性并且不能解决根本问题)是让您的生产程序集与包装了 SimplyAccount SDK 的接口对话,而不是直接与它对话。然后,您的生产代码不需要引用 SDK dll,您可以通过传入您创建的接口的假实现来自由测试它。

这具有与该 SDK 分离的设计优势,但就像我说的那样,它可能会对您的代码库造成很大的破坏。

于 2011-04-15T21:31:47.033 回答