我正在开发 MVC 应用程序并编写单元测试。我对单元测试的编码模式/过程感到困惑。我正在编写单元测试,但我不知道我是否以正确的方式编写。
我给出了一个测试用例的例子,请检查。
基本上,在测试方法中,我正在编写与在 GetPartiesByEmployee() 方法中编写的相同代码,并且我正在比较否。从方法返回的记录和从测试方法中的代码博客返回的记录是正确的吗?
那是对的吗 ?
[测试方法]
public void Test_Get_Parties_By_Employee_Method() { var actualResult = oPartyHelper.GetPartiesByEmployee(6); Employee oEmployee = new Employee(); oEmployee = db.Employees.Find(6); var roles = oEmployee.Roles.ToList(); List<Party> parties = new List<Party>(); foreach (Role item in roles) { var PartyCollection = from e in item.Parties.OrderBy(e => e.Name) where (e.IsDeleted == false || e.IsDeleted == null) select e; parties.AddRange(PartyCollection.ToList()); } parties=parties.Distinct().OrderBy(p => p.Id).ToList(); var expectedCount = parties.Count(); var actualList = (List<Party>)actualResult; var actualCount = actualList.Count; Assert.AreEqual(expectedCount, actualCount, "All parties are not same"); }
实际方法:
public List<Party> GetPartiesByEmployee(int employeeId)
{
Employee oEmployee = new Employee();
oEmployee = db.Employees.Find(employeeId);
var roles = oEmployee.Roles.ToList();
List<Party> parties = new List<Party>();
foreach (Role item in roles)
{
var PartyCollection = from e in item.Parties.OrderBy(e => e.Name)
where (e.IsDeleted == false || e.IsDeleted == null)
select e;
parties.AddRange(PartyCollection.ToList());
}
return parties.Distinct().OrderBy(p=>p.Id).ToList();
}