[英]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. 我有VS 2012,.NET 4.5和单元测试项目,其中有5个服务引用到Wcf服务。
This Wcf Services implements contracts with the same name for contract. 此Wcf服务实施合同的相同名称的合同。
I have 5 methods for unit testing. 我有5种单元测试方法。 The code is the same unless new instruction for create the object (5 types different)
除非用于创建对象的新指令(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();
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. 方法GetTestOdpNetQuery具有相同的名称,svc.GetTestOdpNetQuery,考虑到svc变量对应于5种类型的不同之一。
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");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.