犀牛制品存根和嘲笑是唯一的好接口? [英] Rhino Mocks stubs and mocks are only good for interfaces?

查看:229
本文介绍了犀牛制品存根和嘲笑是唯一的好接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它是正确的犀牛嘲笑存根和嘲笑是唯一的好接口,而不是具体的类?我花了相当多的时间试图让这片code工作的。没想到废止pubSubClient总是从调用类的发送方法。这种方法有一定的相关性,并抛出异常。

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.

这是否意味着我要提取的每一个类我想嘲笑/存根与犀牛的接口?还是我失去了一些东西,在技术上或概念?

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?

更新:好吧,看来我想通了什么角色我失踪: 犀牛制品不能拦截对非虚方法。所以,我想我要么使用的接口,使得在具体的类中的每个方法的虚拟。请纠正我,如果有另一种选择。

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's answer of using partial mocks is incorrect. That's not what partial mocks are for.

乔恩·埃里克森的回答多半是正确的:犀牛制品和最小起订量不能拦截非虚调用,也不能截取静态方法或属性。这意味着你可以不假如下:

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可以伪造这些,因为乔恩提及。

TypeMock can fake these, as Jon mentioned.

应该注意到有一个额外的嘲讽框架,可以拦截所有来电:在微软摩尔框架。它的工作原理相同的方式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.

痣是免费的(现在)。这也是测试版。痣只仅适用于微软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.

这篇关于犀牛制品存根和嘲笑是唯一的好接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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