20 May 2010

Rhinomocks stub mulitple return values

Rhinomocks stubbing is great, but sometimes we need a stubbed instance to return different values.

[Test]
public void gggg()
{
var myobject = MockRepository.GenerateStub<IMyInterface>();
myobject.Stub(x => myobject.MyMethod()).Return("woof");
myobject.Stub(x => x.MyMethod()).Return("croak");

In this case MyMethod will always return “woof”. The “croak” will only be return if we use the .Repeat statements to tell Rhinomocks to limit the usage of the values.

[Test]
public void gggg()
{
var myobject = MockRepository.GenerateStub<IMyInterface>();
myobject.Stub(x => myobject.MyMethod()).Return("woof").Repeat.Once();
myobject.Stub(x => x.MyMethod()).Return("croak");
}
Here the first call to MyMethod will return “woof”, all subsequent calls will return “croak”.

1 comment:

  1. In your examples should not the lambda in both calls to Stub be the same? i.e. I don't think x => myobject.MyMethod() is going to work. It should be x => x.MyMethod() in both cases.

    ReplyDelete