C# 使用多个服务引用(不同类型、相同方法名)在单元测试中重用代码

C# 使用多个服务引用(不同类型、相同方法名)在单元测试中重用代码,c#,wcf,unit-testing,code-reuse,C#,Wcf,Unit Testing,Code Reuse,我有VS2012、.NET4.5和单元测试项目,其中有5个对Wcf服务的服务引用 此Wcf服务使用合同的相同名称实现合同 我有5种单元测试方法。代码相同,除非创建对象的新指令(5种不同类型) 此代码很常见 var res = svc.GetTestOdpNetQuery(DataUtils.Select_Sysdate); 方法GetTestOdpNetQuery,其名称为svc.GetTestOdpNetQuery,考虑到svc变量对应于5种不同类型中的一种 有没有办法共享代码和重用

我有VS2012、.NET4.5和单元测试项目,其中有5个对Wcf服务的服务引用

此Wcf服务使用合同的相同名称实现合同

我有5种单元测试方法。代码相同,除非创建对象的新指令(5种不同类型)

此代码很常见

    var res = svc.GetTestOdpNetQuery(DataUtils.Select_Sysdate);
方法GetTestOdpNetQuery,其名称为svc.GetTestOdpNetQuery,考虑到svc变量对应于5种不同类型中的一种

有没有办法共享代码和重用代码,避免代码重复

[TestMethod]
public void Get_Data_de_OdpNet_con_service_AnyCPU()
{
    var svc = new SvcReferenceServiceOdpNet.ServiceOdpNetClient();
    var res = svc.GetTestOdpNetQuery(DataUtils.Select_Sysdate);
    Assert.IsNotNull(res, "Null Value");

    TestContext.WriteLine("Result: ");
    TestContext.WriteLine(res);
    Assert.IsFalse(res.StartsWith("ERROR"), "Error found ERROR");
}

[TestMethod]
public void Get_Data_de_OdpNet_con_service_x86()
{
    var svc = new SvcReferenceServiceOdpNetx86.ServiceOdpNetClient();
    var res = svc.GetTestOdpNetQuery(DataUtils.Select_Sysdate);
    Assert.IsNotNull(res, "Null Value");

    TestContext.WriteLine("Result: ");
    TestContext.WriteLine(res);
    Assert.IsFalse(res.StartsWith("ERROR"), "Error found ERROR");
}

[TestMethod]
public void Get_Data_de_OdpNet_con_service_AnyCPU_hosted_en_IIS()
{
    var svc = new SvcReferenceServiceOdpNet_IISHosted.ServiceOdpNetClient();
    var res = svc.GetTestOdpNetQuery(DataUtils.Select_Sysdate);
    Assert.IsNotNull(res, "Null Value");

    TestContext.WriteLine("Result: ");
    TestContext.WriteLine(res);
    Assert.IsFalse(res.StartsWith("ERROR"), "Error found ERROR");
}


[TestMethod]
public void Get_Data_de_OdpNet_con_service_x64_hosted_en_IIS()
{
    var svc = new SvcReferenceServiceOdpNetx64_IISHosted.ServiceOdpNetClient();
    var res = svc.GetTestOdpNetQuery(DataUtils.Select_Sysdate);
    Assert.IsNotNull(res, "Null Value");

    TestContext.WriteLine("Result: ");
    TestContext.WriteLine(res);
    Assert.IsFalse(res.StartsWith("ERROR"), "Error found ERROR");
}


[TestMethod]
public void Get_Data_de_OdpNet_con_service_AnyCPU_hosted_en_IIS_Net40()
{
    var svc = new SvcReferenceServiceOdpNet_IISHosted_Net40.ServiceOdpNetClient();
    var res = svc.GetTestOdpNetQuery(DataUtils.Select_Sysdate);
    Assert.IsNotNull(res, "Null Value");

    TestContext.WriteLine("Result: ");
    TestContext.WriteLine(res);
    Assert.IsFalse(res.StartsWith("ERROR"), "Error found ERROR");
}

反射是一种选择。但更好的解决办法是委派代表

    [TestMethod]
    public void Get_Data_de_OdpNet_con_service_AnyCPU()
    {
        var svc = new SvcReferenceServiceOdpNet.ServiceOdpNetClient();
        DoTest(svc.GetTestOdpNetQuery, DataUtils.Select_Sysdate);
    }

    [TestMethod]
    public void Get_Data_de_OdpNet_con_service_x86()
    {
     var svc = new SvcReferenceServiceOdpNetx86.ServiceOdpNetClient();
     DoTest(svc.GetTestOdpNetQuery, DataUtils.Select_Sysdate);
    }

    // repeat this test method pattern for all 5 service references and call
    // the DoTest method.

    private void DoTest(Func<DateTime, string> func, DateTime sysDate)
    {
        var res = func(sysDate);
        Assert.IsNotNull(res, "Null Value");

        TestContext.WriteLine("Result: ");
        TestContext.WriteLine(res);
        Assert.IsFalse(res.StartsWith("ERROR"), "Error found ERROR");
    }
[TestMethod]
public void Get_Data_de_OdpNet_con_service_AnyCPU()
{
var svc=new svcverenceservicedpnet.ServiceOdpNetClient();
DoTest(svc.GetTestOdpNetQuery,DataUtils.Select\u Sysdate);
}
[测试方法]
public void Get_Data_de_OdpNet_con_service_x86()
{
var svc=new svcverenceservicedpnetx86.ServiceOdpNetClient();
DoTest(svc.GetTestOdpNetQuery,DataUtils.Select\u Sysdate);
}
//对所有5个服务引用和调用重复此测试方法模式
//最愚蠢的方法。
私有void DoTest(Func Func,DateTime sysDate)
{
var res=func(系统日期);
Assert.IsNotNull(res,“Null值”);
WriteLine(“结果:”);
TestContext.WriteLine(res);
Assert.IsFalse(res.StartsWith(“ERROR”),“ERROR-found-ERROR”);
}
    [TestMethod]
    public void Get_Data_de_OdpNet_con_service_AnyCPU()
    {
        var svc = new SvcReferenceServiceOdpNet.ServiceOdpNetClient();
        DoTest(svc.GetTestOdpNetQuery, DataUtils.Select_Sysdate);
    }

    [TestMethod]
    public void Get_Data_de_OdpNet_con_service_x86()
    {
     var svc = new SvcReferenceServiceOdpNetx86.ServiceOdpNetClient();
     DoTest(svc.GetTestOdpNetQuery, DataUtils.Select_Sysdate);
    }

    // repeat this test method pattern for all 5 service references and call
    // the DoTest method.

    private void DoTest(Func<DateTime, string> func, DateTime sysDate)
    {
        var res = func(sysDate);
        Assert.IsNotNull(res, "Null Value");

        TestContext.WriteLine("Result: ");
        TestContext.WriteLine(res);
        Assert.IsFalse(res.StartsWith("ERROR"), "Error found ERROR");
    }