我有一个使用LINQ to SQL实现的存储库.虽然我没有数据库,但我需要进行单元测试.如何为FreezeAllAccountsForUser方法编写UT?你能用手动模拟来展示一个例子吗?
注意:域对象中使用了继承映射
注意:单元测试将使用Visual Studio Team Test完成
来自@StuperUser的评论.单元测试涉及将代码与其交互的其他对象完全隔离.这意味着如果代码失败,您可以确定失败是与测试中的代码有关.要做到这一点,你必须伪造这些对象.
码
public void FreezeAllAccountsForUser(int userId)
{
List<DTOLayer.BankAccountDTOForStatus> bankAccountDTOList = new List<DTOLayer.BankAccountDTOForStatus>();
IEnumerable<DBML_Project.BankAccount> accounts = AccountRepository.GetAllAccountsForUser(userId);
foreach (DBML_Project.BankAccount acc in accounts)
{
string typeResult = Convert.ToString(acc.GetType());
string baseValue = Convert.ToString(typeof(DBML_Project.BankAccount));
if (String.Equals(typeResult, baseValue))
{
throw new Exception("Not correct derived type");
}
acc.Freeze();
DTOLayer.BankAccountDTOForStatus presentAccount = new DTOLayer.BankAccountDTOForStatus();
presentAccount.BankAccountID = acc.BankAccountID;
presentAccount.Status = acc.Status;
bankAccountDTOList.Add(presentAccount);
}
IEnumerable<System.Xml.Linq.XElement> el = bankAccountDTOList.Select(x =>
new System.Xml.Linq.XElement("BankAccountDTOForStatus",
new System.Xml.Linq.XElement("BankAccountID", x.BankAccountID),
new System.Xml.Linq.XElement("Status", x.Status)
));
System.Xml.Linq.XElement root = …
Run Code Online (Sandbox Code Playgroud)