views:

97

answers:

1

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?

+2  A: 

It's right there on the website, also 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.

Randolpho
It says on the web site, "Typemock Isolator uses aspect-oriented technology to redirect calls from the real code. This enables developers to define the behavior of the external component required for a tested scenario." I was hoping to be able to know more about how that happens. I've heard of Aspect-Oriented "post-compiler" tools like PostSharp.
Rice Flour Cookies
@Rising Star: Sorry, I've edited to add more info. PostSharp actually modifies compiled code. Typemock uses the .NET Profiler API to actually intercept method calls *as they're being called* and redirect them to the fake.
Randolpho
ACCEPTED: .net profiler api. Does it say somewhere else on their web page that they use the .net profiler api, or do you know some other way?
Rice Flour Cookies
@Rising Star: it's on the [FAQ](http://site.typemock.com/typemock-faq/features/how-does-typemock-isolator-work.html), which I linked in my answer. `Internally, it usesthe .NET framework profiler API to monitor an application's execution. When a method is called, the CLR notifies Typemock Isolator; the framework can then return mocked values and override the original code completely.`
Randolpho