You can create a public or internal constructor to take a dependency on AppJobManagement
. If you can, change the reference to an interface for this class as it may make testing easier.
public class ServiceToBeTested : ServiceBase
{
private IAppJobManagement _appJobManagment;
public ServiceToBeTested()
{
_appJobManagment = new AppJobManagement();
}
internal ServiceToBeTested(IAppJobManagement appJobManagment)
{
_appJobManagment = appJobManagment;
}
}
If the ServiceToBeTested
has a number of protected methods you would like to mock/fake, you should consider another dependency. For example, instead of:
public void DoSomethingImportant(string foo)
{
if (someCondition)
{
DoThisOneWay(foo);
}
else
{
DoThisDifferently(foo);
}
}
protected void DoThisOneWay(string foo) {}
protected void DoThisDifferently(string foo) {}
You can use another object:
public interface IServiceWorker
{
void DoThisOneWay(string foo);
void DoThisDifferently(string foo);
}
public void DoSomethingImportant(string foo)
{
if (someCondition)
{
_worker.DoThisOneWay(foo);
}
else
{
_worker.DoThisDifferently(foo);
}
}
The IServiceWorker _worker
would be easily mocked.
Update
Another pattern is to use a Factory
class to generate these objects.
public interface IAppJobManagementFactory
{
IAppJobManagement Create(string arg1, int arg2);
}
This too can be mocked.