如何对抽象类进行单元测试:使用存根扩展? [英] How to unit test abstract classes: extend with stubs?

查看:167
本文介绍了如何对抽象类进行单元测试:使用存根扩展?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何对抽象类和扩展抽象类的类进行单元测试。

I was wondering how to unit test abstract classes, and classes that extend abstract classes.

我应该通过扩展它来测试抽象类,剔除抽象方法,然后测试所有具体方法?然后只测试我覆盖的方法,并在单元测试中测试扩展我的抽象类的对象的抽象方法?

Should I test the abstract class by extending it, stubbing out the abstract methods, and then test all the concrete methods? Then only test the methods I override, and test the abstract methods in the unit tests for objects that extend my abstract class?

我是否应该有一个抽象的测试用例可以用于测试抽象类的方法,并在我的测试用例中为扩展抽象类的对象扩展这个类?

Should I have an abstract test case that can be used to test the methods of the abstract class, and extend this class in my test case for objects that extend the abstract class?

注意我的抽象类有一些具体的方法。

Note that my abstract class has some concrete methods.

推荐答案

编写一个Mock对象并将其用于测试。它们通常非常非常小(继承自抽象类)而不是更多。然后,在单元测试中,您可以调用要测试的抽象方法。

Write a Mock object and use them just for testing. They usually are very very very minimal (inherit from the abstract class) and not more.Then, in your Unit Test you can call the abstract method you want to test.

您应该测试包含某些逻辑的抽象类,就像您拥有的所有其他类一样。

You should test abstract class that contain some logic like all other classes you have.

这篇关于如何对抽象类进行单元测试:使用存根扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆