Повторное использование кода в модульном тесте с использованием нескольких ссылок на службы (разные типы, одинаковые имена методов).

У меня есть VS 2012, .NET 4.5 и модульный тестовый проект с 5 ссылками на службы для служб Wcf.

Эти службы Wcf реализуют контракты с тем же именем для контракта.

У меня есть 5 методов модульного тестирования. Код тот же, за исключением новой инструкции по созданию объекта (5 разных типов)

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

этот код является общим

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

person Kiquenet    schedule 28.03.2014    source источник


Ответы (1)


Рефлексия — один из вариантов. Но лучшим решением были бы делегаты.

    [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");
    }
person Raja Nadar    schedule 28.03.2014