Reputation: 3904
I have the following method:
public void ParseRebootData(RebootDeviceDto rebootDeviceDto)
{
if (rebootDeviceDto.RebootAtUtc.CompareTo(DateTime.UtcNow) <= 0) // if time is either now or in the past, reboot instantly
{
Console.WriteLine("Rebooting machine now");
await new Tasks.Reboot().RebootNow(); // <-- I want to test this line
}
else // schedule the reboot
{
Console.WriteLine($"Scheduling reboot at {rebootDeviceDto.RebootAtUtc}");
_backgroundJobClient.Schedule<Tasks.Reboot>(x => x.RebootNow(), rebootDeviceDto.RebootAtUtc);
}
}
_backgroundJobClient
is passed through by dependency injection in the constructor.
I want to test the line await new Tasks.Reboot().RebootNow();
if it's being called, using Moq's Verify
method.
Since Tasks.Reboot
is just a class, which is not passed through dependency injection, I don't know if I can test this call.
One of the solutions might be to create a TasksService
which I can override in my unit tests (which might not be a bad idea) but I want to know if this is possible at all, as is.
Upvotes: 1
Views: 61
Reputation: 22436
With Moq, you can only provide mock objects from the outside. You cannot replace a static instantiation with another one.
There are other testing frameworks that can do this, e.g. the shims of Microsoft Fakes. On the other hand, shims are supposed to be used with third-party-code. If you can change the code and inject a TasksService, this is the preferred method in order to decouple the code.
Upvotes: 4