2

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();
    }

}

}

Community
  • 1
  • 1
Mark W
  • 3,879
  • 2
  • 37
  • 53
  • 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](http://code.google.com/p/moq/). :) – TrueWill Nov 23 '11 at 20:11
  • 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 Nov 23 '11 at 20:24
  • 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 Nov 24 '11 at 17:58

1 Answers1

3

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
}
the_joric
  • 11,986
  • 6
  • 36
  • 57
  • Thanks, I had thought that the Record, back to record, replay were deprecated uses of the library. – Mark W Nov 24 '11 at 18:02
  • I would say that Expect() is also deprecated :) Personally I use only Stub() and AssertWasCalled(). – the_joric Nov 24 '11 at 20:57
  • Ok, so in that case how do you deal with needing to re-Stub() a different behavior? Do you just treat that as another testfixture? – Mark W Nov 28 '11 at 18:30
  • btw, I too only rely on AssertWas/WasNotCalled() and Stub(). Only issue I've had is this, trying to stub out different behavior for a different test. – Mark W Nov 28 '11 at 19:53
  • You may want to look at NSubstitute -- it has better syntax and easily re-stubs your substitute by just calling Returns() method again. See http://nsubstitute.github.com/help/replacing-return-values/ for details – the_joric Nov 30 '11 at 23:54