开发者

Mocking none interface classes

Is it true 开发者_运维百科that mocking frameworks in general and Rhino mocking in specific only mocks interfaces and classes that have virtual method? For example can I mock following simple class:

public class MyClass
{
    void method1()
    {
        //some code goes here
    }
}

If the answer is true, why such a limitation exists? Is there any work-around?


The limitation exists, because the mocking frameworks cannot change method1 without it being virtual or an interface. It is a reasonable limitation, since interfaces allow you to decouple your dependencies and is a common trait of good code, IMO. Unfortunately, most of the .Net framework does not have virtual methods or interfaces... which requires ugly wrapping in order to create interfaces.

If you really don't like it, one mocking framework option out there that gets past the limitation by hooking the CLR with some crazy magic foo. That famework is called TypeMock Isolator.


I am not sure how you would mock a class that did not implement an interface or have virtual methods. If some code is expecting an instance of MyClass and you pass it an instance of MyDerivedMockClass, then the MyClass members will be accessed not MyDerivedMockClass members because they are not overridden virtual members.


Typemock is your best bet.

http://site.typemock.com/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜