views:

462

answers:

1

Hi,

i have a class e.g. DerivedClass that inherits from a base class e.g. BaseClass. BaseClass implements an interface called IBaseClass. IBaseClass has 1 property called TestProperty which is an integer auto property.

I PartialMultiMock DerivedClass like so:

derivedClassMock = repository.PartialMultiMock<DerivedClass>(typeof(IBaseInterface));

I then set an expectation as follows:

derivedClassMock.Expect(d => d.TestProperty).Return(141);

but i keep getting the following exception:

"Invalid call, the last call has been used or no call has been made (make sure that you are calling a virtual (C#) / Overridable (VB) method)."

If i mark the implementations of TestProperty in BaseClass as virtual, everything works but im trying to understand as to why this needs to be. If DerivedClass implemented IBaseInterface i wouldn't need to mark it as virtual to get the partial mock functionality. (at least i think not - please correct me if im wrong)

I then went a little further and tried to cast the multi mock to the IBaseInterface and set the expectation on that as follows:

var derivedInterface = (IBaseInterface) derivedClassMock;
derivedInterface.Expect(d => d.TestProperty).Return(1);

This test now runs without exceptions but the value returned from the TestProperty is not 1, as expected but 0 i.e. the default value of int. This suggests to me behavior similar to a stub.

Can someone explain, if possible, to help me understand this a little better as i'm confused? Can i not partially multi mock a class with an inherited interface andwhy does setting the expectation on the interface exhibit stub like behavior?

Thanks in advance.

+1  A: 

I haven't actually used PartialMultiMock but in this case, it seems that you are trying to stub out the return value of your DerivedClass -- which has implemented "TestProperty" as an auto-property. Since this is an auto-property with a setter then it seems to me that you would not require stubbing for this property at all.

What if you did:

var derivedClassMock = MockRepository.GenerateStub(); derivedClassMock.TestProperty = 146;

Matt Hidinger