Can I change the behaviour of a stub during runtime? Something 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.
WARNING
Changing the behaviour of stubs is a code smell!
It typically indicates that your unit tests are too complicated, are hard to understand and are brittle, breaking easily on correct changes of the class under test.
Check out:
So, please: only use this solution if you can't avoid it. In my eyes this article borders on bad advice - however there are rare situations where you really need it.
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 ...
Update:
I'll be using this in the future, so things look a little nicer: