Rhino模拟stub和expect,总是选择第一个原因

本文关键字:选择 第一个 模拟 stub expect Rhino | 更新日期: 2023-09-27 18:15:28

我采用了这样的解决方案:

使用rhino mock对一个属性进行两次存根处理

但是当我把两个存根都改成。Expect时,第一个Expect胜出:

这是单声道的再现:

using System;
使用NUnit.Framework

;使用Rhino.Mocks;

FirstMonoClassLibrary

名称空间{(有关)公共类testinggrhinomocks{Sut _systemUnderTest;IFoo _dependency;

    [SetUp]
    public void Setup()
    {
        _dependency = MockRepository.GenerateMock<IFoo>();
        _dependency.Expect(x => x.GetValue()).Return(1);
        _systemUnderTest = new Sut(_dependency);
    }
    [Test]
    public void Test()
    {
        _dependency.Stub(x => x.GetValue()).Return(2);
        var value = _systemUnderTest.GetValueFromDependency();
        Assert.AreEqual(2, value);  // Fails  says it's 1
    }   
}
public interface IFoo
{
    int GetValue();
}
public class Sut
{
    private readonly IFoo _foo;
    public Sut(IFoo foo)
    {
        _foo = foo;
    }   
    public int GetValueFromDependency()
    {
        return _foo.GetValue();
    }
}

}

Rhino模拟stub和expect,总是选择第一个原因

您需要做以下操作:

[Test]
public void Test()
{
   _dependency.BackToRecord();
   _dependency.Expect(_ => _.GetValue).Return(2);
   _dependency.Replay();
   var value = _systemUnderTest.GetValueFromDependency();
   value.ShouldBe(2);   // Fails  says it's 1
}