在针对生产代码程序集运行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)
答案 0 :(得分:2)
一个解决方案(虽然非常具有破坏性且无法解决根本问题)是让您的生产程序集与包装SimplyAccount SDK的界面对话,而不是直接与它交谈。然后你的生产代码不需要引用SDK dll,你可以通过传入你创建的接口的虚假实现来自由地测试它。
这具有与该SDK分离的设计优势,但就像我说的那样,它可能对您的代码库造成很大的破坏。