How does “Typemock Isolator” mock static methods?

落爺英雄遲暮 提交于 2019-12-09 08:52:49

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/3207028/how-does-typemock-isolator-mock-static-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!