Moq, how do you test internal methods?

They say that my boss uses Moq, and that’s it. I like it, but it seems that unlike MSTest or mbunit, etc .... you cannot test internal methods.

So I have to post some internal implementation in my interface so that I can test it.

Did I miss something?

Can you test internal methods with Moq?

Thank you so much

+7
c # moq
source share
6 answers

There is nothing wrong with making the internals visible to other classes for testing. If you need to check the insides of a class, be sure to do it. Just because methods are not publicly available does not mean that you should ignore them and only test public ones. A well-designed application will in fact have most of the code encapsulated inside your classes so that they are not publicly available. Therefore, ignoring non-public methods in your testing is a big mistake IMHO. One of the beauty of unit testing is that you test all parts of your code, no matter how small and when your tests all work and work 100%, it is a very reasonable assumption that when all these parts are combined, the application will work correctly for end user. Of course, checking that the last part - those where the integration level tests are conducted - is another discussion. So do the test!

+5
source share

If you have a lot of code that is not tested by public methods, you probably have code that should be moved to other classes.

As another answer says, you can use the InternalsVisibleTo attribute for this. But this does not mean that you should do it.

+3
source share

From my point of view, Mocking should be used to model some kind of behavior on which we depend, but do not test. Consequently:

Q: Did I miss something? - No, you did not miss anything, the MOQ lacks the ability to imitate personal behavior.

Q: Can you test internal methods using Moq? - If the result of private behavior is visible in public, then yes, you can test the internal method, but this is not because of Moq, which you can check. I would like to emphasize that Mock is not the ability to test, but rather the ability to such behavior, which we do not test, but depend on it.

C: The main advantage of TDD is that your code becomes easy to change. If you start testing internal components, then the code becomes tough and difficult to change - I disagree with this comment for two main reasons: 1: This is not an innovative fallacy, since TDD is not just the ability to code faster, but also better quality code. Therefore, the more tests we can do, the better. 2: This does not make the code more difficult to change if you can somehow check the internal methods.

+2
source share

Your initial premise that it is necessary to test an internal method is a general misconception among beginners about hardware testing.

Of course, there may be cases where private methods must be tested in isolation, but the 99% common case is that private methods are implicitly tested because they force public methods to pass their tests. Public methods call private methods.

Private methods exist for some reason. If they do not lead to external testable behavior, then you do not need it.

Do any of your public tests run if you just delete them? If so, they are already undergoing testing. If not, why do you need them? Find out what they are for, and then express it in a test against the open interface.

The main advantage of TDD is that your code becomes easy to change. If you start testing internals, then the code becomes hard and hard to change.

+1
source share

InternalsVisibleTo is your friend for testing internal components. Remember to sign your builds and you are safe.

+1
source share

All Articles