Search code examples
c#unit-testingrhino-mocks

RhinoMocks use default implementation for property


I have some code that I use with Entity Framework like

class Person{
  pubic Person() {
     Address = new Address();
  }
  public virtual Address Address { get; set; }
}

The reason I'm marking Address as virtual is for lazy loading.

Now to test, I'm stubbing the Person. But since it's stubbed, the Address getter just returns null (even though it's set in the constructor). If I stub out the Address property (person.Stub(x => x.Address).Return(new Address());) things work fine. But I don't really want to have to stub out the property! Is there any way to tell RhinoMocks not to override this getter even though it's virtual?


Solution

  • Sure, but you have to use a partial mock:

    var person = MockRepository.GeneratePartialMock<Person>();