2
votes

I went with this solution:

Stubbing a property twice with rhino mocks

but even when I change both of my Stubs to .Expect, the first Expect is winning out:

Here's the recreation in mono:

using System;

using NUnit.Framework; using Rhino.Mocks;

namespace FirstMonoClassLibrary { [TestFixture] public class TestingRhinoMocks { 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();
    }

}

}

1
One option would be to create another TestFixture with a different Setup; another would be to create a parameterized factory method for the SUT; a third would be to avoid setting expectations in Setup. I ran into similar issues in the past and switched to Moq. :) - TrueWill
How do you know your test/sut isn't wrong? What does ` _dependency.GetValue` give you? Test that to see if your mock is behaving properly. - Adam Rackis
Here's recreating it in Mono (only have my mac at the moment), and it still fails. Wondering in the other thread I posted why that was seen as a solution when it doesn't work. - Mark W

1 Answers

3
votes

you need to do the following:

[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
}