Rhino Mocks stubs and mocks are only good for interfaces?(Rhino Mocks 存根和模拟仅适用于接口?)
问题描述
Rhino Mocks 存根和模拟仅适用于接口,而不适用于具体类,这对吗?我花了很长时间试图让这段代码正常工作.我没想到存根的 pubSubClient 总是从类中调用 Send 方法.该方法有一些依赖并抛出异常.
Is it correct that Rhino Mocks stubs and mocks are only good for interfaces, not concrete classes? I spent quite a time trying to make this piece of code working. I did not expect the stubbed pubSubClient to always call Send method from the class. That method has some dependencies and throws exception.
[Test]
public void Test01()
{
PubSubMessage psm = new PubSubMessage();
var pubSubClient = MockRepository.GenerateStub<PubSubClient>();
pubSubClient.Stub(x => x.Send(psm)).IgnoreArguments().Return(null);
// actual PubSubClient Send method throws exception
// the rest of the test is skipped...
}
但是,当我提取接口并使用 IPubSubClient 运行相同的测试时,它似乎按预期工作.
However, when I have extracted the interface and run the same test with IPubSubClient, it seems to be working as expected.
这是否意味着我必须为我想用 Rhino 模拟/存根的每个类提取接口?还是我在技术上或概念上遗漏了什么?
Does it mean that I have to extract the interface for every class I want to mock/stub with Rhino? Or I am missing something, technically or conceptually?
更新:好的,看来我知道我遗漏了哪一部分:Rhino Mocks 无法拦截对非虚拟方法的调用.所以,我想我要么使用接口,要么使具体类上的每个方法都是虚拟的.如果还有其他选择,请纠正我.
UPDATE: OK, It seems I figured out what part I was missing: Rhino Mocks cannot intercept calls to non-virtual methods. So, I guess I have either use interfaces or make every method on the concrete class virtual. Please correct me if there's another option.
推荐答案
Bryan 使用部分模拟的答案不正确.这不是部分模拟的用途.
Bryan's answer of using partial mocks is incorrect. That's not what partial mocks are for.
Jon Erickson 的回答基本正确:Rhino Mocks 和 Moq 无法拦截非虚拟调用,也无法拦截静态方法或属性.这意味着您不能伪造以下内容:
Jon Erickson's answer is mostly correct: Rhino Mocks and Moq can't intercept non-virtual calls, nor can they intercept static methods or properties. That means you can't fake the following:
DateTime.Now; // static property, can't fake static property
someClass.SomeNonVirtualMethod(); // can't fake non-virtual method
sealedClass.Foo(); // can't fake anything on sealed classes
Utilities.SomeStaticMethod(); // can't fake static methods
someList.Any(); // can't fake extension methods like Linq's .Any()
TypeMock 可以伪造这些,正如 Jon 提到的那样.
TypeMock can fake these, as Jon mentioned.
需要注意的是,还有一个额外的模拟框架可以拦截所有调用:Microsoft Moles 框架.它的工作方式与 TypeMock 相同,它使用 .NET 分析器 API 来拦截调用.
It should be noted there is an additional mocking framework that can intercept all calls: the Microsoft Moles framework. It works the same way TypeMock does, it uses the .NET profiler API to intercept calls.
Moles 是免费的(目前).它也是测试版.Moles 仅适用于 Microsoft Pex 工具.而且它的 API 明显不如 TypeMock 精致、优雅的 API.
Moles is free (for now). It's also beta. Moles only only works with Microsoft Pex tools. And its API is clearly inferior to TypeMock's refined, elegant API.
这篇关于Rhino Mocks 存根和模拟仅适用于接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Rhino Mocks 存根和模拟仅适用于接口?
基础教程推荐
- rabbitmq 的 REST API 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- SSE 浮点算术是否可重现? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- c# Math.Sqrt 实现 2022-01-01