ca9163d9
ca9163d9

Reputation: 29209

Visual studio 2010 Premium MSTest bug?

The following is the test code.

namespace ConsoleApplication2
{
    class MyClass {}
    class Program
    {
        static void Main(string[] args) { }

        List<MyClass> Test() { return new List<MyClass>(); }
    }
}

Then using Visual studio create a union test for method Test().

    [TestMethod()]
    [DeploymentItem("ConsoleApplication2.exe")]
    public void TestTest()
    {
        Program_Accessor target = new Program_Accessor(); // TODO: Initialize to an appropriate value
        List<MyClass_Accessor> actual;
        actual = target.Test();
        Assert.IsTrue(actual != null);
    }

It will get the following exception when running the test. How to workaround the issue?

Test method TestProject1.ProgramTest.TestTest threw exception:  
System.InvalidCastException: Unable to cast object of type 'System.Collections.Generic.List`1[ConsoleApplication2.MyClass]' to type 'System.Collections.Generic.List`1[ConsoleApplication2.MyClass_Accessor]'.

I tried the following method and it doesn't work too.

IEnumerable<MyClass> Test1() { return new List<MyClass>(); }

Upvotes: 1

Views: 209

Answers (1)

Dmitry Harnitski
Dmitry Harnitski

Reputation: 6018

Your program and your method are not public so MSTest and any other code cannot call it. MS Test built wrapper Program_Accessor to use your code through reflection.

Change your code to use public access modifier and regenerate the test. It is the easiest way to learn unit-testing.

public class Program
{
   public class MyClass{}

   static void Main(string[] args) { }

   public List<MyClass> Test() { return new List<MyClass>(); }
}

You may use InternalsVisibleToAttribute if you do not want to make MyClass public.

Friend Assemblies in MSDN

Upvotes: 2

Related Questions