Testing generics

Apologies if this is posted in the wrong place - please let me know where would be more appropriate.

I am still trying to come to terms with the way the unit testing in VS2005 Beta 2 works - it's a slghtly tortuous process, but that probably says more about me than anything!

I am stuck with generics.

I have a class as follows:




public class Example {
   public Example() {
      mList =
new System.Collections.Generic.List<Example>();
   }
   public System.Collections.Generic.List<Example> mList;
   public System.Collections.Generic.List<Example> ListExposedAsProperty {
      get { return mList; }
   }
}


 

I create a test for it, and add a little code to it, as:



public void ATest() {
   object target = M5LocalTests.ExampleAccessor.CreatePrivate();
   ExampleAccessor accessor = new ExampleAccessor(target);
   System.Collections.Generic.
List<ExampleAccessor> localListReference = accessor.ListExposedAsProperty;
   // this will throw an exception of 
   // Test method M5LocalTests.ExampleTest.ATest threw exception: 
   // System.InvalidCastException: Unable to cast object of 
   // type 'System.Collections.Generic.List`1' to 
   // type 'System.Collections.Generic.List`1'..
   Assert.AreEqual(0, localListReference.Count);
}

 


Two strange things happen.  Firstly, there is no accessor generated for the mList member variable, and I can't make it generate one - right clicking and choosing the create private accessor option doesn't have any effect here (although this does work for non-generic variables).

Secondly, as commented in the code above, the test throws an exception, claiming not to be able to cast between two on the face of it identical types.  Thsi is presumably bound up with the difference between a List<ExampleAccessor> and a List<Example> which is the real type, but beyond that I'm stuck.

Any thoughts Workarounds   I like generics and I like unit testing - I'd to have to sacrifice one or the other!

Thanks
Stuart



Answer this question

Testing generics

  • Gurudta

    Do you really need all the Accessor classes I find that testing the public methods/properties of a class is enough most of the time.



    public class ATest() {
        Example target = new Example();
        List<Example> list = target.ListExposedAsProperty;
        Assert.AreEqual(0, list.Count);
    }

     


  • chickplee

    I have set it up this way because that's what Visual Studio did for me.  I just right click the class I want to test and VS offers to create tests for me.  It sets up all the accessors and so on - I just used them :-)
    (I also quite like the idea of being able to unit test the private methods, as otherwise I end up exposing methods as public just to be able to test them, which I don't like)

    The classes under test are part of an ASP.NET project.  Presumably to use them directly, I'd have to add a reference to the assembly they are compiled into the test project and then just import the namespace.  On the other hand - I'm clearly being stupid, but I'm not sure where the dynamically compiled website goes when it's compiled, so i can't find the assembly to refer to.  I know that's a feeble excuse, but I have assumed that there must be some reason for VS having set up testing with the accessors.  Am I barking up completely the wrong tree

    Thanks


  • John Daldry

    Hi Stuart,

    Thank you for reporting the issues with generics and unit test generation. I apologize for the delay in responding, I just became aware of this post today. It sometimes takes longer than we would like for an issue to find its way to the right person.

    In general our support for generics is pretty limited. We will only support generating for public generics with public type parameters and even in those instances the generated code may be commented out because it will not be completely correct in all cases. We will do better in version 2 of code generation.

    In current builds we generate an Assert.Fail for ListExposedAsProperty:

    // Unit Test Generation Error: A private accessor could not be created for Example.ListExposedAsProperty: Generic parameters with non-public type parameters are not supported: Example

    Although the Example class is indeed public all ASP unit test code goes through the accessor model. I believe this happens because you can’t take a direct assembly reference to an ASP project.

    mList not being wrapped in the accessor class is another known limitation with generics. When we release, we will include in the product documentation a detailed discussion of what our generic support look like. Essentially generics testing needs to be manually provided, but in simpler cases unit test generation will help get you started.

    Thank you,

    Michael Brisset

    Visual Studio Team System


  • Testing generics