Kevin Meredith Kevin Meredith - 20 days ago 7
C# Question

Mocking Static Methods

Recently, I've begun to use Moq to unit test. I use Moq to mock out classes that I don't need to test.

How do you typically deal with static methods?

public void foo(string filePath)
{
File f = StaticClass.GetFile(filePath);
}


How could this static method,
StaticClass.GetFile()
get mocked?

P.S. I'd appreciate any reading materials you recommend on Moq and Unit Testing.

Answer

Mocking frameworks like Moq or Rhinomocks can only create mock instances of objects, this means mocking static methods is not possible.

You can also search Google for more info.

Also, there's a few questions previously asked on StackOverflow here, here and here.