简体   繁体   中英

Reuse code in a unit test using several Service References (different types, same method names)

I have VS 2012 , .NET 4.5, and Unit test project with 5 Service References to Wcf Services.

This Wcf Services implements contracts with the same name for contract.

I have 5 methods for unit testing. The code is the same unless new instruction for create the object (5 types different)

    var svc = new SvcReferenceServiceOdpNet.ServiceOdpNetClient();
    var svc = new SvcReferenceServiceOdpNetx86.ServiceOdpNetClient();
    var svc = new SvcReferenceServiceOdpNet_IISHosted.ServiceOdpNetClient();
    var svc = new SvcReferenceServiceOdpNetx64_IISHosted.ServiceOdpNetClient();
    var svc = new SvcReferenceServiceOdpNet_IISHosted_Net40.ServiceOdpNetClient();

this code is common

    var res = svc.GetTestOdpNetQuery(DataUtils.Select_Sysdate);

Method GetTestOdpNetQuery with the same name, svc.GetTestOdpNetQuery, considering that svc variable corresponds to one of 5 types differents.

Any way for sharing code and reuse, and avoid code duplication?

[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");
}

Reflection is one option. But a better solution would be delegates.

    [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");
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM