Most of the Mock framework doesn’t support static method mocking. So most of the time you need to modify your code to make the code testable. I’m going to use Moq mocking framework to show mocking.
Class with static method:
Class under test and using static methods
In the above code, the static method call (MyHelper.GetHelp())need to be mocked. The problem is that MyHelper can’t be passed from outside and second problem is that Moq framework doesn’t have option to mock static method.
Approach to use to modify code to make static method mockable
Now let’s modify the code so that we can separate the static method invocation from our methods that we want to test. Let’s modify the MyManager class to make it testable.
1. Modify Code to separate dependencies on static methods:
In the following code snippet I have separated extracted a method in MyManager which will return the help. The extract method GetHelp method needs to be virtual and protected as this will be overridden by stub class.
2. Create a stub of the class under test in the test project
Now you can override the GetHelp method in the stub class as shown below. So your stub class is not using static method rather it’s doing something fake.
3. Test stub class instead of actual class
Now your stub class MyManageStub is alike the MyManager except the static method invocation is removed. Now you test will use MyManagerStub as shown below
The approach I have described in this post is a one of the approach taken from this link.