The It.Is and It.IsAny methods of the Moq unit-testing framework and why your "partial mocking" might do not work
Unit-testing of existing code can sometimes be a challenge, since you have to deal with classes that contain too much of functionality. In that case the “Single Responsibility” principle was not in focus, however, refactoring the code can be an issue, so you will have to unit-test the class as it is. Apart from that we do not want to “mock” the Interface of a class, which would mock all its method, but the class it self.
Such classes are calling methods of the same class, so the task is to mock only few of the methods, but not all of them, since we also want to unit-test some of them. The feature of partially mocking the methods of a class is called partial mocking. The methods that we want to mock, have to be defined as virtual, so that Moq can ovveride them. This feature can be a sign of code smell, when overused.
There are some great examples in StackOverflow on how to achieve that, however, in this article I would like to focus on an issue that caused my partial mocks not to work and show you how I fixed it.
Read the complete article