How to unit test abstract classes: extend with stubs?

后端 未结 14 1733
有刺的猬
有刺的猬 2020-11-27 08:37

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 abstr

14条回答
  •  迷失自我
    2020-11-27 09:15

    If an abstract class is appropriate for your implementation, test (as suggested above) a derived concrete class. Your assumptions are correct.

    To avoid future confusion, be aware that this concrete test class is not a mock, but a fake.

    In strict terms, a mock is defined by the following characteristics:

    • A mock is used in place of each and every dependency of the subject class being tested.
    • A mock is a pseudo-implementation of an interface (you may recall that as a general rule, dependencies should be declared as interfaces; testability is one primary reason for this)
    • Behaviors of the mock's interface members -- whether methods or properties -- are supplied at test-time (again, by use of a mocking framework). This way, you avoid coupling of the implementation being tested with the implementation of its dependencies (which should all have their own discrete tests).

提交回复
热议问题