Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 具有泛型类型参数的方法的单元测试_C#_Unit Testing_Generic Type Argument - Fatal编程技术网

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