Rhino Mocks Method ‘YYY' requires a return value or an exception to throw.
Rhino Mocks Method ‘YYY’ requires a return value or an exception to throw.
Categories:
less than a minute
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.
public interface ITest
{
object Freak();
}
You can try a mock like the following:
var test = MockRepository.GenerateMock();
test.Expect(t => t.Freak()).IgnoreArguments()
.WhenCalled(x => x.ReturnValue = "THIS IS A RETURN VALUE")
.Repeat.Any();
Calling test.Freak(); will result in a return value of “THIS IS A RETURN VALUE"
Now change the interface to be:
public interface ITest
{
string Freak();
}
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:
test.Expect(t =>; t.Freak()).IgnoreArguments()
.WhenCalled(x =>; x.ReturnValue = "THIS IS A RETURN VALUE")
.Return(string.Empty) // THIS IS NEEDED SO RHINO MOCKS KNOWS THE TYPE RETURNED FROM THE METHOD CALL.
.Repeat.Any();
The fake return value is ignored in favor of the given by the WhenCalled delegate!!!
Hope it helps.