Search code examples
c#visual-studio-2008rhino-mocksrhino-mocks-3.5

How can I set a value to a Rhino Mocked object?


I have a scenario like this:

 form = MockRepository.GenerateMock<IAddAddressForm>();
 mediator = new AddAddressMediator(form);

The mediator is the real object under test and needs to be able to set values for the "form" object.

But the only way I can see to set values for the form object is like this:

  form.Stub(x=>x.FirstName).Return(item.FirstName)

I don't want to be doing that in my real code.

Am I missing the point of mocks?


Solution

  • Stubs have built in support for property behaviour. In cases where you aren't using stubs, you can use the PropertyBehaviour() method for a similar effect.

    Within the mediator, you should be using the form object normally -- it should not know that it has been handed a fake object.

    This code:

    form.Stub(x=>x.FirstName).Return(item.FirstName)
    

    should not be in your real object, but may be part of your test to set up the expectations for how you will use your mock object.

    Edit:

    From what you've provided, I can't judge whether you're "missing the point of mocks". The essential purpose is to provide a way to test code that has dependencies in isolation from those dependencies. Have a look at Martin Fowler's essay "Mocks Aren't Stubs", and the Usage Guidance section of the Rhino Mocks documentation.