Common Language Runtime detected an invalid program error in unit testing
Asked Answered
E

1

1

I have the following code

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

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?

Euhemerize answered 15/1, 2013 at 7:52 Comment(2)
Which unit testing framework?Serapis
can you add a stack trace? I may be experiencing a similar issue, but only since January 10th.Inexpedient
V
2

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) 

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.

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.

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. :-)

Vortical answered 18/1, 2013 at 18:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.