<p>These days, i came across a problem with Team System Unit Testing. I found that the automatically created accessor class ignores generic constraints - at least in the following case:</p>

<p>Assume you have the following class:</p>

<pre><code>namespace MyLibrary
{
 public class MyClass
 {
  public Nullable&lt;T&gt; MyMethod&lt;T&gt;(string s) where T : struct
  {
   return (T)Enum.Parse(typeof(T), s, true);
  }
 }
}
</code></pre>

<p>If you want to test MyMethod, you can create a test project with the following test method:</p>

<pre><code>public enum TestEnum { Item1, Item2, Item3 }

[TestMethod()]
public void MyMethodTest()
{
 MyClass c = new MyClass();
 PrivateObject po = new PrivateObject(c);
 MyClass_Accessor target = new MyClass_Accessor(po);

 // The following line produces the following error:
 // Unit Test Adapter threw exception: GenericArguments[0], 'T', on
 // 'System.Nullable`1[T]' violates the constraint of type parameter 'T'..
 TestEnum? e1 = target.MyMethod&lt;TestEnum&gt;("item2");

 // The following line works great but does not work for testing private methods.
 TestEnum? e2 = c.MyMethod&lt;TestEnum&gt;("item2");
}
</code></pre>

<p>Running the test will fail with the error mentioned in the comment of the snippet above. The problem is the accessor class created by Visual Studio. If you go into it, you will come up to the following code:</p>

<pre><code>namespace MyLibrary
{
 [Shadowing("MyLibrary.MyClass")]
 public class MyClass_Accessor : BaseShadow
 {
  protected static PrivateType m_privateType;

  [Shadowing(".ctor@0")]
  public MyClass_Accessor();
  public MyClass_Accessor(PrivateObject __p1);
  public static PrivateType ShadowedType { get; }
  public static MyClass_Accessor AttachShadow(object __p1);

  [Shadowing("MyMethod@1")]
  public T? MyMethod(string s);
 }
}
</code></pre>

<p>As you can see, there is no constraint for the generic type parameter of the MyMethod method.</p>

<p>Is that a bug? Is that by design? Who knows how to work around that problem?</p>

[{'answer_id': 64877, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': "<p>I vote bug.  I don't see how this could be by design.</p>\n"}, {'answer_id': 168453, 'author': 'Zachary Yates', 'author_id': 8360, 'author_profile': 'https://Stackoverflow.com/users/8360', 'pm_score': 1, 'selected': False, 'text': '<p>I didn\'t verify everything, but it looks like the call to:</p>\n\n<pre><code>TestEnum? e1 = target.MyMethod("item2");\n</code></pre>\n\n<p>uses type inference to determine the generic type param T.  Try calling the method differently in the test if possible:</p>\n\n<pre><code>TestEnum? e1 = target.MyMethod&lt;TestEnum&gt;("item2");\n</code></pre>\n\n<p>That may yield different results.</p>\n\n<p>Hope that helps!</p>\n'}, {'answer_id': 217704, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>Search for unit tests with generics on msdn. This is a known limitation. Vote for a resolution on Microsoft Connect, as it is definately needs resolving.</p>\n'}, {'answer_id': 1196250, 'author': 'Josh Heyse', 'author_id': 142347, 'author_profile': 'https://Stackoverflow.com/users/142347', 'pm_score': 2, 'selected': False, 'text': '<p>Here is a similar issue on connect for reference.\n<a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=324473&amp;wa=wsignin1.0" rel="nofollow noreferrer">https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=324473&amp;wa=wsignin1.0</a></p>\n'}, {'answer_id': 3570884, 'author': 'Igor Zevaka', 'author_id': 129404, 'author_profile': 'https://Stackoverflow.com/users/129404', 'pm_score': 1, 'selected': False, 'text': '<p>Looks like a bug. The workaround would be to change the method to <code>internal</code> and add <code>[assembly: InternalsVisibleTo("MyLibrary.Test")]</code> to the assembly containing class under test.</p>\n\n<p>This would be my preferred way of testing non-public methods as it produces much cleaner looking unit tests.</p>\n'}]
['https://Stackoverflow.com/questions/64813', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/6777/']