gpt4 book ai didi

.net - 模拟框架(在 .Net 中)如何创建模拟对象?

转载 作者:行者123 更新时间:2023-12-04 00:53:29 27 4
gpt4 key购买 nike

在 Microsoft .Net 框架的上下文中,我真的很好奇模拟框架(Rhino Mocks、Moq、NMock 等)实际上是如何从给定类型创建模拟对象的。

我对一种方法的特定机制感兴趣,或者如果他们使用不同的方法,也许对不同方法的一些概述感兴趣。或者,如果有人能指出我的一些文章,那也很棒。

最佳答案

有不同的模拟技术。

一些模拟库,如 MoqRhinoMocks使用 Castle Dynamic proxies .本质上,它们使用反射和运行时代码生成 (CodeDom) 来动态生成类,这些类要么实现被模拟的接口(interface),要么派生自被模拟的非密封类。

TypeMock有点不同 - 它使用 Profiler API 来拦截和重定向对任何类型的任何方法的调用。这使得它能够模拟密封类型、系统类,甚至拦截和转移对具体类型的非虚拟方法的调用。

更新:来自 TypeMock website :

Typemock Isolator uses an Aspect- Oriented programming design that creates a mock aspect. Internally, it uses the .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.

关于.net - 模拟框架(在 .Net 中)如何创建模拟对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3587264/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com