如何解决使用C#泛型和方法链接安排测试种子数据
我是一名具有一定编码经验的测试人员。我必须编写一堆自动化测试,以测试现有的旧业务层。尽管可以由任何模块的业务逻辑(而不仅仅是它们自己的模块)引用这些业务类,但大约有1000个业务逻辑类,大致分为大约30个逻辑模块。
系统的数据库(sql Server)包含大约800个表,并使用sql脚本进行定义和更新。对于测试,我使用的是完整数据库的备份,还原,截断版本,我将其作为测试的空LocalDB加载。然后,我为测试中涉及的每个业务类提供所需的测试数据种子LocalDB-为此,我创建了一个新的业务类对象并调用了业务类的DoSave()
函数。
可以理解,某些业务类将在测试中非常频繁地使用(例如客户,合同,交付,产品,价格等)。因此,我想创建一个TestData类,在其中定义这些实体的良好示例列表,并根据需要在测试中重用它们。
我还希望能够利用方法链接使自己和其他测试人员可以轻松地从每个类的记录列表中查找和加载我们要定位的特定实体。最后,我正在寻找一种使用泛型执行此操作的方法,以便可以使用泛型代码实现数据播种。
因此,我尝试了两种类似的方法来实现此目的,但找不到解决某些问题的方法。我不确定是否有可能,但我认为应该有。
简化的测试用例将如下所示。对DataBuilder<Client>()
的调用是将代码定向为返回定义的记录。但是我希望通过泛型类似地使用DataBuilder<Contract>()
或DataBuilder<Delivery>()
等访问其他班级的记录。
using System;
using Old.System.ClientModule;
using Xunit;
namespace Old.System.Client.Tests
{
public class ClientTests
{
[Fact]
public void ClientDoSave_ShouldCreateAClient_WhenValidClientDetailsSet()
{
// Arrange
SetupLocalDBWithConnectionString(@"Server=(LocalDB)\MSsqlLocalDB;Integrated Security=true;AttachDbFileName=.\Path\To\LOCAL_DB.MDF");
var client1 = new DataBuilder<Client>().First();
var client2 = new Client();
client2.ClientId = client.ClientId;
// Act
client1.DoSave();
client2.DoRead();
// Assert
Assert.Equal(client1.ClientLongName,client2.ClientLongName);
}
}
}
这是我第一次尝试使用DataBuilder和TestData-在这里我尝试使用DataSet = TestData.GetRecords<T>();
定义TestData中要调用的GetRecords。但是我不能通过给每个人一个唯一的GetRecords()
来使<T>
超载。
using System.Collections.Generic;
namespace Old.System.Tests
{
public class DataBuilder<T>
{
private List<T> DataSet = new List<T>();
public DataBuilder()
{
DataSet = TestData.GetRecords<T>();
}
// i kNow these chained methods aren't great - they're just here for display
public List<T> All()
{
return DataSet;
}
public T First()
{
return DataSet[0];
}
// many other (proper) chained methods
}
}
using System.Collections.Generic;
using Old.System.ClientModule;
namespace Old.System.Tests
{
static class TestData
{
public static List<Client> GetRecords<Old.System.ClientModule.Client>()
{
return new List<Client>
{
new Client { ClientId = "12345678",ClientShortName = "JohnJ",ClientLongName = "John Johnson" },new Client { ClientId = "23456789",ClientShortName = "PeterP",ClientLongName = "Peter Peterson" }
};
}
public static List<Contract> GetRecords<Old.System.ClientModule.Contract>()
{
return new List<Contract>
{
new Contract { ContractId = "123" },new Contract { ContractId = "234" }
};
}
}
}
这是我对DataBuilder和TestData的第二次尝试-在这里,我尝试使用一些值传递给重载的TestData.GetRecords(T Entity)
方法,但是我无法确定在此处发送的内容。我知道我无法发送T
(类型),所以我尝试了TestData.GetRecords(new T())
,但这不起作用。
using System.Collections.Generic;
namespace Old.System.Tests
{
public class DataBuilder<T>
{
private List<T> DataSet = new List<T>();
public DataBuilder()
{
// DataSet = TestData.GetRecords(T);
// DataSet = TestData.GetRecords(new T());
DataSet = TestData.GetRecords(???);
}
// i kNow these chained methods aren't great - they're just here for display
public List<T> All()
{
return DataSet;
}
public T First()
{
return DataSet[0];
}
// many other (proper) chained methods
}
}
using System.Collections.Generic;
using Old.System.ClientModule;
namespace Old.System.Tests
{
static class TestData
{
public static List<Client> GetRecords(Client Entity)
{
return new List<Client>
{
new Client { ClientId = "12345678",ClientLongName = "Peter Peterson" }
};
}
public static List<Contract> GetRecords(Contract Entity)
{
return new List<Contract>
{
new Contract { ContractId = "123" },new Contract { ContractId = "234" }
};
}
}
}
请忽略测试的“行为”和“断言”的内容以及链接方法的不合逻辑的方面-我只是在这里包括了此处的内容,以表明我的意图。我的问题基于如何在这种情况下使用泛型。如何为传递到DataBuilder的Type调用特定于类型的TestData.GetRecords()
?
我们非常感谢您的帮助,但我真的很想尝试使用这种模式找到解决方案,而不是使用Key=BusinessClass
和Value=List<BusinessClass> of Records
这样的解决方案字典。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。