Well, I think everyone's comments so far is technically correct - using something like RhinoMocks
or Moq
, you really can't mock static methods in a facile, straightforward manner.
But using Moles
, you definitely can. So if you have significant (currently) untestable code that reside within static methods, I think you should be looking into Moles.
(This link is a bit dated but I still find it helpful)
http://research.microsoft.com/en-us/projects/pex/molesmanual.pdf
(Key text)
Moles can be used to detour any .NET method, including non-virtual and static methods in sealed types.
How it works: Suppose you have a typical situation like this:
public static class SomeStaticClass
{
public static int SomeStaticMethod(string s)
{
return "Static method called: " + s;
}
}
public class SomeInstanceClass
{
public string SomeInstanceMethod(string s)
{
return SomeStaticClass.SomeStaticMethod(s);
}
}
Using Moles, your test code would look like this:
[TestMethod()]
[HostType("Moles")]
public void ShouldBeAbleToTestStaticMethod()
{
var instance = new SomeInstanceClass();
var testValue = instance.SomeInstanceMethod("Some test string");
SomeStaticClass.SomeStaticMethod = (s) => "Moled you! " + s;
Assert.That(testValue, Is.EqualTo("Moled you! Some test string"); // sorry, this has code smell, lol
}
Of course you need to set up Moles into your test project, so be sure to look it up - lots of web resources to help you on your way.
Some helpful posts:
https://msdn.microsoft.com/en-us/library/ff798308.aspx
http://adventuresdotnet.blogspot.com/2011/03/mocking-static-methods-for-unit-testing.html
https://wannabeegeek.wordpress.com/2013/03/13/unit-testing-made-easy-with-moles-part-i/