public class MyClass: AbstractBase
{
public override bool Init(IAppContext contextIn)
{
if (base.Init(contextIn))
{
//my code
}
}
}
I have a class as given above and wanted to write a unit test for the Init method and have mocked the IAppContext. How can I use mock to bypass the call to base?
This is what i am doing:
Mock<IAppContext> mockContex = new Mock<IAppContext >();
MyClass myClassInstance - new MyClass ();
myClassInstance.Init(mockContex.object);
The base.init looks like:
public virtual bool Init(IAppContext context_in)
{
if (context_in == null)
{
throw new ArgumentNullException("context_in", "IAppContext argument s null");
}
this.myCommunication = context_in.getInterface<ICommunication>();
if (this.myCommunication == null)
{
throw new ArgumentNullException("myCommunication", "ICommunication argument is null");
}
this.myStateManager = new IStateManager(this.myCommunication);
if (this.myStateManager == null)
{
throw new InvalidOperationException("Could not create the State Manager");
}
return true;
}
You can setup your
IAppContextmock in a way thatbase.Initwill return true:Now
base.Initwill returntruewhen called withappContextMock.Note that you don’t need your last null check (
this.myStateManager == null) – the only way fornew IStateManager(this.myCommunication)to fail, is to throw exception. If it does, it won’t get to null check part anyways.