Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
NUnit和AppDomain';s未经处理的异常Venthandler_Nunit_Backgroundworker_Appdomain_Unhandled Exception - Fatal编程技术网

NUnit和AppDomain';s未经处理的异常Venthandler

NUnit和AppDomain';s未经处理的异常Venthandler,nunit,backgroundworker,appdomain,unhandled-exception,Nunit,Backgroundworker,Appdomain,Unhandled Exception,我在用NUnit编写单元测试时遇到了一些问题,该测试检查后台工作程序在其RunWorkerCompleted事件中是否引发了自定义异常(ProcessException)——为了便于讨论,我们假设无法更改该代码 我尝试在单元测试中连接到AppDomain的UnhandledExceptionHandler,如下所示: AppDomain currentDomain = AppDomain.CurrentDomain; UnhandledExceptionEvent

我在用NUnit编写单元测试时遇到了一些问题,该测试检查后台工作程序在其RunWorkerCompleted事件中是否引发了自定义异常(ProcessException)——为了便于讨论,我们假设无法更改该代码

我尝试在单元测试中连接到AppDomain的UnhandledExceptionHandler,如下所示:

        AppDomain currentDomain = AppDomain.CurrentDomain;
        UnhandledExceptionEventHandler handler = null;

        handler = (s, e) =>
        {
            // Need to make it clear that an exception is expected in the output window
            Console.WriteLine("==== Unhandled exception has been caught ====");
            processExceptionWasThrownByBackgroundWorker = true;
            currentDomain.UnhandledException -= handler;
            Assert.AreEqual(typeof(ProcessException), e.GetType());
        };

        currentDomain.UnhandledException += handler;

        CallMyBackgroundWorkerThatThrows();

        Assert.AreEqual(true, processExceptionWasThrownByBackgroundWorker);
当使用TestDriven.Net从visual studio中运行时,此单元测试运行良好并通过。但是,当它在生成计算机上运行时,显示出NUnit控制台也在捕获未处理的异常[1],并返回NUnit进程失败,同时报告测试全部通过。事实上,它正在尝试反序列化定制异常-ProcessException-并失败,我认为这就是本文所描述的[2]

[1] :

[2] :

生成计算机的输出:

[exec] Tests run: 147, Errors: 0, Failures: 0, Inconclusive: 0, Time: 27.6745774 seconds
[exec] Not run: 0, Invalid: 0, Ignored: 0, Skipped: 0
[exec] Unhandled exceptions:
[exec] 1) Blah.ShelledProcessBackgroundRunnerTests.A_background_process_will_be_killed_if_the_timeout_is_exceeded : System.Runtime.Serialization.SerializationException: Unable to find assembly 'Blah, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
[exec] at System.Runtime.Serialization.Formatters.Binary.BinaryAssemblyInfo.GetAssembly()
[exec] at System.Runtime.Serialization.Formatters.Binary.ObjectReader.GetType(BinaryAssemblyInfo assemblyInfo, String name)
[exec] at System.Runtime.Serialization.Formatters.Binary.ObjectMap..ctor(String objectName, String[] memberNames, BinaryTypeEnum[] binaryTypeEnumA, Object[] typeInformationA, Int32[] memberAssemIds, ObjectReader objectReader, Int32 objectId, BinaryAssemblyInfo assemblyInfo, SizedArray assemIdToAssemblyTable)
[exec] at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryObjectWithMapTyped record)
[exec] at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryHeaderEnum binaryHeaderEnum)
[exec] at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
[exec] at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
[exec] at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
[exec] at System.Runtime.Remoting.Channels.CrossAppDomainSerializer.DeserializeObject(MemoryStream stm)
[exec] at System.AppDomain.Deserialize(Byte[] blob)
[exec] at System.AppDomain.UnmarshalObject(Byte[] blob)
[exec] c:\build_work\ourcibuild(229,26): External Program Failed: C:\Program Files (x86)\NUnit 2.5.10\bin\net-2.0\nunit-console-x86.exe (return code was -100)
这是否准确反映了可能发生的情况?
如果我在单元测试中处理未处理的异常,我是否可以更改NUnit的行为以忽略该异常?

您不能改用NUnit的ExpectedExceptionAttribute或Assert.Throws吗?不,这不起作用。不过应该可以。您编写的代码将执行,然后断言,大多数运行者将在异常时停止,因为他们没有预料到它。正确的测试方法是使用以下内容:Assert.Throws(()=>CallMyBackgroundWorkerThrows());正如帕诺斯在上面所建议的那样。如果这不起作用,您需要研究它为什么不起作用,因为这是处理单元测试中异常的正确方法。