Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does "Typemock Isolator" mock static methods?

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?

like image 715
Vivian River Avatar asked Jul 08 '10 19:07

Vivian River


1 Answers

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.

like image 156
Randolpho Avatar answered Oct 05 '22 03:10

Randolpho