2013-01-05 75 views
0

我有一个系统,其中所有类都扩展基类Sol.Data.Object。在该基类我有用于从数据库中检索数据的方法之一:使用泛型类型参数的方法的单元测试

public static ObjectType ReadById<ObjectType>(string schema, long id) 
{ 
    SqlCommand command = User.CreateCommand(string.Format("{1}.Retrieve{0}", 
              typeof(ObjectType).Name, 
              schema), 
              new SqlParameter("@ID", id)); 
     ..... 
} 

我将调用此方法例如是这样的: Sol.Movie.ReadId("dbo", 2)

我用Visual Studio 2010中创建用于该方法中一个单元测试:

public void ReadByIdTestHelper<ObjectType>() 
{ 
    string schema = "dbo"; 
    long id = 1; 
    ObjectType expected = default(ObjectType); //What should I put in here?! 
    ObjectType actual; 
    actual = Sol.Data.Object.ReadById<ObjectType>(schema, id); 
    Assert.AreEqual(expected, actual); 
    Assert.Inconclusive("Verify the correctness of this test method."); 
} 

[TestMethod()] 
public void ReadByIdTest() 
{ 
    ReadByIdTestHelper<Sol.Movie>(); 
} 

我该如何定义预期类型?我试过typeof(ObjectType),但它给我编译错误。

感谢您的帮助!

回答

0

我用这个方法来解决这个问题:

public void ReadByIdTestHelper<ObjectType>() 
{ 
    string schema = "dbo"; 
    long id = 1; 
    Sol.Movie movie = new Movie(); 
    ObjectType actual; 
    actual = Sol.Data.Object.ReadById<ObjectType>(schema, id); 
    Assert.AreEqual((actual is ObjectType), true); 
} 

[TestMethod()] 
public void ReadByIdTest() 
{ 
    ReadByIdTestHelper<Sol.Movie>(); 
} 

不知道这是否是要做到这一点,虽然最好的办法。

+2

你能接受你的答案吗? –

+0

@AlexanderStepaniuk我不能接受我自己的答案! – Afflatus

+1

你可以。请看[为什么我不能接受我自己的答案?](http://blog.stackoverflow.com/2008/11/why-cant-i-accept-my-own-answer/)。我相信你必须等待48小时,然后你才能接受。 –

相关问题