How to unit test generic classes

Posted by Rowland Shaw on Stack Overflow See other posts from Stack Overflow or by Rowland Shaw
Published on 2010-05-06T20:49:15Z Indexed on 2010/05/09 8:18 UTC
Read the original article Hit count: 227

I'm trying to set up some unit tests for an existing compact framework class library. However, I've fallen at the first hurdle, where it appears that the test framework is unable to load the types involved (even though they're both in the class library being tested)

Test method MyLibrary.Tests.MyGenericClassTest.MyMethodTest threw exception: System.MissingMethodException: Could not load type 'MyLibrary.MyType' from assembly 'MyLibrary, Version=1.0.3778.36113, Culture=neutral, PublicKeyToken=null'..

My code is loosely:

public class MyGenericClass<T> : List<T> where T : MyType, new()
{
    public bool MyMethod(T foo)
    {
        throw new NotImplementedException();
    }
}

With test methods:

    public void MyMethodTestHelper<T>()
        where T : MyType, new()
    {
        MyGenericClass<T> target = new MyGenericClass<T>();
        foo = new T(); 
        expected = true;
        actual = target.MyMethod(foo);
        Assert.AreEqual(expected, actual);
    }

    [TestMethod()]
    public void MyMethodTest()
    {
        MyMethodTestHelper<MyType>();
    }

I'm a bit stumped though, as I can't even get it to break in the debugger to get to the inner exception, so what else do I check?

EDIT this does seem to be something specific to the Compact Framework - recompiling the class libraries and the unit tests for the full framework, gives the expected output (i.e. the debugger stops when I'm going to throw a NotImplementedException).

© Stack Overflow or respective owner

Related posts about c#

Related posts about unit-testing