I have a Singleton that is available in my class through a static property similar to this: OtherClassNotBeingTested.Instance.SomeInstanceMethod()
I would like to test my class without creating one of these objects. Is there a way for RhinoMocks to return a stub when getter is called for a static property Instance?
To be more clear, here is the code for the instance property:
public static OtherClassNotBeingTested Instance
{
get
{
if (mInstance == null)
{
lock (mSyncRoot)
{
if (mInstance == null)
{
mInstance = new OtherClassNotBeingTested();
}
}
}
return mInstance;
}
}
Update: Here is how I fixed it:
class ClassBeingTested
{
public ClassBeingTested(IGuiInterface iGui):this(iGui, Control.Instance)
{
}
public ClassBeingTested(IGuiInterface iGui, IControl control)
{
mControl = control;
}
}
My unit tests call the second constructor. The actual code calls the first constructor. The code in the class uses the local mControl field instead of singleton. (I think this is called dependency injection.)
I also reorganized Singleton as suggested by Tony Pony.