Moq how do you test internal methods?

前端 未结 6 1686
借酒劲吻你
借酒劲吻你 2021-01-05 02:02

Told by my boss to use Moq and that is it. I like it but it seems that unlike MSTest or mbunit etc... you cannot test internal methods

So I am forced to make public

6条回答
  •  鱼传尺愫
    2021-01-05 02:56

    There is nothing wrong with making the internals visible to other classes for testing. If you need to test the internals of a class, by all means do so. Just because the methods are not public does not mean you should ignore them and test only the public ones. A well designed application actually will have a majority of the code encapsulated within your classes in such a way that they are not public. So ignoring the non-public methods in your testing is a big mistake IMHO. One of the beauties of unit testing is that you test all the parts of your code, no matter how small and when your tests are all up and running at 100% it is a very reasonable assumption that when all these parts are put together, your application will work properly for the end user. Of course verifying that latter part is where integration level tests come in - which is a different discussion. So test away!!!

提交回复
热议问题