chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
In trying to implement unit tests for some of our code using TypeMock we got a System.TypeInitializationException with the following message “An attempt was made to load a program with an incorrect format” (see below for details).

After troubleshooting the problem we managed to write a small test application which reproduces the issue. The code is included below. Basically, we have two classes (Library and OtherSetup) and we want to mock Library. In the sample code Library and OtherSetup has nothing to do with each other, but an instance of OtherSetup is created as part of the test method for Library.

As can be seen in the listing below OtherSetup has an instance variable of type Queue<string> and for some reason this triggers the exception. We discovered that the problem is related to specific generic classes (e.g. Queue and Stack, but not e.g. List and Dictionary) and only when reference types are used. I.e. if Queue<string> is changed to Queue<int> everything works as expected.

Are we doing something wrong or are there some limitations with regards to combining generics and mock objects?


------------------------------------------------
Error Message
------------------------------------------------
Test method Library.LibraryTest.TestReturnTrue threw exception: System.TypeInitializationException: The type initializer for 'System.Collections.Generic.Queue`1' threw an exception. --->  System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B).


------------------------------------------------
Stack Trace
------------------------------------------------
    at System.Collections.Generic.Queue`1..cctor()
 --- End of inner exception stack trace ---
    at System.Collections.Generic.Queue`1..ctor()
   at Library.OtherSetup..ctor() in C:Documents and SettingsBRRMy DocumentsVisual Studio 2005ProjectsMockTestMockTestLibrary.cs:line 14
   at Library.LibraryTest.TestReturnTrue() in C:Documents and SettingsBRRMy DocumentsVisual Studio 2005ProjectsMockTestLibraryTestLibraryTest.cs:line 20


------------------------------------------------
Library.cs
------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;

namespace Library {
    public class Library {
        public bool ReturnTrue() {
            return true;
        }
    }

    public class OtherSetup {
        //Queue<int> q = new Queue<int>();
        Queue<string> q = new Queue<string>();
    }
}


------------------------------------------------
LibraryTest.cs
------------------------------------------------
using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TypeMock;


namespace Library {
    [TestClass]
    public class LibraryTest {
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext) {
            MockManager.Init();
        }

        [TestMethod]
        public void TestReturnTrue() {
            Mock mock = MockManager.Mock(typeof(Library));
            mock.ExpectAndReturn("ReturnTrue", true);
            OtherSetup setup = new OtherSetup();
            Library library = new Library();
            Assert.IsTrue(library.ReturnTrue());
            mock.Verify();
        }
    }
}
asked by brian.rasmussen (7.2k points)

4 Answers

0 votes
Hi,
Thanks for the detailed example.
I have tried to run the example with NUnit and it works like a charm, I will have to try it with Microsofts Unit Tests and see if I can replicate the problem.
answered by scott (32k points)
0 votes
Hi Scott,

thanks for the reply. I forgot to mention that we're using Visual Studio 2005 (Team Ed. for software developers) with the built-in support for unit tests.

I'll try to run the program with NUnit as well.

Regards,
Brian
answered by brian.rasmussen (7.2k points)
0 votes
Hi Scott,

I have modified the code to run under NUnit and unfortunately I am still able to reproduce the error.

I'm using:
NUnit 2.2.5. .NET 2.0.50727.42. Windows XP SP2 (5.1.2600). VS2005 Team Ed. for software developers.

-------------------------------------------------------
modified Library.cs
-------------------------------------------------------
using System;
using System.Text;
using System.Collections.Generic;
//using Microsoft.VisualStudio.TestTools.UnitTesting;
using NUnit.Framework;
using TypeMock;

namespace Library {
    //[TestClass]
    [TestFixture]
    public class LibraryTest {
        //[ClassInitialize()]
        [SetUp]
        public void MyClassInitialize() {
            MockManager.Init();
        }

        //[TestMethod]
        [Test]
        public void TestReturnTrue() {
            Mock mock = MockManager.Mock(typeof(Library));
            mock.ExpectAndReturn("ReturnTrue", true);
            OtherSetup setup = new OtherSetup();
            Library library = new Library();
            NUnit.Framework.Assert.IsTrue(library.ReturnTrue());
            mock.Verify();
        }
    }
}


-------------------------------------------------------
modified Library.cs
-------------------------------------------------------
Library.LibraryTest.TestReturnTrue : System.TypeInitializationException : The type initializer for 'System.Collections.Generic.Queue`1' threw an exception.
  ----> System.BadImageFormatException : An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B) at System.Collections.Generic.Queue`1..ctor()
   at Library.OtherSetup..ctor() in C:Documents and SettingsBRRMy DocumentsVisual Studio 2005ProjectsMockTestLibraryLibrary.cs:line 14
   at Library.LibraryTest.TestReturnTrue() in C:Documents and SettingsBRRMy DocumentsVisual Studio 2005ProjectsMockTestLibraryTestLibraryTest.cs:line 23
--TypeInitializationException
   at System.Collections.Generic.Queue`1..cctor()
answered by brian.rasmussen (7.2k points)
0 votes
Hi again,

The 3.0.2 code you send me fixes the problem! :D Thanks a lot for excellent support.

Regards,
Brian
answered by brian.rasmussen (7.2k points)
...