Is it recommended to mock concrete class?

后端 未结 5 2033
粉色の甜心
粉色の甜心 2020-12-02 10:09

Most of the examples given in mocking framework website is to mock Interface. Let say NSubstitute that I\'m currently using, all their mocking examples is to mock interface.

5条回答
  •  我在风中等你
    2020-12-02 10:26

    Its not that it is recommended, it's that you can do this if you have no other choice.

    Usually well designed project rely on defining interfaces for your separate components so you can tests each of them in isolation by mocking the other ones. But if you are working with legacy code /code that you are not allowed to change and still want to test your classes then you have no choice and you cannot be criticized for it (assuming you made the effort to try to switch these components to interfaces and were denied the right to).

提交回复
热议问题