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();
}
}
}