I am adding tests to some gnarly legacy code to have enough confidence to seriously reorganize it. One of the problems is that the one who wrote the code obviously did not try to make the code testable (given that he never wrote a single unit test!)
A common problem is that there are currently no interfaces, but only an 11-level inheritance chain. I use Rhino Mocks to isolate the tested class from its dependencies, but since I am mocking the class, not the interface, I can only drown out the read-only property if it has the virtual .
My current thinking is that I just add the virtual to the property. It is not planned to add any additional objects to the existing dependency chain, and this will allow you to write tests.
Are there any arguments against adding the virtual , or is this an acceptable compromise to get the tests?
Sample Code ...
In the test class:
var someClassStub = MockRepository.GenerateStub<SomeClass>(); someClassStub.Stub(s => s.SomeProperty).Return("Test");
In SomeClass:
public virtual string SomeProperty { get { return someDependency.SomeMethod(); } }
source share