title: Unit testing private methods in C#
body: <p>Visual Studio allows unit testing of private methods via an automatically generated accessor class. I have written a test of a private method that compiles successfully, but it fails at runtime. A fairly minimal version of the code and the test is:</p>
  
  <pre><code>//in project MyProj
  class TypeA
  {
      private List&lt;TypeB&gt; myList = new List&lt;TypeB&gt;();
  
      private class TypeB
      {
          public TypeB()
          {
          }
      }
  
      public TypeA()
      {
      }
  
      private void MyFunc()
      {
          //processing of myList that changes state of instance
      }
  }    
  
  //in project TestMyProj           
  public void MyFuncTest()
  {
      TypeA_Accessor target = new TypeA_Accessor();
      //following line is the one that throws exception
      target.myList.Add(new TypeA_Accessor.TypeB());
      target.MyFunc();
  
      //check changed state of target
  }
  </code></pre>
  
  <p>The runtime error is:</p>
  
  <pre><code>Object of type System.Collections.Generic.List`1[MyProj.TypeA.TypeA_Accessor+TypeB]' cannot be converted to type 'System.Collections.Generic.List`1[MyProj.TypeA.TypeA+TypeB]'.
  </code></pre>
  
  <p>According to intellisense - and hence I guess the compiler - target is of type TypeA_Accessor. But at runtime it is of type TypeA, and hence the list add fails.</p>
  
  <p>Is there any way I can stop this error? Or, perhaps more likely, what other advice do other people have (I predict maybe "don't test private methods" and "don't have unit tests manipulate the state of objects").</p>