【问题标题】:Why do Rhino Stubs allow me to set expectations on them?为什么 Rhino Stubs 允许我对它们设定期望?
【发布时间】:2016-09-15 06:40:32
【问题描述】:

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

但是,我很困惑为什么 Rhino Stub 对象提供了诸如 .Expect.VerifyAllExpectations() 之类的方法,而这些方法似乎根本什么都不做。为什么 mock/stub 对象看起来提供相同的接口?

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

【问题讨论】:

    标签: c# .net unit-testing mocking rhino-mocks


    【解决方案1】:

    这种行为的原因是基于 IntelliSense 限制(在扩展方法上)+ Rhinomocks 设计(+ 断言上的错误),正如我在 here 中解释的那样。

    以下示例显示,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 从未被调用...)

    【讨论】:

      猜你喜欢
      • 2013-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-10
      • 2017-02-08
      • 2018-11-16
      • 1970-01-01
      相关资源
      最近更新 更多