Recently, I had to use the MultiMock functionality of Rhino.Mocks, which is regrettably not exposed through the static MockRepository helper functions. I ended up having to do it by hand by calling StrictMultiMock on a MockRepository instance. However, I found that calling the Stubbed property on my strict mock returned the error “requires a return value or an exception to throw” after interacting with that property.

var successful = MockRepository.GenerateStrictMock<IFoobar>();
successful.Stub(f => f.Length).Return(101);

var a = successful.Length; // 101
var b = successful.Length; // 101

var failure = new MockRepository().StrictMock<IFoobar>();
failure.Stub(f => f.Length).Return(103);

var c = failure.Length; // 0
var d = failure.Length; // Exception: Previous method 'IFoobar.get_Length();' requires a return value or an exception to throw.

After looking into the Rhino.Mocks source on GitHub, I found that the static helper functions in MockRepository invoked MockRepository.Replay on the freshly created mock before returning it. Presumably, just calling StrictMock or StrictMultiMock did not advance the mock to ‘replay’ mode, and it would not return anything when interrogated.

Changing my mock setup code to the following allowed it to function as I expected.

var successful = MockRepository.GenerateStrictMock<IFoobar>();
successful.Stub(f => f.Length).Return(101);

var a = successful.Length; // 101
var b = successful.Length; // 101

var failure = new MockRepository().StrictMock<IFoobar>();
failure.GetMockRepository().Replay(failure); // NEW!!!
failure.Stub(f => f.Length).Return(103);

var c = failure.Length; // 103
var d = failure.Length; // 103

Hopefully this helps someone else who runs into the same issue. This was tested on the 3.6.1 package available from NuGet.