K
Kenneth Cochran
It has become painfully obvious to me that I lack enough knowledge of C#'s
capabilities and limitations to coheres the project I'm working on into a
test framework. So I'm starting this thread to document my attempts and beg
for the occasional tidbits of help from the C# development community. I hope
this will prove useful to others who read this thread as well. The project I
inherited is several hundred thousand lines of code; all without unit tests.
I'm attempting to get the entire project under test making minimal changes
to do so. So without further ado:
public class ParentClass
{
private IMyInterface ProductionObject;
protected virtual IMyInterface createMyObject()
{
return new MyClass(this);
}
public ParentClass()
{
ProductionObject = createMyObject();
}
public Method()
{
...
ProductionObject.DoSomething();
...
}
}
ParentClass created ProductionObject in its constructor. ProductionObject is
later used by Method(). The first thing I did was extract creation to a
virtual method: createMyObject(). Continuing on...
public class ChildClassarentClass
{
private IMyInterface mockMyObject;
protected override IMyInterface createMyObject()
{
return mockMyObject;
}
public ChildClass(IMyInterface mockMyObject)
{
this.mockMyObject = mockMyObject;
}
}
ChildClass overrides createMyObject to return a mock object created by the
ChildClass constructor. This is where things begin to derail. C# base
constructors are implicitly called by child constructors. The base
constructor correctly calls ChildClass's overridden createMyObject.
Unfortunately, since the child constructor has not yet finished,
mockMyObject is still undefined. The base constructor returns control to the
child constructor which assigns mockMyObject from its parameter but at this
point it's too late. When the test is is run on ParentClass.Method() it
accesses and undefined mockMyObject and throws an exception. Any
suggestions?
capabilities and limitations to coheres the project I'm working on into a
test framework. So I'm starting this thread to document my attempts and beg
for the occasional tidbits of help from the C# development community. I hope
this will prove useful to others who read this thread as well. The project I
inherited is several hundred thousand lines of code; all without unit tests.
I'm attempting to get the entire project under test making minimal changes
to do so. So without further ado:
public class ParentClass
{
private IMyInterface ProductionObject;
protected virtual IMyInterface createMyObject()
{
return new MyClass(this);
}
public ParentClass()
{
ProductionObject = createMyObject();
}
public Method()
{
...
ProductionObject.DoSomething();
...
}
}
ParentClass created ProductionObject in its constructor. ProductionObject is
later used by Method(). The first thing I did was extract creation to a
virtual method: createMyObject(). Continuing on...
public class ChildClassarentClass
{
private IMyInterface mockMyObject;
protected override IMyInterface createMyObject()
{
return mockMyObject;
}
public ChildClass(IMyInterface mockMyObject)
{
this.mockMyObject = mockMyObject;
}
}
ChildClass overrides createMyObject to return a mock object created by the
ChildClass constructor. This is where things begin to derail. C# base
constructors are implicitly called by child constructors. The base
constructor correctly calls ChildClass's overridden createMyObject.
Unfortunately, since the child constructor has not yet finished,
mockMyObject is still undefined. The base constructor returns control to the
child constructor which assigns mockMyObject from its parameter but at this
point it's too late. When the test is is run on ParentClass.Method() it
accesses and undefined mockMyObject and throws an exception. Any
suggestions?