带泛型参数的方法单元测试



我有一个系统,其中所有的类都扩展了基类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),但它给我编译错误。

谢谢你的帮助!

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

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>();
}

我不知道这是不是最好的方法

最新更新