Why is it so bad to mock classes?

前端 未结 9 1888
一向
一向 2020-12-07 17:57

I recently discussed with a colleague about mocking. He said that mocking classes is very bad and should not be done, only in few cases.

He says that only interfaces

9条回答
  •  旧巷少年郎
    2020-12-07 18:01

    Generally you'd want to mock an interface.

    While it is possible to mock a regular class, it tends to influence your class design too much for testability. Concerns like accessibility, whether or not a method is virtual, etc. will all be determined by the ability to mock the class, rather than true OO concerns.

    There is one faking library called TypeMock Isolator that allows you to get around these limitations (have cake, eat cake) but it's pretty expensive. Better to design for testability.

提交回复
热议问题