Question

As some of you will know, it is generally not possible to mock a static method in .net.

By mocking, I mean to replace a method in a class with another method with the same signature, usually for testing purposes.

The two main methods used for mocking a method are to declare it virtual or define it in an interface. Neither of these two are allowed for .net static methods.

However, there is an expensive tool called "Typemock Isolator" which allows for mocking of static methods. How does Isolator accomplish this seemingly impossible feat?

Was it helpful?

Solution

It's in the FAQ.

Basically, a Typemock fake instantiates a new object of the original type and hooks into each and every method and property call for that object, redirecting the call to the fake and returning the values you specify. It uses some deep voodoo in the .NET Framework (the .NET Profiler API, if you're interested) to do this, but it's all "legit". Technically anyone can build a duplicate of Typemock, but I have yet to see one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top