为什么 Rhino Stubs 允许我对它们设定期望值?

Why do Rhino Stubs allow me to set expectations on them?

这个问题阐明了 Rhino 中模拟和存根之间的概念差异:What are the differences between mocks and stubs on Rhino Mocks?

然而,我很困惑为什么 Rhino Stub 对象提供 .Expect.VerifyAllExpectations() 等方法,而这些方法似乎什么都不做。为什么 mock/stub 个对象看似提供相同的接口?

这让我觉得我错过了一些基本的东西 - 或者它只是一个实现怪癖?

此行为的原因是基于 IntelliSense 限制(关于扩展方法)+ Rhinomocks 设计(+ 断言错误),正如我解释的那样

以下示例显示 Expect 方法在存根上无非是 Stub 方法。

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...)