C# 具有泛型类型参数的方法的单元测试
我有一个系统,在这个系统中,我的所有类都扩展了基类C# 具有泛型类型参数的方法的单元测试,c#,unit-testing,generic-type-argument,C#,Unit Testing,Generic Type Argument,我有一个系统,在这个系统中,我的所有类都扩展了基类Sol.Data.Object。在这个基类中,我有一个从数据库检索数据的方法: public static ObjectType ReadById<ObjectType>(string schema, long id) { SqlCommand command = User.CreateCommand(string.Format("{1}.Retrieve{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));
.....
}
我应该如何定义期望的类型?我尝试了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>();
}
public void ReadByIdTestHelper()
{
字符串schema=“dbo”;
长id=1;
Sol.Movie电影=新电影();
ObjectType-actual;
actual=Sol.Data.Object.ReadById(schema,id);
AreEqual((实际为ObjectType),true);
}
[TestMethod()]
public void ReadByIdTest()
{
ReadByIdTestHelper();
}
不知道这是否是最好的方法。你可以。请看。我相信你必须等48小时,然后你才能接受。@AlexanderStepaniuk当然!我会等48小时。
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>();
}