This is the code..
this class has list, method to add project into list
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
I am stuck At 2)Act and 3)Assert
CodePudding user response:
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.
I see some ideas:
- Make the list public, with a private set - so you can observe it.
- Create a getter for a Ireadonlycollection, so you can expose the list but disallow modifying the contents.
- You could use Internal visibillity too and configurre your project accordingly.
CodePudding user response:
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.
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 void AddProject(Project p) // Method for project addition
{
if (!HasProject(p)) // 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;
}
