"Typemock隔离器"如何进行?模拟静态方法? [英] How does "Typemock Isolator" mock static methods?

查看:89
本文介绍了"Typemock隔离器"如何进行?模拟静态方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您中有些人会知道,通常无法在.net中模拟静态方法.

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.

用于模拟方法的两个主要方法是将其声明为虚拟方法或在接口中定义它. .net静态方法都不允许使用这两种方法.

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.

但是,有一个昂贵的工具叫做"Typemock Isolator",它可以模拟静态方法.隔离器如何完成这项看似不可能的壮举?

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

推荐答案

它位于基本上,Typemock伪造品会实例化原始类型的新对象,并挂接到该对象的每个方法和属性调用中,将调用重定向到该伪造品并返回您指定的值.它在.NET Framework( .NET Profiler API (如果您有兴趣)执行此操作,但这全都是合法".从技术上讲,任何人都可以构建Typemock的副本,但是我还没有看到.

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.

这篇关于"Typemock隔离器"如何进行?模拟静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆