简体   繁体   English

我无法为此代码编写测试..有人告诉我该怎么做吗?

[英]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:我看到一些想法:

  1. Make the list public, with a private set - so you can observe it.使用私人设置公开列表 - 这样您就可以观察它。
  2. Create a getter for a Ireadonlycollection, so you can expose the list but disallow modifying the contents.为 Ireadonlycollection 创建一个 getter,以便您可以公开列表但不允许修改内容。
  3. You could use Internal visibillity too and configurre your project accordingly.您也可以使用内部可见性并相应地配置您的项目。

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.

相关问题 谁能告诉我我在摘要中犯错了吗 - Can anyone tell me were I am making mistake in the snippet 我正在尝试从 a.csv 读取信息并将其放入 C# 中的数组中。 谁能告诉我为什么代码不起作用? - I am trying to read information from a .csv and put it into an array in C#. Can anyone tell me why the code doesn't work? 谁能告诉我在Linq是否可行? - Can anyone tell me if this is possible to do in Linq? 谁能告诉我为什么我不能从项目外部访问此代码? - Can anyone tell me why I cant access this code from outside of it's project? 谁能告诉我这段代码在c#中到底意味着什么? - Can anyone tell me what exactly this code means in c#? 谁能告诉我这个ZeroMQ代码有什么问题吗? - Can anyone tell me what is wrong with this ZeroMQ code? 任何人都可以告诉我这部分代码有什么问题 - Can anyone tell me what's wrong in this part of code 谁能告诉我为什么此代码的可维护性指数仅为40? - Can anyone tell me why the maintainability index is only 40 for this code? 如何编写(测试)编译器/ JIT无法优化的代码? - How do I write (test) code that will not be optimized by the compiler/JIT? 有人能告诉我为什么这段代码不起作用吗? 我试图找到第 10001 个素数 - Can somebody tell me why this code is not working? I am trying to find the 10001st prime number
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM