What does Rhino Mocks mean by "requires a return value or an exception to throw"?

Chris picture Chris · Jun 12, 2012 · Viewed 8.2k times · Source

When mocking a call to a WCF Service, I get the following error:

Method 'ICustomerEntities.GetCustomerFromPhoneNumber("01234123123");' requires a return value or an exception to throw.

I've googled this and searched on here - all I can find is I need to re-order various calls etc, but that doesn't seem to make sense in my situation? Perhaps someone could point out to me that it actually does?

My test setup looks like this

_entities = MockRepository.GenerateStrictMock<ICustomerEntities>();

And the test method fails on the third line, setting result2

_entities.Expect(ip => ip.GetCustomerFromPhoneNumber("01234123123"));
var test = MockRepository.GenerateMock<ICustomerEntities>(_factory);
var result2 = _entities.GetCustomerFromPhoneNumber("01234123123");
var result = test.GetAllCustomersWithAGivenPhoneNumber("01234123123");
Assert.AreEqual(result,result2);

The original call is attempting to mock this (its in a method called GetAllCustomersWithAGivenPhoneNumber):

using (var entities = _factory.CreateEntities())
{
    var customer = entities.GetCustomerFromPhoneNumber(telephoneNumber);
}

Answer

GazTheDestroyer picture GazTheDestroyer · Jun 12, 2012

Remember a mocked object is just that, a mock. There is no implementation at all, so if the mock needs to return a value from a function, it has no way of knowing what to return unless you tell it.

I assume your GetCustomerFromPhoneNumber() returns a Customer object or id, so you need to tell Rhino what to return:

I've never used Rhino, but hopefully this will point you in the right direction even if the syntax isn't quite right.

var test = MockRepository.GenerateMock<ICustomerEntities>(_factory);
test.Stub(ent => ent.GetCustomerFromPhoneNumber("01234123123")).Return(new Customer());