요즘 팀 시스템 테스팅 문제가 발생했습니다. 당신이 MyMethod라는를 테스트하려면비공개 액세서 클래스가 제네릭 제약을 무시합니다
namespace MyLibrary
{
public class MyClass
{
public Nullable<T> MyMethod<T>(string s) where T : struct
{
return (T)Enum.Parse(typeof(T), s, true);
}
}
}
, 당신이 테스트 프로젝트를 만들 수 있습니다
당신이 다음 클래스가 있다고 가정 : 적어도 다음과 같은 경우에 - 나는 자동으로 생성 접근 클래스는 일반적인 제약을 무시 발견 다음 테스트 방법을 사용하십시오.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<TestEnum>("item2");
// The following line works great but does not work for testing private methods.
TestEnum? e2 = c.MyMethod<TestEnum>("item2");
}
위의 스니 j의 설명에 언급 된 오류로 테스트를 실행하는 데 실패합니다. 문제는 Visual Studio에서 만든 접근 자 클래스입니다. 당신이 그것으로 갈 경우, 다음과 같은 코드까지 올 것이다 : 당신이 볼 수 있듯이
namespace MyLibrary
{
[Shadowing("MyLibrary.MyClass")]
public class MyClass_Accessor : BaseShadow
{
protected static PrivateType m_privateType;
[Shadowing("[email protected]")]
public MyClass_Accessor();
public MyClass_Accessor(PrivateObject __p1);
public static PrivateType ShadowedType { get; }
public static MyClass_Accessor AttachShadow(object __p1);
[Shadowing("[email protected]")]
public T? MyMethod(string s);
}
}
가의 MyMethod라는 메서드의 제네릭 형식 매개 변수에 대한 제약이 없다.
버그입니까? 그게 설계된거야? 그 문제를 해결하는 방법을 누가 압니까?
"해결되지 않음으로 마감되었습니다"? 환상적. –