问题描述
我是 mocking 的新手,所以我在这里可能完全错了,但我相信大多数 mocking 框架都依赖于接口.不幸的是,我们的大部分代码都没有使用接口.前几天我看到了一个 Java 中的 Mocking 框架,它重现了类对象的字节码,以不调用其内部方法,但您仍然可以测试它是否正在调用这些方法.
I am new to mocking so I might have it totally wrong here but I believe that most mocking frameworks are interface dependent. Unfortunately most of our code is not using an interface. Now the other day I saw a Mocking framework in Java that reproduced the byte code of a classobject as to not call its internal methods but you could still test that it WAS calling these methods.
我的问题是:.Net 是否有任何模拟框架可以做类似的事情?我正在寻找免费的东西,我不想要需要虚拟或抽象方法的东西.
My question is: does .Net have any mocking frameworks that can do a similar thing? I am looking for something free and I don't want something that requires methods to be virtual or abstract.
推荐答案
微软研究院开发了 Moles 为此,它是 Pex 的一部分,但可以独立安装.它是免费的.网站上有一篇很好的 介绍性文章 (pdf) 解释了如何模拟静态方法.他们需要一些时间才能找到您想要的东西(第 16 页,任务 3).
Microsoft Research has developed Moles for this, which is a part of Pex but can be installed independently. And it's free. There's a good introductory article (pdf) on the website that explains how to mock a static method. It takes some time before they get to the stuff you want (page 16, Task 3).
这里和 这里(频道9) 你可以找到一个关于如何存根 DateTime.Now
的例子.使用 Moles,你可以模拟任何你想要的东西.
Here and here (Channel 9) you can find an example on how to stub DateTime.Now
. Using Moles, you can mock anything you want.
这篇关于C# 的非接口依赖模拟框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!