简体   繁体   English

使用Rhino.Mocks模拟静态方法

[英]Mocking Static methods using Rhino.Mocks

Is it possible to mock a static method using Rhino.Mocks? 是否可以使用Rhino.Mocks模拟静态方法? If Rhino does not support this, is there a pattern or something which would let me accomplish the same? 如果Rhino不支持这个,是否有一种模式或某种东西可以让我做到这一点?

Is it possible to mock a static method using Rhino.Mocks 是否可以使用Rhino.Mocks模拟静态方法

No, it is not possible. 不,这是不可能的。

TypeMock can do this because it utilizes the CLR profiler to intercept and redirect calls. TypeMock可以这样做,因为它利用CLR分析器来拦截和重定向调用。

RhinoMocks, NMock, and Moq cannot do this because these libraries are simpler; RhinoMocks,NMock和Moq不能这样做,因为这些库更简单; they don't use the CLR profiler APIs. 他们不使用CLR分析器API。 They are simpler in that they use proxies to intercept virtual members and interface calls. 它们更简单,因为它们使用代理来拦截虚拟成员和接口调用。 The downside of this simplicity is that they cannot mock certain things, such as static methods, static properties, sealed classes, or non-virtual instance methods. 这种简单性的缺点是它们无法模拟某些事物,例如静态方法,静态属性,密封类或非虚拟实例方法。

将静态方法调用包装在另一个类的虚拟实例方法中,然后将其模拟出来。

If you can't use TypeMock to intercept the method call, the recommended pattern to use is to create a proxy that forwards to the non-virtual or static methods you are interested in testing, then set the expectation on the proxy. 如果您无法使用TypeMock拦截方法调用,则建议使用的模式是创建一个代理,该代理转发到您要测试的非虚拟或静态方法,然后在代理上设置期望。 To illustrate, consider the following classes. 为了说明,请考虑以下类。

class TypeToTest
{
    public void Method() { }
}

interface ITypeToTest
{
    void Method();
}

class TypeToTestProxy : ITypeToTest
{
    TypeToTest m_type = new TypeToTest();

    public void Method() { m_type.Method(); }
}

By creating this proxy, you can now use an ITypeToTest in place of where you were passing or setting a TypeToTest instance, making sure that the default implementation uses the TypeToTestProxy as it forwards to the real implementation. 通过创建此代理,您现在可以使用ITypeToTest代替传递的位置或设置TypeToTest实例,确保默认实现使用TypeToTestProxy因为它转发到实际实现。 Then, you can create a mock ITypeToTest in your test code and set expectations accordingly. 然后,您可以在测试代码中创建模拟ITypeToTest并相应地设置期望值。

Note that creating these proxies can be very tedious, error-prone, and time consuming. 请注意,创建这些代理可能非常繁琐,容易出错且耗时。 To address this I maintain a library and set of tools that generate assemblies containing these types for you. 为了解决这个问题,我维护了一个库和一组工具,可以为您生成包含这些类型的程序集。 Please refer to this page for more information. 有关更多信息,请参阅此页面

This is the biggest drawback to Rhino Mocks. 这是Rhino Mocks的最大缺点。 I don't know that it is even possible for Rhino Mocks to have this implemented without a re-concept of how it does its mocking. 我不知道Rhino Mocks甚至可以实现这一点而不重新考虑它是如何进行模拟的。

The only mock framework that I know of that supports mocking statics is TypeMock. 我所知道的唯一支持模拟静态的模拟框架是TypeMock。

As Rytmis suggested, you need to wrap the statics in something (ie an instance class with virtual methods or an interface) that you can then mock out. 正如Rytmis建议的那样,你需要将静态包装在某些东西(即带有虚方法或接口的实例类)中然后可以模拟出来。

I have been mocking using moq, I don't think we can mock static members using this because moQ creates a new proxy for the target (class or interface). 我一直在使用moq进行模拟,我认为我们不能使用这个模拟静态成员,因为moQ为目标(类或接口)创建了一个新的代理。 So only the inheritable members (virtual in case of class, public in terms of interface ) can be mocked. 因此,只有可继承的成员(在类的情况下为虚拟,在接口方面为public)可以被模拟。 Clearly, static members are not inherited, hence the problem. 显然,静态成员不是继承的,因此存在问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM