views:

666

answers:

1

Can I change the behaviour of a stub during runtime? Somthing like:

    public interface IFoo { string GetBar(); }
    [TestMethod]
    public void TestRhino()
    {
        var fi = MockRepository.GenerateStub<IFoo>();
        fi.Stub(x => x.GetBar()).Return("A");
        Assert.AreEqual("A", fi.GetBar());
        fi.Stub(x => x.GetBar()).Return("B");
        Assert.AreEqual("B", fi.GetBar());    // Currently fails here
    }

My code example still fails in the given line, fi.GetBar() still returns "A".

Or is there another trick to model stubs whose behaviour changes over time? I'd rather not resort to using fi.Stub(...).Do(...).

Ah, probably I just need hardcopy of the fine manual for somebody to hit me over the head with it. It looks like it should really be obvious, but I can't find it.

+11  A: 

Ah, I figured it out myself. Rhino supports record/replay mode. While AAA syntax always keeps the objects in replay mode we can switch to record and back to replay just to clear the stub's behaviour.

It looks a little hackish, however ...

    public interface IFoo { string GetBar(); }
    [TestMethod]
    public void TestRhino()
    {
        var fi = MockRepository.GenerateStub<IFoo>();
        fi.Stub(x => x.GetBar()).Return("A");
        Assert.AreEqual("A", fi.GetBar());

        // Switch to record to clear behaviour and then back to replay
        fi.BackToRecord(BackToRecordOptions.All);
        fi.Replay();

        fi.Stub(x => x.GetBar()).Return("B");
        Assert.AreEqual("B", fi.GetBar());
    }

Update:

I'll be using this in the future, so things look a little nicer:

internal static class MockExtension {
    public static void ClearBehavior<T>(this T fi)
    {
        // Switch back to record and then to replay - that 
        // clears all behaviour and we can program new behavior.
        // Record/Replay do not occur otherwise in our tests, that another method of
        // using Rhino Mocks.

        fi.BackToRecord(BackToRecordOptions.All);
        fi.Replay();
    }
}
froh42
If you want to use terminology that is native to Rhino Mocks, make it ClearExpectations
Richard Szalay
@Richard: Behavior added with `.Stub` does not set expectations. You are thinking of Behavior added with `.Expect`.
Wim Coenen
very nice sample. thanks.
DevilDog74