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);
有效,但如果我们有数千条记录,那么我需要编写数千行。
所以请回答 - 一行代码比较两个列表...
提前谢谢
答案 0 :(得分:1)
如果我理解正确,你只有两个List<Employee>
个实例,并且你想断言他们是平等的。即它们具有相同数量的Employee
实例,顺序相同,属性相同。
如果是这样,这与FakeItEasy无关。您只需要一种执行断言的方法。就个人而言,我会使用Fluent Assertions。
result.Should().BeEquivalentTo(emplist);
答案 1 :(得分:0)
您可以使用HashSet&lt;&gt;比较两个清单。
首先,定义一个像这样的均衡器
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));
答案 2 :(得分:0)
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(一);
答案 3 :(得分:-1)
其他答案中建议的比较方法要求您在所有对象上实现equals。从维护角度来看,这不能很好地扩展。此外,在某些测试中,仅比较字段的子集。这意味着要实现多个比较或等于方法。另一种方法是stateprinter,它将状态转储为字符串以进行比较。它甚至可以为您编写和重写您的断言。有关自动化和https://github.com/kbilsted/StatePrinter/blob/master/doc/AutomatingUnitTesting.md的更多信息,请参阅https://github.com/kbilsted/StatePrinter/blob/master/doc/TheProblemsWithTraditionalUnitTesting.md,以深入讨论您现在面临的单元测试问题。