本文介绍了为什么Rhino Stubs让我对它们设置期望?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题阐明了Rhino中模拟和存根之间的概念差异:

This question clarifies the conceptual differences between mocks and stubs in Rhino: What are the differences between mocks and stubs on Rhino Mocks?

但是我却感到困惑,为什么Rhino Stub对象提供了诸如.Expect.VerifyAllExpectations()之类的方法,而这些方法似乎根本没有任何作用.为什么模拟/存根对象似乎提供相同的接口?

However I'm left confused why Rhino Stub objects provide methods such as .Expect and .VerifyAllExpectations() when these appear to do nothing at all. Why do mock/stub objects seemingly provide the same interface?

这让我觉得我已经错过了一些基本知识-或者仅仅是实现上的怪癖?

It's making me think I've missed something fundamental - or is it just an implementation quirk?

推荐答案

这种行为的原因是基于IntelliSense限制(在扩展方法上)+ Rhinoomocks设计(+断言上的错误),正如我所解释的.

The reason for this behavior is based on IntelliSense limitation(on extension methods) + Rhinomocks design( + bug on the asserts) as I explained here.

以下示例显示了Expect方法对存根而言仅是Stub方法.

The following example shows that the Expect method has nothing more than Stub method on stubs.

public class Foo
{
    public virtual string DoSomthing()
    {
        return String.Empty;
    }
}

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {

        var f = MockRepository.GenerateStub<Foo>();

        f.Expect(x => x.DoSomthing())
         .Return("2");

        f.VerifyAllExpectations();

    }
}

如果执行上面的示例,您将看到测试不会失败(尽管从未调用过DoSomthing ...)

If you'll execute the above example you'll see that the test won't fail(Although DoSomthing was never called...)

这篇关于为什么Rhino Stubs让我对它们设置期望?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 18:41