Yesterday I found myself stucked with a strange exception while programming a unit test using Rhino Mocks.

Supose you have an interface like the following one.

1public interface ITest
2{
3    object Freak();
4}

You can try a mock like the following:

1var test = MockRepository.GenerateMock();
2
3test.Expect(t => t.Freak()).IgnoreArguments()
4    .WhenCalled(x => x.ReturnValue = "THIS IS A RETURN VALUE")
5    .Repeat.Any();

Calling test.Freak(); will result in a return value of “THIS IS A RETURN VALUE"

Now change the interface to be:

1public interface ITest
2{
3    string Freak();
4}

Calling test.Freak(); results in the following exception: Method ‘ITest.Freak();’ requires a return value or an exception to throw.

The solution is easy just change the Expect call to look like this one:

1test.Expect(t =>; t.Freak()).IgnoreArguments()
2    .WhenCalled(x =>; x.ReturnValue = "THIS IS A RETURN VALUE")
3    .Return(string.Empty) // THIS IS NEEDED SO RHINO MOCKS KNOWS THE TYPE RETURNED FROM THE METHOD CALL.
4    .Repeat.Any();

The fake return value is ignored in favor of the given by the WhenCalled delegate!!!

Hope it helps.