Testing an inheriting C # class
I have the following code in c♯ but cannot figure out how to test it; I need to test aMethodUnderTest. I cannot change ABaseClass like in the library. But if I have to test AClass, I need to be able to establish what TheThing returns. Anyone have any ideas or experience with testing this type of code?
public ABaseClass
{
protected ThingBaseClass theThing {get;}
public virtual ....
...
}
public AClassUnderTest : ABaseClass
{
public MyThing Thing
{
get
{
return (MyThing)base.theThing;
}
}
public void aMethedUnderTest()
{
Thing.doSomething();
}
}
Note. The initial value for the Thing parameter is null. TheThing doesn't have a public setter.
I think you can create a mock that inherits ABaseClass and then check if AClass.Thing returns the same result as ABaseClassMock.theThing
You can use mocking libraries like Moles. This way you can set the value to "thing" from your test code. http://research.microsoft.com/en-us/projects/moles/
You can create a layout ABaseClass
and still install theThing
with MOQ
(even if it doesn't have a setter)
var mock = new Mock<ABaseClass>();
mock.SetupGet(x => x.theThing).Returns(\*Whatever you need*\);