简体   繁体   English

如何对抽象类进行单元测试

[英]How to unit test abstract classes

Used the create unit tests tool in Visual Studio and obviously it tries to instantiate my abstract classes.在 Visual Studio 中使用了创建单元测试工具,显然它试图实例化我的抽象类。

My question is: Should I try to unit test the way Visual Studio is trying to get me to do it, or should I create a mock class to be instantiated, or should I only test the methods that use this abstract class?我的问题是:我应该尝试对 Visual Studio 试图让我这样做的方式进行单元测试,还是应该创建一个要实例化的模拟类,或者我应该只测试使用这个抽象类的方法?

Thanks.谢谢。

If there are methods on this abstract class that are worth testing, then you should test them.如果这个抽象类上有值得测试的方法,那么你应该测试它们。 You could always subclass the abstract class for the test (and name it like MyAbstractClassTesting) and test this new concrete class.您始终可以为测试创建抽象类的子类(并将其命名为 MyAbstractClassTesting)并测试这个新的具体类。

There are two opposite points of view:有两种相反的观点:

  • Do not test abstract class itself, test concrete classes inherited from it不要测试抽象类本身,测试从它继承的具体类
  • Abstract class should be tested as well because provides some built in logic shared across all the inherited classes so you just test base logic in abstract class once抽象类也应该进行测试,因为它提供了一些在所有继承类之间共享的内置逻辑,因此您只需在抽象类中测试一次基本逻辑

I prefer second option (currently) and testing abstract classes using RhinoMocks PartialMock feature which allows me to create a mock of an abstract class.我更喜欢第二个选项(当前)并使用RhinoMocks PartialMock功能测试抽象类,功能允许我创建抽象类的模拟。

  1. Just test the implementing classes.只需测试实现类。

  2. You could always create a specific implementation for testing that adds no extra functionality.您始终可以为测试创建一个特定的实现,它不会增加额外的功能。

  3. Listen to the tests.听测试。 Using mocking tools that do magic to allow testing abstract classes and private methods etc. are a test code smell使用具有魔力的模拟工具来允许测试抽象类和私有方法等是一种测试代码味道

use from mockrepository :从 mockrepository 使用:

[testmethod]
       public void testwithmockrepository()
       {
           var mockrepository = new rhino.mocks.mockrepository();
           var mock = mockrepository.partialmock<myabstractclass>();

           using ( mockrepository.record() )
           {
               expect.call( mock.dosomething( arg<string>.is.anything ) ).return( "hi..." ).repeat.once();
           }
           using ( mockrepository.playback() )
           {
               assert.areequal( "hi..." , mock.dosomething( "salam" ) );
           }
       }

I would not test the abstract classes because of a very simple reason: the implementing class may have their own implementation of certain methods - if you test the abstract class, you wont have idea how the further code actually behaves.我不会测试抽象类,因为一个非常简单的原因:实现类可能有自己的某些方法的实现 - 如果您测试抽象类,您将不知道进一步代码的实际行为。 Moreover, if you test abstract classe's implemented methods, then you are binding your test with the abstract class implementation - but you can't create object of abstract class so what is the use of such test?此外,如果您测试抽象类的实现方法,那么您将测试与抽象类实现绑定 - 但您不能创建抽象类的对象,那么这种测试有什么用? :) :)

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

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