[英]How to compare two lists in .NET
List<Employee> emplist = new List<Employee>();
emplist.Add(new Employee { Name = "Emp_1", BasicSalary = 1000, Id = Guid.NewGuid(), HRA = 100, DA = 10, TotalSalary = 1110 });
emplist.Add(new Employee { Name = "Emp_2", BasicSalary = 1000 * 2, Id = Guid.NewGuid(), HRA = 200, DA = 20, TotalSalary = 2220 });
emplist.Add(new Employee { Name = "Emp_3", BasicSalary = 1000 * 3, Id = Guid.NewGuid(), HRA = 300, DA = 30, TotalSalary = 3330 });
var result = empRep.CallSupportFindAll();
// CollectionAssert.AreEqual(emplist, result);
Assert.AreEqual(emplist, result);
var r1 = result[0];
Assert.AreEqual(r1.Name, emplist[0].Name);
Assert.AreEqual(r1.TotalSalary, emplist[0].TotalSalary);
Assert.AreEqual(r1.BasicSalary, emplist[0].BasicSalary);
我想比较两个列表emplist
和result
。 Assert.AreEqual(r1.Name, emplist[0].Name);
工作,但如果我们有成千上万的记录,那么我需要写成千上万的行。 所以请回答-对于比较两个列表的一个行代码...在此先感谢
如果我理解正确,您仅拥有List<Employee>
两个实例,并且您想断言它们是相等的。 也就是说,它们具有相同数量的Employee
实例,并且顺序相同,并且属性相同。
如果是这样,则与FakeItEasy无关。 您只需要执行断言的方法。 我个人将使用Fluent Assertions做到这一点。
result.Should().BeEquivalentTo(emplist);
您可以使用HashSet <>比较两个列表。
首先,定义一个这样的均衡器
public class EmployeeComparer : IEqualityComparer<Employee>
{
public bool Equals(Employee x, Employee y)
{
if (x == null && y == null)
return true;
if (x == null || y == null)
return false;
//You can implement the equal method as you like. For instance, you may compare by name
if (x.Id == y.Id)
return true;
return false;
}
public int GetHashCode(Employee employee)
{
return employee.Id.GetHashCode();
}
}
接下来,根据方法的输入和结果创建2个哈希集
var equaliser = new EmployeeComparer();
HashSet<Employee> inputHashset = new HashSet<Employee>(emplist ,equaliser );
HashSet<Employee> resultHashset = new HashSet<Employee>(result,equaliser);
最后,声明两组的相等性。 这意味着,而不是
Assert.AreEqual(emplist, result);
做
Assert.IsTrue(inputHashset.SetEquals(resultHashset));
public bool compareTwolist<T>(List<T> lst1,List<T> lst2)
{
bool bresult = false;
if (lst1.GetType() != lst2.GetType())
{
return false;
}
//if any of the list is null, return false
if ((lst1 == null && lst2 != null) || (lst2 == null && lst1 != null))
return false;
//if count don't match between 2 lists, then return false
if(lst1.Count != lst2.Count)
return false;
foreach (T item in lst1)
{
T obj1 = item;
T obj2 = lst2.ElementAt(lst1.IndexOf(item));
Type type = typeof(T);
foreach (System.Reflection.PropertyInfo property in type.GetProperties())
{
string obj1Value = string.Empty;
string obj2Value = string.Empty;
if (type.GetProperty(property.Name).GetValue(obj1) != null)
obj1Value = type.GetProperty(property.Name).GetValue(obj1).ToString();
if (type.GetProperty(property.Name).GetValue(obj2) != null)
obj2Value = type.GetProperty(property.Name).GetValue(obj2).ToString();
//if any of the property value inside an object in the list didnt match, return false
if (obj1Value.Trim() != obj2Value.Trim())
{
bresult = false;
break;
}
}
}
return bresult;
}
是的,我通过创建用户定义函数并传递两个我们要比较的列表得到了一个解决方案。
只是检查var a是否为真
var a = compareTwolist(empwithoutid,resultwithoutid); Assert.IsTrue(a);
其他答案中建议的比较方法要求您对所有对象实施均等。 从维护的角度来看,这不能很好地扩展。 同样在某些测试中,仅要比较字段的子集。 这意味着要实施的多个比较或等于方法。 另一种方法是stateprinter,它将状态转储到字符串中以进行比较。 它甚至可以为您编写和重写断言。 见https://github.com/kbilsted/StatePrinter/blob/master/doc/AutomatingUnitTesting.md的自动化和更多信息https://github.com/kbilsted/StatePrinter/blob/master/doc/TheProblemsWithTraditionalUnitTesting.md为您现在面对的有关单元测试问题的深入讨论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.