Imagine a function like this:
private static ConcurrentList<object> list = new ConcurrentList<object>();
public void Add(object x)
{
Task.Factory.StartNew(() =>
{
list.Add(x);
}
}
I don’t care WHEN exactly the fentry is added to the list, but i need it to be added in the end ( obviously 😉 )
I don’t see a way to properly unittest stuff like this without returning any callback-handler or sth. and therefor adding logic that’s not required for the program
How would you do it?
One way to do this is to make your type configurable such that it takes a
TaskSchedulerinstance.Now in your unit tests what you can do is create a testable version of
TaskScheduler. This is an abstract class which is designed to be configurable. Simple have the schedule function add the items into a queue and then add a function to manually do all of the queue items “now”. Then your unit test can look like thisExample implementation of
TestableScehduler