There are lots of different ways to resolve this. The simpliest way is to pass an interface to Beep into the class (either through constructor or method itself).
public Boolean BeepInTime(Interfaces.IDateTime time,TimeSpan beepTime, Interfaces.IBeep beep)
{
var h = time.GetTime();
if (h == beepTime)
{
return beep.Beeping();
}
else
{
return false;
}
}
In your unit test you create a mock IBeep object and pass in the mock. There are lots of third party mocking frameworks out there also so you do not have to create your own mocks. I personally use Moq and it is a good one. RhinoMocks is also popular. Both are free.
If you do not want to have to create the Beep class in your production code then you could do this...
public Boolean BeepInTime(Interfaces.IDateTime time,TimeSpan beepTime, Interfaces.IBeep beep = null)
{
if (beep == null)
beep = new Beep();
var h = time.GetTime();
if (h == beepTime)
{
return beep.Beeping();
}
else
{
return false;
}
}
NOTE: I would not normally pass the interface into the constructor and assign it to a field rather then pass it into the method, especially if the class is called from many methods). There are many other ways to do this including using NInject. I am just providing these examples to give you a sneak peek into how it is done.
I would recommend doing a few days of research on the internet looking into these things before deciding on any one method.