[英]I am not able to write test for this code ..anyone tell me how to do?
This is the code..这是代码..
this class has list, method to add project into list此 class 有列表,将项目添加到列表中的方法
class ABC
{
private List<Project> List { get; set; } = new List<Project>(); // List for project
public List<Project> List
{
get { return ProjectList; }
}
public void AddProject(Project p) // Method for project addition
{
if (!List.Any(x => x.Id == p.Id)) // to check project id already not exist
{
List.Add(p); // it will add if already not into list
}
}
}
I want to write n-unit test for "AddProject" method我想为“AddProject”方法编写 n 单元测试
I am stuck At 2)Act and 3)Assert我被困在 2)Act 和 3)Assert
As Somebody mentioned, there is no way to check that the List was modified - you need some externally visible change to make a useful test.正如有人提到的,没有办法检查 List 是否已修改 - 您需要一些外部可见的更改才能进行有用的测试。
I see some ideas:我看到一些想法:
Here is a full example:这是一个完整的例子:
i have added a new function called HasPrject you can use it in any place to check if the list has a specific project.我添加了一个名为HasPrject的新 function,您可以在任何地方使用它来检查列表是否有特定项目。
public class Program
{
static void Main(string[] args)
{
ABC abc = new ABC();
Project p = new Project("1");
abc.AddProject(p);
Console.WriteLine(abc.HasProject(p));
}
}
class ABC
{
private List<Project> List { get; set; } = new List<Project>();
public Boolean HasProject(Project p){
return List.Any(x => x.Id == p.Id);
}
public Boolean HasProject(Project p){
return List.Any(x => x.Id == p.Id);
}
public void AddProject(Project p) // Method for project addition
{
if (!List.Any(x => x.Id == p.Id)) // to check project id already not exist
{
List.Add(p); // it will add if already not into list
}
}
}
class Project
{
public Project(string Id)
{
this.Id = Id;
}
public string Id;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.