Moq how do you test internal methods?

前端 未结 6 1687
借酒劲吻你
借酒劲吻你 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!!!

    0 讨论(0)
  • 2021-01-05 03:00

    You can use the InternalsVisibleTo attribute to make the methods visible to Moq.

    http://geekswithblogs.net/MattRobertsBlog/archive/2008/12/16/how-to-make-a-quotprotectedquot-method-available-for-quotpartialquot-mocking-and-again.aspx

    0 讨论(0)
  • 2021-01-05 03:00

    InternalsVisibleTo is your friend for testing internals. Remember to sign your assemblies and you're safe.

    0 讨论(0)
  • 2021-01-05 03:02

    If you have many code that isn't tested by the public methods, you probably have code that should be moved to another classes.

    As said in another answer, you can use the InternalsVisibleTo attribute for that. But that doesn't mean you should do it.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-05 03:05

    Your initial presumption that it is necessary to test internal method is a common beginners misconception about unit testing.

    Granted, there may exist cases where private methods should be tested in isolation, but the 99% common case is that the private methods are being tested implicitly because they make the public methods pass their tests. The public methods call the private methods.

    Private methods are there for a reason. If they do not result in external testable behaviour, then you don't need them.

    Do any of your public tests fail if you just flat out delete them? If yes, then they are already being tested. If not, then why do you need them? Find out what you need them for and then express that in a test against the public interface.

    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.

    0 讨论(0)
提交回复
热议问题