如何对抽象类进行单元测试 [英] How to unit test abstract classes
问题描述
使用了Visual Studio中的创建单元测试工具,显然它试图实例化我的抽象类.
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?
谢谢.
推荐答案
如果此抽象类上有一些值得测试的方法,则应该对其进行测试.您始终可以将测试的抽象类子类化(并命名为MyAbstractClassTesting),然后测试这个新的具体类.
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.
这篇关于如何对抽象类进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!