公共语言运行时在单元测试中检测到无效的程序错误 [英] Common Language Runtime detected an invalid program error in unit testing

查看:524
本文介绍了公共语言运行时在单元测试中检测到无效的程序错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码

 this.SafeUpdate(rate, Guid.Parse(import.myGuid), c => c.myGuid);

SafeUpdate基本上采用已解析的guid值并将其应用于rate对象的myGuid属性。从我的前端可以正常运行,但是在单元测试中运行时会引发检测到CLR ...错误。奇怪的是,对于DateTime.Parse和int.Parse而言,相同的语句工作正常。对于Guid和小数,它只是失败。我不认为该错误与解析有关(提取到单独的变量中后,解析的值正确)。我不认为这是在嘲笑,因为该语句对guid和decimal以外的所有其他类型都适用。有什么想法吗?

SafeUpdate basically takes the parsed guid value and applies it to the myGuid property on the rate object. This works fine from my front end, but throws the "CLR detected..." error when run in a unit test. What's odd is the same statement for DateTime.Parse and int.Parse works fine. It just fails for Guid and decimals. I don't believe the error is with the parsing (it has the correct parsed value when extracted into a separate variable). I don't believe it's the mocking either as the statement works fine for all other types other than guid and decimal. Any ideas?

推荐答案

昨天我们在构建服务器上遇到了类似的错误。我们的异常是由DataContractSerializer的ReadObject()方法引发的。

We experienced a similar error yesterday on our build server. Our exception was thrown by the ReadObject() method of a DataContractSerializer.

System.InvalidProgramException: Common Language Runtime detected an invalid program.
 at System.Xml.EncodingStreamWrapper..ctor(Stream stream, Encoding encoding)
 at System.Xml.XmlUTF8TextReader.SetInput(Stream stream, Encoding encoding, XmlDictionaryReaderQuotas quotas, OnXmlDictionaryReaderClose onClose)
 at System.Xml.XmlDictionaryReader.CreateTextReader(Stream stream, Encoding encoding, XmlDictionaryReaderQuotas quotas, OnXmlDictionaryReaderClose onClose)
 at System.Xml.XmlDictionaryReader.CreateTextReader(Stream stream, XmlDictionaryReaderQuotas quotas)
 at System.Runtime.Serialization.XmlObjectSerializer.ReadObject(Stream stream) 

我们编写了一个控制台应用程序,仅执行此操作。它运行没有错误,但是在简单的单元测试中失败了。我们正在将Gallio / MbUnit 3.4.11.0用于我们的测试框架,目标是.net 4.0。

We've written a console app that does just this one thing. It runs without error, but fails in a simple unit test. We are using Gallio/MbUnit 3.4.11.0 for our test framework with a target of .net 4.0.

using System;
using System.IO;
using System.Runtime.Serialization;
using MbUnit.Framework;

namespace TestApp
{
    [TestFixture]
    class Program
    {
        static void Main()
        {
            FooBar();
            Console.ReadKey();
        }

        public static void FooBar()
        {
            var type = typeof(string);
            var foo = "foo";

            using (var stream = new MemoryStream())
            {
                var serializer = new DataContractSerializer(type);
                serializer.WriteObject(stream, foo);
                stream.Seek(0, SeekOrigin.Begin);
                var deserializer = new DataContractSerializer(type);
                var bar = deserializer.ReadObject(stream);
                Console.WriteLine(bar);
            }
        }

        [Test]
        public void Test()
        {
            FooBar();
        }
    }
}

应用程序运行良好,但测试抛出。奇怪的是,此测试通过了我的开发箱,但在我们的构建服务器以及同事的开发箱上失败了。显然,我的开发箱有一些不同之处,它可以使测试通过,但是我还没有找到区别。

The application runs fine, but the test throws. Strangely, this test passes on my dev box but fails on our build server as well as the dev box of a coworker. Clearly, there is something different about my dev box that allows the test to pass, but I have not located that difference yet.

更新1
我的开发箱上的System.dll版本为4.0.30319.296,但在构建服务器和我的同事的开发箱上为4.0.30319.1001。但是,System.Xml.dll和System.Runtime.Serialization.dll在4.0.30319.1相同。

Update 1 The version of System.dll on my dev box is 4.0.30319.296 but on the build server and the dev box of my coworker it is 4.0.30319.1001. System.Xml.dll and System.Runtime.Serialization.dll are identical at 4.0.30319.1, however.

更新2
谷歌快速搜索 4.0.30319.1001会返回此安全更新, http://support.microsoft.com/ kb / 2742595 ,它同时应用于我们的构建服务器和我的同事的dev框,但不适用于我的dev框。我在构建服务器上卸载了更新,重新启动后,问题消失了!我猜微软还没有对此进行单元测试。 :-)

Update 2 A quick google search for "4.0.30319.1001" returns this security update, http://support.microsoft.com/kb/2742595, which was applied to both our build server and the dev box of my coworker, but not my dev box. I uninstalled the update on the build server, rebooted, and the issue went away! I guess Microsoft doesn't have a unit test for this one yet. :-)

这篇关于公共语言运行时在单元测试中检测到无效的程序错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆