Moq how do you test internal methods?

前端 未结 6 1784
借酒劲吻你
借酒劲吻你 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 03:03

    From my point of view Mocking should be used to mock up some behavior that we are dependent on but are not setting out to test. Hence:

    Q: Am I missing something? - No you're not missing anything, MOQ is missing the ability to mock private behaviors.

    Q: Can you test internal methods using Moq? - If the result of the private behavior is visible publicly, then yes you can test the internal method but it's not because of Moq that you can test them. I would like to make a point here is that Mock is not the ability to test but rather the ability to similar behaviors that we are not testing but depend on.

    C: A main benefit with TDD is that your code becomes easy to change. If you start testing internals, then the code becomes rigid and hard to change - I don't agree with this comment for 2 main reasons: 1: It is not a beginner misconception, as TDD is not just about the ability to code faster but also better quality code. Hence the more test we can do the better. 2: It doesn't make the code anymore harder to change if you can somehow can test the internal methods.

提交回复
热议问题