Visual sutdio 2008에는 단위 테스트 스텁이 자동으로 만들어집니다. 나는 몇 가지 기본적인 단위 테스트를 작성하려면이를 사용했다,하지만 난 뭔가에 의해 혼란 스러워요 : T 일 때 : 나는 " 'T'로 유형 'StorageUnitTests.FileStoreTest.bla을'변환 할 수 없습니다 오류"이유는 무엇입니까C# Generics Casting
private class bla : BaseStoreItem
{
//
}
/// <summary>
///A test for StoreData
///</summary>
public void StoreDataTestHelper<T>() where T : BaseStoreItem
{
FileStore<T> target = new FileStore<T>(); // TODO: Initialize to an appropriate value
BaseStoreItem data = new bla();
target.StoreData(data);
}
[TestMethod()]
public void StoreDataTest()
{
//Assert.Inconclusive("No appropriate type parameter is found to satisfies the type constraint(s) of T. " +
// "Please call StoreDataTestHelper<T>() with appropriate type parameters.");
StoreDataTestHelper<bla>();
}
을 "bla"라고 입력 하시겠습니까?
"bla"는 좋은 함수 이름이 아니라 알고 있습니다.
나는 새로운 T() 대신 Activator.CreateInstance가()이어야한다고 생각한다. –
@Akash 왜? Activator.CreateInstance는 리플렉션을 사용하며 느립니다. 새로운 T()가 훨씬 더 우아하다고 생각합니다. – gsharp
gsharp, 죄송합니다. 코드에서 new() 제약이 생겨서 new()가 지정되지 않았습니다. –