简体   繁体   English

使方法可用于单元测试C#

[英]Make method testable for unit tests c#

So , i have the following code structure 所以,我有以下代码结构

public void method1(int index)
{
   switch(index)
   {
       case 1:
           Method2();break;
       case 2:
            Method3();break;
   }
 }

 public void Method2()
 {
     var entity = new SomeEntity();
     repo.Add(entity);

     var anotherEntity= new AnotherEntity();
     repo.Update(anotherEntity);
 }

While covering method2 with unit test, i ran into issue, that if i want to check that entity add to db, anyway it runs an update method too. 在用单元测试介绍method2时,我遇到了一个问题,即如果我想检查将实体添加到db,无论如何它也会运行一个更新方法。 How i can split it somehow, just wanna get maybe some best practices for method in which it's needed to do multiple operations with db. 我如何以某种方式拆分它,只是想获得一些方法的最佳实践,该方法需要使用db进行多次操作。 Thanks for helping me! 感谢您的帮助!

In tests you need to test only behaviour of the object. 在测试中,您只需要测试对象的行为。
In your case behaviour of your object is adding one entity and update another. 在您的情况下,对象的行为是添加一个实体并更新另一个实体。
Object have public method Method2 which responsible for this behaviour. 对象具有负责此行为的公共方法Method2

So you ending up with two tests, one for updating and one for adding. 因此,您最终进行了两项测试,一项用于更新,另一项用于添加。 You can mock a repository and tests that methods Add and Update called with expected arguments. 您可以模拟存储库并测试使用预期参数调用的AddUpdate方法。 Or will be better if you can use "InMemory" database and have tests which cover persistence layer as well. 或者,如果可以使用“ InMemory”数据库并进行覆盖持久层的测试,那就更好了。

It is ok that Update executed also in test for Add , you will assert only behaviour of Add and ignore Update method. 可以在对Add测试中执行Update也可以,您只需声明Add行为,而忽略Update方法。

Your problem have nothing to do with Single Responsibility Principle. 您的问题与“单一责任原则”无关。 Single Responsibility Principle means that object should have only one reason to change . 单一责任原则意味着对象应该只有一个改变的原因

In your case if updating another entity is part of logic for adding another one - it should stay in one class/method and been tested as whole. 在您的情况下,如果更新另一个实体是添加另一个实体的逻辑的一部分,则应将其保留在一个类/方法中并进行整体测试。

First of all Method2() violates the single responsibility principle of SOLID 首先Method2()违反了SOLID的单一责任原则

Single Responsibility Principle : a class should have only a single responsibility (ie changes to only one part of the software's specification should be able to affect the specification of the class). 单一职责原则 :类应仅具有单一职责(即,仅对软件规范的一部分进行更改才能影响该类的规范)。

These operations need to be in their own methods and in doing so will lead to: 这些操作需要采用自己的方法,这样做会导致:

  • Better maintainability 更好的可维护性
  • Better options for writing unit tests. 编写单元测试的更好选择。

     public void Method2() { //This method only adds new entities var entity = new SomeEntity(); repo.Add(entity); } public void Method3() { //This method only updates entities var anotherEntity= new AnotherEntity(); repo.Update(anotherEntity); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM