As others have mentioned, you have to be careful when treading the fine line of what-to-test vs. what-not-to-test. It can lead to brittle tests that are too coupled with the implementation.
That being said, I absolutely see the utility in doing what you're trying to do. Testing the exception handling alone is worth asking this question.
The problem is rooted in your use of a static object (Task.Factory
). Static objects/methods are notorious for impeding testability. The solution to this problem is the same used for decoupling any type of static object - introducing a seam.
You are using the static object to kick off, and return a reference to, a Task
. This is something that you can define a role interface for. It might look something like:
public interface ITaskFactory
{
Task StartNew(Action action);
}
With a production implementation that looks like:
public class TplTaskFactory : ITaskFactory
{
public Task StartNew(Action action)
{
return Task.Factory.StartNew(action);
}
}
You could then use dependency injection to supply your SUT with a mock implementation of this interface, which gives you the capability you're looking for.